I can't figure out how to connect the LED fade circuit and the circuit to read a button. I need to be able to read the button so the fade effect can be turned on or off and adjusted. I have the code, I just can't make the circuit. Please help.
Edit: here is the code I'm working with.
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
- {
brightness =0;
}
}
}
old_val = val;
if (state ==1) {
analogWrite(LED, brightness);
}else{
analogWrite(LED, 0);
}
}
This is from Getting Started with Arduino by Massimo Banzi, if that helps.