Created
January 22, 2011 09:41
-
-
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
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
#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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey what if i want to fade more than one LED? I
ve tried with the delay but it
s 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