Struggling coding with buttons

后端 未结 1 1146
别跟我提以往
别跟我提以往 2021-01-27 05:33

Ive been really struggling recently with one problem.

The problem is that I cannot find a way to make one button trigger one sequence on an 8x8 display for 10 seconds an

相关标签:
1条回答
  • 2021-01-27 06:03

    The problem is delay(50);. The arduino does not check the input pins during these 50 milliseconds, and it's likely when you are pressing a button. You can either attach an interrupt function when the button is pressed (https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/). Or you could try a looped delay like so:

    unsigned int startTime = millis();
    unsigned int currentTime = startTime;
    while(currentTime - startTime >= 50) {
        // check button state
        currentTime = millis();
    }
    

    https://www.arduino.cc/en/Tutorial/StateChangeDetection

    From the code you posted it appears you don't need delay(50); at all.

    0 讨论(0)
提交回复
热议问题