Skip to content

Instantly share code, notes, and snippets.

@jonathanGB
Last active August 29, 2015 14:12
Show Gist options
  • Select an option

  • Save jonathanGB/ff530d167ce92bd6e1f0 to your computer and use it in GitHub Desktop.

Select an option

Save jonathanGB/ff530d167ce92bd6e1f0 to your computer and use it in GitHub Desktop.
Gradually turn on and off a LED w/ Arduino using recursion
// 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