Created
August 3, 2015 09:04
-
-
Save theokelo/ed593753ba2a33051e61 to your computer and use it in GitHub Desktop.
Arduino Code for an LED button control
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
| int LED = 9; //pin for the LED | |
| int BUTTON = 7; //pin for the BUTTON | |
| int val = 0; //stores state of the pin | |
| int old_val = 0; //stores previous state of the pin | |
| int state = 0; | |
| int brightness = 128; //stores brightness value | |
| int old_b = 127; | |
| unsigned long startTime = 0; //when did we begin pressing? | |
| void setup() | |
| { | |
| pinMode(LED,OUTPUT); //set LED as an output | |
| pinMode(BUTTON,INPUT); //set pushbutton as an input | |
| } | |
| void loop() | |
| { | |
| val = digitalRead(BUTTON); //read input value and store it | |
| //check if there was a transition | |
| if((val == HIGH) && (old_val == LOW)) | |
| { | |
| state = 1 - state; //change value of state | |
| startTime = millis(); //millis() is the Arduino clock | |
| //it returns how many ms have passed | |
| //since the board has been reset | |
| delay(10); | |
| } | |
| //checking if the button is being held down | |
| if((val == HIGH) && (old_val == HIGH)) | |
| { | |
| if(state == 1 && (millis() - startTime) > 1000) | |
| { | |
| if(brightness < 255 && brightness > old_b) | |
| { | |
| old_b = brightness; | |
| brightness++; //increment brightness by 1 | |
| delay(5); | |
| } | |
| if (brightness == 255) old_b = 256; | |
| if(brightness > 0 && brightness < old_b) | |
| { | |
| old_b = brightness; | |
| brightness--; | |
| delay(5); | |
| } | |
| if(brightness == 0) old_b = -1; | |
| } | |
| } | |
| old_val = val; | |
| if(state == 1) | |
| analogWrite(LED, brightness); | |
| else | |
| analogWrite(LED,0); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment