diff --git a/1_Basic_GPIO/digitalIn.md b/1_Basic_GPIO/digitalIn.md index d5c6492..b0f97de 100644 --- a/1_Basic_GPIO/digitalIn.md +++ b/1_Basic_GPIO/digitalIn.md @@ -24,3 +24,42 @@ The usual `gpio_init` and `gpio_set_dir` are used to initialize and set the dire The same code can be used for controlling the fan from earlier experiment. LED connected to the `ledPin` is replaced with connection to the gate pin of the mosfet. - insert gif of controlling fan with button + +## ToggleButton + +Instead of setting the fan status to be the same as the the button status, this example toggles everytime the button is pressed. + +Below code demonstrates using a button to toggle the state of the fan between on and off. + +```c + int buttonStatus = gpio_get(buttonPin); + bool outputStatus = false; + int state = buttonStatus * 2 + gpio_get(buttonPin); + while (true) + { + state = buttonStatus * 2 + gpio_get(buttonPin); + switch (state) + { + case 1: + outputStatus = !outputStatus; + gpio_put(ledPin, outputStatus); + buttonStatus = 1; + sleep_ms(10); + break; + case 2: + buttonStatus = 0; + sleep_ms(10); + default: + break; + } + } +``` + +To toggle the state on button state changes, change in button press is recorded using `buttonStatus` variable. + +If the button is pressed, `buttonState` will be `0` and new state will be `1`. Hence, the value in the `status` variable will be `1`. When this happens, the fan state is toggled. Output doesn't change when the button is released. + + +Small delay is added to mitigate switch bouncing, as explained in [Circuit Basics blog on switch de-bouncing](https://www.circuitbasics.com/how-to-use-switch-debouncing-on-the-arduino/). + + - insert gif of fan toggling. diff --git a/1_Basic_GPIO/digitalOut.md b/1_Basic_GPIO/digitalOut.md index 7d27b35..2aec639 100644 --- a/1_Basic_GPIO/digitalOut.md +++ b/1_Basic_GPIO/digitalOut.md @@ -2,7 +2,7 @@ Add table of contents ## Blinky -The classic, helloWorld of embedded programming. This program turns the LED on and off periodically. +The classic, helloWorld of embedded programming. This program turns the onboard LED on and off periodically. ### Initial steps: - Create new project with project generator. @@ -12,6 +12,7 @@ The classic, helloWorld of embedded programming. This program turns the LED on a +Copy the below code into the project, build and upload to the pico. ### Basic Code: ``` C @@ -71,7 +72,7 @@ sleep_ms(500); This program blinks an LED connected to one of the GPIO(General Purpose Input/Output) ports. -For this example, I used Pin 20 == GP15. I connected a resistor to this pin and connected an LED in series with the resistor to the ground, as shown in the image below. This resistor limits the amount of current drawn by the LED. There are other ways to limit the current, which are discussed in this document later. +For this example, I used Pin 20(GP15). I connected a resistor to this pin and connected an LED in series with the resistor to the ground, as shown in the image below. This resistor limits the amount of current drawn by the LED. There are other ways to limit the current, which are discussed in this document later. ```c const uint LEDPin = 15; @@ -90,13 +91,13 @@ while (true) } ``` -The only change here is in line 1, `PICO_DEFAULT_LED_PIN` is changed to `15`, to represent GP15. Remember: in the program, a GPIO pin is represented by the GP number and NOT by the pin number, as shown in the [Pico's Pinout](https://pico.pinout.xyz/). +The only change here is in line 1, `PICO_DEFAULT_LED_PIN` is changed to `15`, to represent GP15. Remember: in the program, a GPIO pin in the program is represented by the GP number and NOT by the pin number, as shown in the [Pico's Pinout](https://pico.pinout.xyz/). --- ## Multiple Blinky -This program controls multiple LED connected to various GPIO ports. Here, the program is written such that it looks like a loading bar. For this setup, internal current limits are used. They are toggled programatically as shown in the code below. +This program controls multiple LED connected to various GPIO ports. Here, the program is written such that it resembles a loading bar. For this setup, internal current limits are used. They are toggled programatically as shown in the code below. ```c @@ -134,9 +135,11 @@ sleep_ms(500); The rest of the code is similar to the prior examples, which toggles the LEDS one by one, in series and turns them off, creating a loading effect. Visual output can be seen below. +add gif of above + ## ToggleBlinky -This example uses `gpio_get_out_level` to to get the output level for gicen given gpio port. The above example can be simplified by using this function call, as below. This function will be covered in DigitalIn.md as well. +This example uses `gpio_get_out_level` to to get the output level for given given gpio port. The above example can be simplified using this function call, as below. This function will be covered in DigitalIn.md as well. ```c int LED1 = 11; @@ -166,7 +169,7 @@ This example uses `gpio_get_out_level` to to get the output level for gicen give ## ParallelBlinky -In this example, the init and set dir operations are performed simultaneously using `gpio_init_mask` and `gpio_set_dir_masked` calls. These take a integer value called mask, where each bit represents one GPIO pin. The above example can be simplified and replicated with the code below. +In this example, the init and set dir operations are performed simultaneously using `gpio_init_mask` and `gpio_set_dir_masked` calls. These take a integer value called mask, where each bit of that integer represents one GPIO pin. The above example can be simplified and replicated with the code below. ```c int LED1 = 11; @@ -190,11 +193,14 @@ while (true) sleep_ms(500); ``` -Here, the `pinMask` variable represents the gpio pins. The position from the right represents the the gpio pin with that number and putting a `1` in that position means it is highlighted. So the 0th bit maps to GP0, 1st bit maps to GP1 and so on. We need GP11 to GP15, so the bits in 11th to 15th positions are set to `1`, denoting they are highlighted. -`gpio_init_mask` will initialize only the pins highlighted in the function's input, which is its first argument `pinMask`. -`gpio_set_dir_masked` will set the IO direction of only the highlighted pins in its first argument `pinMask` to the direction specified in its second argument `dirMask`. In dirMask, `1` denoted output and `0` denotes input. +Here, the `pinMask` variable represents the gpio pins. The position number from the right represents GP# with that number. A `1` in a position means the pin represented by that position is highlighted. So the 0th bit maps to GP0, 1st bit maps to GP1 and so on. We need GP11 to GP15, so the bits in 11th to 15th positions are set to `1`, making them highlighted. -For this example, the init mask functions will initialize GP11 to GP15, because they are highlighted by `pinMask`. The set dir mask function will focus on GP11 to GP15, because they are highlighted by `pinMask`, take the bits in highlighted locations from `dirMask` and set the direction if they are 1 or 0. +`gpio_init_mask` will initialize only the pins highlighted in the function's input. + + +`gpio_set_dir_masked` will set the IO direction of only the highlighted pins in its first argument to the direction specified in its second argument `dirMask`. In dirMask, `1` denoted output and `0` denotes input. + +For this example, the init mask functions will initialize GP11 to GP15, because they are highlighted by `pinMask`. The set dir mask function will focus on GP11 to GP15, because they are highlighted by `pinMask`, and set the direction if they are 1 or 0 based on `dirMask`. ---