cwiringpi

How can I make my source wait until button release before continuing?


If I press button 1 it will spam my screen saying it was pressed, I want it to wait until released before it does anything else, similar to a toggle but not a toggle.

I've tried adding a delay before, but that doesn't quite do exactly what I need... I want it to wait until released, instead of wait a hard set amount of time

#include <stdio.h>
#include <wiringPi.h>

int main(void) {
    wiringPiSetupGpio();
    while(1) {
        if(!digitalRead(8) && digitalRead(7)) {
            printf("Button-1 pressed\n");
        }
        if(!digitalRead(7) && digitalRead(8)) {
            printf("Button-2 pressed\n");
        }
    }
    return 0;
}

Expected outcome:

Button-1 pressed

Actual outcome:

Button-1 pressed
Button-1 pressed
Button-1 pressed
Button-1 pressed
Button-1 pressed
Button-1 pressed
etc...

Solution

  • You have to program this behavior by yourself.

    int sevenDown = 0;
    while(1) {
        if(!sevenDown && digitalRead(7)) {
            sevenDown = 1\n");
        }
        if(sevenDown && !digitalRead(7)) {
            printf("Button-1 pressed\n");
            sevenDown = 0;
        }
    }