Created
January 27, 2016 16:54
-
-
Save helloCaller/fa002947cc7a77660d5f to your computer and use it in GitHub Desktop.
Arduino, Dim LED with button
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
/* | |
Brighness Changer | |
Change Brightness of an LED | |
Adapted by Luke Garwood after example 5-2 from Getting Started With Arduino by Massimo Banzi and Michael Shiloh | |
*/ | |
int LEDPIN = 9; //set up variable LEDPIN as the pin to which the LED connects | |
int BUTTONPIN = 7; //set up variable BUTTONPIN as the pin to which the button connects | |
int ButtonValue; // set up variable to hold value input from BUTTONPIN. | |
int Old_ButtonValue; //set up variable to determine the previous value of ButtonValue | |
int LightOn = 0; // Used to determine whether the light is on or off, 1 being true = on, 0 being false = off | |
int Brightness = 0; //sets an initial brightness of 0 (none) | |
unsigned long startTime = 0; //determines begginning of button press | |
void setup() { //function that only runs once initially | |
pinMode(LEDPIN, OUTPUT); //pinMode(pin number, either INPUT or OUTPUT) | |
//Set up LEDPIN as an output | |
pinMode(BUTTONPIN, INPUT);// Set up BUTTONPIN as an Input (it recieves information from the Arduino rather than sending information/values) | |
} | |
void loop() { //function that continues to loop | |
ButtonValue = digitalRead(BUTTONPIN); //digitalRead tells us if the value for this pin is HIGH or LOW | |
//when button is pushed down digitalRead(BUTTONPIN) returns HIGH | |
if((ButtonValue == HIGH) && (Old_ButtonValue == LOW)) { //a condition that asserts if ButtonValue is read as HIGH (button is pushed) | |
//AND was previously LOW | |
LightOn = 1 - LightOn; // this alternates LightOn to be either 1 or 0 (1 being light on, 0 light off) | |
startTime = millis(); // capture a time at button press | |
delay(10); //for debouncing purposes, putting in a delay so that only one button press is registered | |
} | |
if((ButtonValue == HIGH) && (Old_ButtonValue == HIGH)){ // conditions to tell if the button is being held down | |
if(LightOn == 1 && (millis() - startTime) > 20){ //if the button is held for more than 20ms | |
Brightness ++; //incrementally increase brightness | |
delay(35); //delay so brightness doesn't change too quickly (longer the delay, longer it takes to reach max) | |
if(Brightness > 255){ //once brightness reaches 255 | |
Brightness = 0; // reset the LED back to 0 | |
} | |
} | |
} | |
Old_ButtonValue = ButtonValue; //after the above code has been executed, giving Old_ButtonValue the ButtonValue to detect the moment of button press. | |
if(LightOn == 1) { //when light clicks on | |
analogWrite(LEDPIN, Brightness); //turn LED on at whatever value Brightness is at | |
} else { | |
analogWrite(LEDPIN, 0); // or else click light off | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this code almost works, but turns off upon button release after long press.
How can you modify the code so it doesnt turn off upon button release