Created
July 24, 2012 19:27
-
-
Save aaronvb/3172080 to your computer and use it in GitHub Desktop.
Increase LED Brightness with Reset
This file contains 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
/* | |
Increase LED Brightness with Reset | |
Each press of the button increases the LED brightness by 10%. | |
Press and hold the button for 3 seconds to reset the LED to 0%. | |
created 2012 | |
by Aaron Van Bokhoven | |
*/ | |
const int buttonPin = 2; // number of the button pin | |
const int ledPin = 9; // number of the LED pin | |
int buttonState = 0; // button state starts at 0 | |
int brightness = 0; // brightness of LED starts at 0 | |
int fadeAmount = 25.50; // fadeAmount is equal to 10% of 255(max LED brightness) | |
int buttonPressedCounter = 0; // timer set to 0 whe depressing button | |
int buttonCounter = 0; // button counter set to 0 | |
int buttonLastState = 0; // button last state set to 0 | |
void setup() { | |
// set button as input | |
pinMode(buttonPin, INPUT); | |
// set LED as output | |
pinMode(ledPin, OUTPUT); | |
// init serialize communication at 9600 bits per second | |
Serial.begin(9600); | |
} | |
void loop() { | |
// get button state | |
buttonState = digitalRead(buttonPin); | |
// activate when button is being held down | |
if (buttonState == HIGH) { | |
buttonPressedCounter ++; // start counter | |
Serial.print("Pressed Timer: "); | |
Serial.println(buttonPressedCounter); | |
// if counter equals 100 turn off LED and reset counter to 0 | |
if (buttonPressedCounter == 50) { | |
buttonCounter = 0; | |
analogWrite(ledPin, 0); // reset LED | |
} | |
} | |
else { | |
// on button depress, set counter to 0 | |
buttonPressedCounter = 0; | |
} | |
// activate once to increase button counter, which adjusts the LED brightness | |
if (buttonState != buttonLastState) { | |
if (buttonState == HIGH) { | |
// increase counter only if less than 10 | |
if (buttonCounter < 10) { | |
buttonCounter ++; | |
} | |
Serial.print("LED Level: "); | |
Serial.println(buttonCounter); | |
analogWrite(ledPin, buttonCounter * fadeAmount); // adjust LED brightness to buttoncounter * fadeamount | |
} | |
buttonLastState = buttonState; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment