(Arduino study) LED brightness control by switch connection
The source code is combine of Brightness changing(http://feelmare.blogspot.kr/2013/12/arduino-study-led-brightness-changing.html) and LED turn on/off(http://feelmare.blogspot.kr/2013/12/arduino-led-onoff-using-switch-aduino.html).
The LDE brightness is changed when switch is connected.
////
The LDE brightness is changed when switch is connected.
////
const int LED=9;
const int BUTTON = 7;
int val = 0;
int old_val = 0;
int state = 0;
int brightness = 128;
unsigned long startTime = 0;
void setup(){
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT);
}
void loop()
{
val = digitalRead(BUTTON);
if( (val == HIGH) && (old_val == LOW) ){
state = 1-state;
startTime = millis();
delay(10);
}
if( (val == HIGH) && (old_val==HIGH) ){
if(state == 1 && (millis() - startTime) > 500 ){
brightness++;
delay(10);
if(brightness > 255){
brightness=0;
}
}
}
old_val = val;
if(state == 1)
{
analogWrite(LED, brightness);
}
}
////
