Start a new topic

Button state change from arduino

Hi. I am trying to control two relays from nextion screen and it works fine. However the both relays cant be On at the same time, so if the relay1 is On and i turn relay2 On, relay1 should turn Off. This i can also do, but what i dont understand is how to change the ralay1 button on the screen also Off. Tried sendCommand but cant get it to work :( Could you please help me.


Here is the cosw and HMI file


 

#include <SoftwareSerial.h>
#include <Nextion.h>

int relay1 = 7;
int relay2 = 6;

SoftwareSerial nextion(2, 3);// Nextion TX to pin 2 and RX to pin 3 of Arduino

Nextion myNextion(nextion, 9600); //create a Nextion object named myNextion using the nextion serial port @ 9600bps

boolean button1State;
boolean button2State;

void setup() {
  Serial.begin(9600);
  myNextion.init(); // send the initialization commands for Page 0
  
 pinMode (relay1, OUTPUT);
 pinMode (relay2, OUTPUT);
 digitalWrite(relay1, HIGH);
 digitalWrite(relay2, HIGH);
 
}

void loop() {
  
  String message = myNextion.listen(); //check for message

  if (message == "65 0 2 0 ffff ffff ffff") {
    myNextion.buttonToggle(button1State, "bt1", 0, 2);
    if (button1State == LOW) {
      digitalWrite(relay1, HIGH); 
    

   
      
    } else {
      // turn led1 off:
      digitalWrite(relay1, LOW);
    }
  
}

    if (message == "65 0 3 0 ffff ffff ffff") {
    myNextion.buttonToggle(button2State, "b1", 0, 2);
    if (button2State == LOW) {
      digitalWrite(relay2, HIGH);
   
      
    } else {
      // turn led2 off:
      digitalWrite(relay2, LOW);
    }
 } 
 

}

 

HMI
(216 KB)

Then you must also code this logic into your program.


Your source shows no such attempt to address this,

- the machine will do what you ask and follow what you told.



Oh, sorry. I had the wrong code atttached. Here i try to sendCommand 


#include <SoftwareSerial.h>
#include <Nextion.h>

int relay1 = 13;
int relay2 = 12;

SoftwareSerial nextion(2, 3);// Nextion TX to pin 2 and RX to pin 3 of Arduino


Nextion myNextion(nextion, 9600); //create a Nextion object named myNextion using the nextion serial port @ 9600bps

boolean button1State;
boolean button2State;

void setup() {
  
delay(1000);
  
  Serial.begin(9600);
  myNextion.init(); // send the initialization commands for Page 0
  
 pinMode (relay1, OUTPUT);
 pinMode (relay2, OUTPUT);

 delay(1000);
 
 digitalWrite(relay1, HIGH);
  digitalWrite(relay2, HIGH);
 
}

void loop() {
  
  String message = myNextion.listen(); //check for message

  if (message == "65 0 2 0 ffff ffff ffff") {
    myNextion.buttonToggle(button1State, "bt1", 0, 2);
    if (button1State == HIGH) {
      digitalWrite(relay2, HIGH);
      delay(100);
      digitalWrite(relay1, LOW); 
      delay(100);
      myNextion.sendCommand("bt1.picc=0"); //set "bt1" image to 0
      myNextion.sendCommand("ref bt1"); //refresh
      
    

   
      
    } else {
      // turn led1 off:
      digitalWrite(relay1, HIGH);
    }
  
}

    if (message == "65 0 3 0 ffff ffff ffff") {
    myNextion.buttonToggle(button2State, "b1", 0, 2);
    if (button2State == HIGH) {
      digitalWrite(relay1, HIGH);
       delay(100);
      digitalWrite(relay2, LOW); 
      delay(100);
      myNextion.sendCommand("bt0.picc=0"); //set "bt0" image to 0
      myNextion.sendCommand("ref bt0"); //refresh
   
      
    } else {
      // turn led2 off:
      digitalWrite(relay2, HIGH);
    }
 } 
 

}

 

Skip your code for a moment.  First you need to deal with your logic.

Logic starts in the mind, then flows to paper, then into design and code.


First I am not a fan of an unsupported unofficial library using ffff instead of 0xFF

- So personally I think someone attempting to use that which even the authors have

  abandoned has selected their troubles as a challenge, and .. challenge you have.


Second, I am not a fan of debugging code.

- it may be the artistic license of the programmer to code in a fashion the wish

  - but Booleans are true or false -- pins are HIGH and LOW. Booleans are not HIGH

    (unless they are from Colorado, where they are legally allowed to be HIGH)

- I am certainly not a fan of having to investigate the authors library to see what they have done


Combining two dual states as you have, is a tri state

  - state 0 - both off

  - state 1 - relay A on

  - state 2 - relay B on

  ( state 3 - relay A and B on is not permitted or not desired, I would argue the first)


IF you deal with the logic in this tri state, then you may have better clarity


The Nextion side behaviour of a dual state button is to toggle in dual state regardless.

     state = 1 - state

this means using two dual states, the undesired state 3 may occur and will need correction


Dual States are not set to check conditions by default, so ...

Is this logic Nextion side? or MCU side?
Or do you select a different way to represent it Nextion side?


I am not certain that you MCU side relay code (2nd code) is failing.

(you did not say what is failing, just throw all code up and said "here, fix it")

I would also not have tried to change the dual state ,picc but its .val

- changing the picture simply will only mess up how the state is displayed


I hope some of this is helpful, I am just not in the mood to code it myself

Login or Signup to post a comment