Last active
August 29, 2015 14:12
-
-
Save jonathanGB/ff530d167ce92bd6e1f0 to your computer and use it in GitHub Desktop.
Gradually turn on and off a LED w/ Arduino using recursion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // must be a PWM on the Arduino Board | |
| #define led 5 | |
| int voltage = 0; | |
| void setup() { | |
| pinMode(led, OUTPUT); | |
| } | |
| void turnOn() { | |
| if (voltage != 255) | |
| { | |
| analogWrite(led, voltage); | |
| delay(5); // in ms | |
| voltage++; | |
| turnOn(); | |
| } | |
| } | |
| void turnOff() { | |
| if (voltage != 0) | |
| { | |
| analogWrite(led, voltage); | |
| delay(5); | |
| voltage--; | |
| turnOff(); | |
| } | |
| } | |
| void loop() { | |
| turnOn(); | |
| turnOff(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment