Hi all
I have a simple circuit: a push-button with a pull-down 10 kohm resistor connected to an LED.
I'm using an Arduino UNO. The loop has a freq of 100Hz.
Every time I push the button I would like to change the state of the LED.
Not every single time I can change its state.
I know that I have to do some kind of debouncing (software or hardware).
I've tried to look at the signal with the scope but it isn't clear to me why I have to do the debouncing.
I have connected the yellow probe through a yellow cable to the pull down resistor
I have read that signals >3V are considered HIGH while signals <1.5V are considered LOW by Arduino.
In the signal on the scope I don't see what causes the problem to che change of the state of the LED.
// Turn on the LED while the button is pressed and
// keep it on while it's released
// Turn off the LED after the button is pressed again.
const int LED=2; // LED connected to digital pin 2
const int BUTTON=3; // button connected to digital pin 3
int buttonState=0; // buttonState stores the value of the button state
int LEDstate; //LEDstate stores the value of the LED state
const int LOOP=4; // Needed to check the loop speed
int loopIndicator=0; // Needed to check the loop speed
void setup() {
pinMode(LED,OUTPUT); //Set the digital pin as output
pinMode(BUTTON,INPUT); //Set the digital pin as input
pinMode(LOOP,OUTPUT); // Needed to check the loop speed
}
void loop() {
buttonState=digitalRead(BUTTON); // read input value and store it
if(buttonState==HIGH){
LEDstate = !LEDstate;
}
// Check whether the LED state is HIGH
if(LEDstate==HIGH){
digitalWrite(LED,HIGH); // Turns the LED on
}
else{
digitalWrite(LED,LOW); // Turns the LED off
}
delay(10);
digitalWrite(LOOP,loopIndicator); // Needed to check the loop speed
loopIndicator=!loopIndicator; // Needed to check the loop speed
}
Cheers