Hi all,
As I am really new to embedded development and arduino I tried to start with the samples and do slight changes but I am having difficulties with a simple one.
I tried to change the button sample and make it works as follows: the led starts on and everytime I press the button it switchs its mode. Problem is it is having a completely unpredictable behaviour. I guess I did something really wrong.
I'll paste the code here hoping anyone can point me out what I am doing wrong:
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
int button = HIGH;
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
}
void loop(){
val = digitalRead(inputPin);
if (HIGH == val) {
if (button == HIGH)
button = LOW;
else
button = HIGH;
digitalWrite(ledPin, button); // turn LED
val = digitalRead(inputPin);
while (HIGH == val) {val = digitalRead(inputPin);}
}
}