Skip to content

Instantly share code, notes, and snippets.

@eladkehat
Created January 22, 2011 09:41
Show Gist options
  • Save eladkehat/791014 to your computer and use it in GitHub Desktop.
Save eladkehat/791014 to your computer and use it in GitHub Desktop.
Arduino sketch that uses a pushbutton to fade a LED in and out. On every button release the fader direction (in or out) is switched. See it working here: http://www.youtube.com/watch?v=osDDfbNOaM0
#define BUTTON 7
#define LED 9
int dir = 1;
int lum = 0;
boolean toggle = false;
void setup() {
pinMode(BUTTON, INPUT);
pinMode(LED, OUTPUT);
}
void loop() {
if (digitalRead(BUTTON) == HIGH) {
toggle = true;
lum += dir;
if ((lum >= 0) && (lum < 256))
analogWrite(LED, lum);
delay(20);
} else { // toggle direction
if (toggle) {
dir = -1 * dir;
toggle = false;
}
}
}
@diegosinao
Copy link

Hey what if i want to fade more than one LED? Ive tried with the delay but its not the best way. I know i should be using some fade-without-delay technic but not sure how. My thing is i want to only fade IN when a PIR is active and OUT when there`s no movement.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment