add smooth blinky example
This commit is contained in:
@@ -63,3 +63,7 @@ If the button is pressed, `buttonState` will be `0` and new state will be `1`. H
|
|||||||
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/).
|
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.
|
- insert gif of fan toggling.
|
||||||
|
|
||||||
|
## EfficientToggle
|
||||||
|
|
||||||
|
This example reads the changes in digital states using the interrupt model. Above examples use the polling method which constantly checks the state of the pin. Interrupt only mode only triggers a particular code to run only if a particular state is observed.
|
||||||
|
|||||||
@@ -233,6 +233,8 @@ Code to turn the fan on and off with a 2 second delay:
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Binary LED Counter
|
## Binary LED Counter
|
||||||
|
|
||||||
This example again uses 5 LEDS to visualize a 5 bit binary counter. Mask commands are used to set the LEDS simultaneously.
|
This example again uses 5 LEDS to visualize a 5 bit binary counter. Mask commands are used to set the LEDS simultaneously.
|
||||||
@@ -257,6 +259,34 @@ if counter is `11001`, `counter<<11` will be `1100100000000000`.
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## SmoothBlinky
|
||||||
|
|
||||||
|
This example demonstrates how brightness of an LED can be controlled by modulating the output using the time delay
|
||||||
|
|
||||||
|
```c
|
||||||
|
const uint LEDPin = 14;
|
||||||
|
gpio_init(LEDPin);
|
||||||
|
gpio_set_dir(LEDPin, GPIO_OUT);
|
||||||
|
gpio_set_drive_strength(LEDPin, GPIO_DRIVE_STRENGTH_2MA);
|
||||||
|
int onDelay=0;
|
||||||
|
int totalDelay=10;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 50; i++)
|
||||||
|
{
|
||||||
|
gpio_put(LEDPin, true);
|
||||||
|
sleep_ms(onDelay);
|
||||||
|
gpio_put(LEDPin, false);
|
||||||
|
sleep_ms(totalDelay - onDelay);
|
||||||
|
}
|
||||||
|
onDelay = (onDelay+1) % totalDelay;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
`onDelay` is maintained such that total time for each iteration remains constant, equal to `totalDelay`. This above code will make the LED look like the brightness changes.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Summary
|
## Summary
|
||||||
|
|
||||||
Below are the API calls used in this document:
|
Below are the API calls used in this document:
|
||||||
@@ -282,3 +312,5 @@ Below are the API calls used in this document:
|
|||||||
| - | - | - |
|
| - | - | - |
|
||||||
| `sleep_ms` | TIME | Pauses the program for given number of milli-seconds when called |
|
| `sleep_ms` | TIME | Pauses the program for given number of milli-seconds when called |
|
||||||
| `stdio_init_all` | -- | Initializes stuff |
|
| `stdio_init_all` | -- | Initializes stuff |
|
||||||
|
|
||||||
|
add fading led example by changing the amount of times the led blinks in a given cycle.
|
||||||
|
|||||||
Reference in New Issue
Block a user