1

My code has 5 buttons. I want that if button 2 is pressed button 3, 4 and 5 are disabled so even if they are pressed they do not work. And I want to use button 1 as a reset button. My code is

#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;

 }

Can anyone please help me with this! I need to make it so that if I press button 1 all the other buttons can work as well but if I press button 2 or 4 or 3 or 5 only that specific button works until button 1 is pressed again. Please help!

2 Answers2

0

Use State Machine for handling of your logic. I wrote a pseudo-code for your problem.

Basically you have two modes MODE_WHERE_ALL_BUTTONS_WORK and MODE_WITH_BUTTON_RESTRICTIONS. If the programm enters the first mode, buttons have different functionally defined then in the second mode. This approach is easily adjusted and you can also add more "modes" later.

#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;
    }

}
Pan Vi
  • 627
  • 6
  • 17
  • Hey Thanks for the advice but it starts to go in an unstoppable loop. So in stead oft he display changing when i press the button it starts a loop. Should I send you a code in private message or something? Can we collab – Abdullah Rauf Jun 07 '21 at 04:46
0

Try like so (Pseudo only):

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);
        }               
    }
}
seccpur
  • 4,996
  • 2
  • 13
  • 21
  • Hey can you please do it for key 1 as well. So I know how to proceed with the other buttons as well. I am new to coding things so need to understand as much as i can. Please see my code and tell if I am going in the right direction. – Abdullah Rauf Jun 07 '21 at 04:43
  • bool isAllKeysReady(int key) { if (key == 0) { status[0] = status[1] = status[2] = status[3] = status[4] = READY; //resetted return true; } if (key == 1) { status[0] = READY; //resetted status[1] = status[2] = status[3] = status[4] = BUSY; return true; } bool all_low = true; for (int i = 0; i < 5; i++) { if (status[i] == BUSY) { all_low = false; break; } } return all_low; } – Abdullah Rauf Jun 07 '21 at 04:44
  • If both keys have same functionality, you can do like so: `if (key == 0 || key== 1) ` on a single line. – seccpur Jun 07 '21 at 12:26