1

我的代码有 5 个按钮。我希望如果按下按钮 2,则按钮 3、4 和 5 被禁用,因此即使按下它们也不起作用。我想将按钮 1 用作重置按钮。我的代码是

#include <doxygen.h>
#include <NexButton.h>
#include <NexCheckbox.h>
#include <NexConfig.h>
#include <NexCrop.h>
#include <NexDualStateButton.h>
#include <NexGauge.h>
#include <NexGpio.h>
#include <NexHardware.h>
#include <NexHotspot.h>
#include <NexNumber.h>
#include <NexObject.h>
#include <NexPage.h>
#include <NexPicture.h>
#include <NexProgressBar.h>
#include <NexRadio.h>
#include <NexRtc.h>
#include <NexScrolltext.h>
#include <NexSlider.h>
#include <NexText.h>
#include <NexTimer.h>
#include <Nextion.h>
#include <NexTouch.h>
#include <NexUpload.h>
#include <NexVariable.h>
#include <NexWaveform.h>

const int BUTTON1_PIN = 22;
const int BUTTON2_PIN = 24;
const int BUTTON3_PIN = 26;
const int BUTTON4_PIN = 28;
const int BUTTON5_PIN = 30;

int lbtn1S = HIGH;
int lbtn2S = HIGH;
int lbtn3S = HIGH;
int lbtn4S = HIGH;
int lbtn5S = HIGH;

int btn1S;
int btn2S;
int btn3S;
int btn4S;
int btn5S;

int buttonDown = 0;

void setup() {

Serial.begin(9600);
Serial1.begin(9600);
delay(1000);

pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);
pinMode(BUTTON3_PIN, INPUT_PULLUP);
pinMode(BUTTON4_PIN, INPUT_PULLUP);
pinMode(BUTTON5_PIN, INPUT_PULLUP);
}

void loop() {

int btn1S = digitalRead(BUTTON1_PIN);
int btn2S = digitalRead(BUTTON2_PIN);
int btn3S = digitalRead(BUTTON3_PIN);
int btn4S = digitalRead(BUTTON4_PIN);
int btn5S = digitalRead(BUTTON5_PIN);

// --------- For Button 1 -------------------

if (lbtn1S == LOW && btn1S == HIGH){

    Serial.print("button 1 state: ");
    Serial.println(btn1S);

//Serial1.print("\xFF\xFF\xFF");
Serial1.print("page 0");
Serial1.print("\xFF\xFF\xFF");
Serial.println ("Hello World 1");
 }


 // --------- For Button 2 -------------------

 if (lbtn2S == LOW && btn2S == HIGH){

    Serial.print("button 2 state: ");
    Serial.println(btn2S);

//Serial1.print("\xFF\xFF\xFF");
Serial1.print("page 1");
Serial1.print("\xFF\xFF\xFF");
Serial.println ("Hello World 2");
 }

// --------- For Button 3 -------------------
if (lbtn3S == LOW && btn3S == HIGH){

Serial.print("button 3 state: ");
 Serial.println(btn3S);

//Serial1.print("\xFF\xFF\xFF");
Serial1.print("page 2");
Serial1.print("\xFF\xFF\xFF");
Serial.println ("Hello World 3");
 }

 // --------- For Button 4 -------------------
if (lbtn4S == LOW && btn4S == HIGH){

 Serial.print("button 4 state: ");
 Serial.println(btn4S);

//Serial1.print("\xFF\xFF\xFF");
Serial1.print("page 3");
Serial1.print("\xFF\xFF\xFF");
Serial.println ("Hello World 1");
 }

 // --------- For Button 5 -------------------
if (lbtn5S == LOW && btn5S == HIGH){

 Serial.print("button 5 state: ");
 Serial.println(btn5S);

//Serial1.print("\xFF\xFF\xFF");
Serial1.print("page 4");
Serial1.print("\xFF\xFF\xFF");
Serial.println ("Hello World 1");
  }

lbtn1S = btn1S;
lbtn2S = btn2S;
lbtn3S = btn3S;
lbtn4S = btn4S;
lbtn5S = btn5S;

 }

谁能帮我解决这个问题!我需要这样做,以便如果我按下按钮 1,所有其他按钮也可以正常工作,但如果我按下按钮 2、4、3 或 5,则只有该特定按钮才能工作,直到再次按下按钮 1。请帮忙!

4

2 回答 2

0

使用状态机来处理您的逻辑。我为你的问题写了一个伪代码

基本上你有两种模式MODE_WHERE_ALL_BUTTONS_WORKMODE_WITH_BUTTON_RESTRICTIONS. 如果程序进入第一种模式,按钮的功能定义与第二种模式不同。这种方法很容易调整,您也可以稍后添加更多“模式”。

#define MODE_WHERE_ALL_BUTTONS_WORK 1
#define MODE_WITH_BUTTON_RESTRICTIONS 2

int current_mode = MODE_WHERE_ALL_BUTTONS_WORK;

void loop() {

    switch (current_mode) {

    case MODE_WHERE_ALL_BUTTONS_WORK:
        handle_all_buttons_work_mode();
        break;

    case MODE_WITH_BUTTON_RESTRICTIONS:
        handle_mode_with_restriction();
        break;

    }

}

void handle_mode_with_restriction(void) {

    // first handle button actions 

    if (btn3 == PRESSED) {
        Serial.print("sorry button 3 wont do anything here!");
    }
    if (btn4 == PRESSED) {
        Serial.print("sorry button 4 wont do anything here!");
    }

    // add transitions to another state 
    if (btn1 == PRESSED) {
        Serial.print("button 1 will send me back to normal mode!");
        current_mode = MODE_WHERE_ALL_BUTTONS_WORK;
    }
}

void handle_all_buttons_work_mode(void) {

    // first handle button actions 

    if (btn3 == PRESSED) {
        Serial.print("action triggered by button 3 will be done!");
        //... whatever should happen when BTN3 pressed
    }
    if (btn4 == PRESSED) {
        Serial.print("action triggered by button 4 will be done!");
        //... whatever should happen when BTN4 pressed
    }

    // add transitions to another state 

    if (btn2 == PRESSED) {
        // here I want to enter the "special mode"
        current_mode = MODE_WITH_BUTTON_RESTRICTIONS;
    }

}
于 2021-06-04T08:18:45.827 回答
0

像这样尝试(仅限伪):

enum State {READY=0, BUSY};
int pins[] = { 22,24,26,28,30 };
State status[] = { READY, READY, READY, READY, READY };


void display(int i)
{
    char info[15];
    sprintf(info, "button %d state:", i + 1);
    Serial.print(info);
    Serial.print("\xFF\xFF\xFF");
    sprintf(info, "page %d", i);
    Serial.print(info);
    Serial.print("\xFF\xFF\xFF");
    sprintf(info, "Hello World  %d", i + 1);
    Serial.println(info);
}

bool isAllKeysReady(int key)
{
    if (key == 0) {
        status[0] = status[1] = status[2] = status[3] = status[4] = READY; //resetted
        return true;
    }

    bool all_low = true;
    for (int i = 0; i < 5; i++) {
        if (status[i] == BUSY) { all_low = false; break; }
    }
    return all_low;
}

void loop()
{
    for (int i = 0; i < 5; i++) {
        if (digitalRead(pins[i] == HIGH) && isAllKeysReady(i)) {
            display(i);
        }               
    }
}
于 2021-06-04T17:09:27.577 回答