When I use a button the main problem I have to solve in the management via microcontroller is the switch bouncing, when I press it. When a switch mechanism moves from one position to another, it doesn't do so instantly. It takes some time for the mechanism to stabilize in its new position. Meanwhile, the mechanism performs some oscillations such that, if we try to read the state of the switch at that moment, the result will be random.
Arduino (which works at 16 MHz) reads the digital pin to which the button is connected several times within its settling transient and perceives the event as if the button is pressed and released several times.

There are several ways to eliminate the bounce of a switch, both hardware and software. Hardware debouncing involves using an additional circuit to eliminate wobble and receive a smooth, smooth transition from HIGH to LOW or LOW to HIGH. It has the advantage that the process does not occupy the microcontroller, but requires additional components. The other approach is software debouncing. This involves delaying the acceptance of the button press until the transient has passed, in order to be sure that the button has actually been pressed and not an abnormal event.

Arduino 3 LED with debouncing.jpg

In the next sketch we look at a software approach. It is a simple process that is based on two states of the button: the previous one, stored in the past iteration of the loop(), and the current one, measured at the beginning of the key press and then the signal transition. If these two values are different, we will accept the button press, otherwise we will not. Furthermore, we must consider that not all switches have the same responses and transients. So, remember that in your projects you will have to adjust a delay() for this (do some testing and find out what is the transition period of your switches).

The sketch that I show you uses three LEDs, each time the button is pressed, one LED lights up and the previous one switches off in a continuous cycle. You can find it to this link: https://www.tinkercad.com/things/3yaJikfCIwn

// definizioni da usare nel codice
#define ledRed 3
#define ledYellow 4
#define ledGreen 5
#define button 7

// dichiarazione variabili di stato
// e assegnamento valore iniziale
int led = 0;
int buttonnow = LOW;
int buttonbefore= LOW;

// setup led e pulsante
void setup(){
    pinMode(ledRed, OUTPUT);
    pinMode(ledYellow, OUTPUT);
    pinMode(ledGreen, OUTPUT);
    pinMode(button, INPUT);
}

void loop(){
    // polling pulsante con debouncing
    buttonnow=digitalRead(button);
    if ((buttonnow==HIGH)&&(buttonbefore==LOW)) {
        led++;
        if (led>3){
            led=1;
        }
    }
    buttonbefore=buttonnow;
    delay (50);
   

    // gestione LED
    if (led==1){
       digitalWrite (ledRed, HIGH);
       digitalWrite (ledYellow, LOW);
       digitalWrite (ledGreen, LOW);
    }
    if (led==2){
        digitalWrite (ledRed, LOW);
        digitalWrite (ledYellow, HIGH);
        digitalWrite (ledGreen, LOW);
    }
    if (led==3){
        digitalWrite (ledRed, LOW);
        digitalWrite (ledYellow, LOW);
        digitalWrite (ledGreen, HIGH);
    }
}


Advertising

Advertising

Advertising