ButtonDualState Nextion and Blynk Switch

I think I’m wrong, but his code is to bad to understand in five minutes :stuck_out_tongue_winking_eye:

I would not have written it like that :thinking:

I don’t. Using Blynk.syncVirtual to force the Blynk server to send the latest values of the Blynk button widgets is unnecessary. Those values are known within the sketch anyway, except at startup when a BLYNK_CONNECTED callback can be use to run a Blynk.syncVirtual on the relevant pins.
If the intention is to use the Blynk.syncVirtual commands within the BLYNK_WRITE callbacks to execute the logic within the BLYNK_WRITE functions then these should be in functions of their own and called when needed.

Ultimately, you wouldn’t want the whole code to rely on having WiFi and a connection to the Blynk server to enable the Nextion to function - especially as this is being used to switch over to an emergency generator and there’s a good change of WiFi dropping out during this process.

Pete.

I will try

I am a newbie :grin::grin:

1 Like

I have tried this … and if there is only one “=”, the Mode switch in Nextion remains in a manual condition and does not want to switch to auto. so I still use two “==”.

this is my new coding

void setup() {
  Serial.begin(9600);
 nexInit();
  Blynk.begin(auth, ssid, pass);

pinMode(StartMan, OUTPUT);
pinMode(StopMan, OUTPUT);
timer.setInterval(1000L, Data);
timer.setInterval(1000L, Manual);
Blynk.syncAll();
}

void Data(){
  Mode.getValue(&dual_state);
   Mstart.getValue(&Start1);
   Mstop.getValue(&Stop1);
  
  if(Start1==0 && Stop1==0)(ManualMode=1);
   if(Start1==1 && Stop1==0)(ManualMode=2);
   if(Start1==0 && Stop1==1)(ManualMode=3);
   if(Start1==1 && Stop1==1)(ManualMode=3);
   delay(200);
}

void  Manual(){
  if(dual_state == 1){
  t5.setText(": Manual");
switch(ManualMode){
  
  case 1:
  digitalWrite(StartMan, HIGH);
  digitalWrite(StopMan, LOW);
  delay(1);
  break;

  case 2:
  digitalWrite(StartMan, LOW);
  digitalWrite(StopMan, LOW);
  delay(1);
  break;

  case 3:
  digitalWrite(StartMan, HIGH);
  digitalWrite(StopMan, HIGH);
  delay(1);
  break;
  default:
  break;
}
}
 else if(dual_state == 0){
   t5.setText(": Automatic");
   digitalWrite(StartMan, HIGH);
  digitalWrite(StopMan, HIGH);

  }
}

void loop() 
{
Blynk.run();
timer.run();
}

BLYNK_WRITE(V6) {
 ModeB = param.asInt(); 
 if (ModeB == 1) {
   Mode.setValue(1);
  digitalWrite(StopMan, LOW);
 }
 else if (ModeB == 0) {
  Mode.setValue(0);
 Mstart.setValue(0);
 Mstop.setValue(0);
  digitalWrite(StopMan, HIGH);
  digitalWrite(StartMan, HIGH); 
   Blynk.virtualWrite(V7, HIGH);
    Blynk.virtualWrite(V8, HIGH);
  
  
 }
}
BLYNK_WRITE(V7) {
 StartM = param.asInt();
  if(StartM == 1){
    Mstart.setValue(1);
    digitalWrite(StartMan, LOW); 
 } else if(StartM == 0) {
    Mstart.setValue(0);
    digitalWrite(StartMan, HIGH); 
  }
 
}
BLYNK_WRITE(V8){
 StopM = param.asInt();
  if(StopM == 1){
    Mstop.setValue(1);
    digitalWrite(StopMan, HIGH);
  }
  else if(StopM == 0){
    Mstop.setValue(0);
    digitalWrite(StopMan, LOW);
  }
}

Remember
== to compare
= To store

I began to think of replacing the usual switches in blynk with segmented switches is that better ??
I’m asking for advice from you guys

Segmented switches are very useful. :wink: