I did the easy examples of the book "Getting Started with Arduino". Now I am experimenting to get everything under the hood.
Button is connected to pin 9 and button is connected to pin 5. The setup is via a breadboard. Two resistors are inserted each 10 K.
The led is always on but lightening more when the button is pressed - why is that? It should only light when it has been turned on by a button press.
The simple code is here:
#define LED 5
#define BUTTON 9
int val = 0;
int old_val = 0;
int state = 0;
void setup() {
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT);
}
void loop() {
val = digitalRead(BUTTON); //read inputValue and store it
if((val == HIGH)&&(old_val == HIGH)){
state = 1;
}
else if((val == HIGH)&&(old_val == LOW)){
state = 1;
}
else if((val == LOW)&&(old_val == LOW)){
state = 0;
}
old_val = val;
if(state == 1) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
}