Only one relay at the same time

Hi everyone.
He's been fighting the topic since morning. Maybe I will get help.

I use Arduino Nano + Nextion to control 4 relays. Everything works correctly. I would like the inclusion of one to disable the previous one. In order not to happen, two relays are turned on at the same time.

Here is my code:

#include "Nextion.h"
#include "NexDualStateButton.h" 

int R1 = 2, R2 = 3, R3 = 4, R4 = 5;

NexDSButton bt0 = NexDSButton(0, 1, "bt0");
NexDSButton bt1 = NexDSButton(0, 2, "bt1");
NexDSButton bt2 = NexDSButton(0, 3, "bt2");
NexDSButton bt3 = NexDSButton(0, 4, "bt3");
char buffer[100] = {0};

NexTouch *nex_listen_list[] = 
{
    &bt0, &bt1, &bt2, &bt3,
    NULL
};
void setup(void)
{    
    nexInit();
    bt0.attachPop(bt0PopCallback, &bt0);
    bt1.attachPop(bt0PopCallback, &bt1);
    bt2.attachPop(bt0PopCallback, &bt2);
    bt3.attachPop(bt0PopCallback, &bt3);
    dbSerialPrintln("setup done"); 
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
digitalWrite(R1, HIGH);
digitalWrite(R2, HIGH);
digitalWrite(R3, HIGH);
digitalWrite(R4, HIGH);
}

void loop(void)
{   
    nexLoop(nex_listen_list);
}

void bt0PopCallback(void *ptr)
{
    uint32_t dual_state;
    NexDSButton *btn = (NexDSButton *)ptr;
    
    dbSerialPrintln("Callback");
    dbSerialPrint("ptr=");
  
    dbSerialPrintln((uint32_t)ptr); 
    memset(buffer, 0, sizeof(buffer));
    
{  bt0.getValue(&dual_state);
   if(dual_state){digitalWrite(R1, HIGH);}else{digitalWrite(R1, LOW);}
} 
{  bt1.getValue(&dual_state);
   if(dual_state){digitalWrite(R2, HIGH);}else{digitalWrite(R2, LOW);}
} 
{  bt2.getValue(&dual_state);
   if(dual_state){digitalWrite(R3, HIGH);}else{digitalWrite(R3, LOW);}
}  
{   bt3.getValue(&dual_state);
   if(dual_state){digitalWrite(R4, HIGH);}else{digitalWrite(R4, LOW);}
}
}

Turning a relay off when it is already off won't cause a problem. So, create a function to turn all 4 relays off. Call that function before you return any relay on.

Can you help? I'm just starting my adventure with Arduiono ...

ZGi:
Can you help? I'm just starting my adventure with Arduiono ...

So, why are you over-using curly braces? Why are you putting anything after the {? Why are you putting multiple statements on one line?

This mess:

{  bt0.getValue(&dual_state);
   if(dual_state){digitalWrite(R1, HIGH);}else{digitalWrite(R1, LOW);}
}

should be

  bt0.getValue(&dual_state);
  if(dual_state)
 {
    digitalWrite(R1, HIGH);
 }
 else
 {
    digitalWrite(R1, LOW);
 }

Adding comments to explain which statement turns the relay on, and which turns it off would be useful.

  bt0.getValue(&dual_state);
  if(dual_state)
 {
    digitalWrite(R1, HIGH);
 }
 else
 {
    allRelaysOff();
    digitalWrite(R1, LOW);
 }

Then, after loop() ends, add:

void allRelaysOff()
{
   digitalWrite(R1, HIGH);
   digitalWrite(R2, HIGH);
   digitalWrite(R3, HIGH);
   digitalWrite(R4, HIGH);
}

OKAY.
I clean up :), I added comments, but it does not work correctly.
I turn on 1, then 2 (the first one turns off), then 3 (the second one turns off), then 4 (the third one turns off), but if I want to turn on the first one, it stays on 4.
I do not know if this is not the fault of the two-position switches used in nextion ...

I do not know if this is not the fault of the two-position switches used in nextion ...

Or the code you modified. Since we can't see what you actually did, or know whether HIGH means on or off, we can no longer help you.

Maybe I did not express myself clearly.
I would like to switch between relays independently.

That is, I squeeze one, the previous one turns off, and so on in a circle.

The relays are controlled by low state, therefore HIGH is off mode;)

My code:

#include "Nextion.h"
#include "NexDualStateButton.h" 

int R1 = 2, R2 = 3, R3 = 4, R4 = 5;

NexDSButton bt0 = NexDSButton(0, 1, "bt0");
NexDSButton bt1 = NexDSButton(0, 2, "bt1");
NexDSButton bt2 = NexDSButton(0, 3, "bt2");
NexDSButton bt3 = NexDSButton(0, 4, "bt3");
char buffer[100] = {0};

NexTouch *nex_listen_list[] = 
{
    &bt0, &bt1, &bt2, &bt3,
    NULL
};
void setup(void)
{    
    nexInit();
    bt0.attachPop(bt0PopCallback, &bt0);
    bt1.attachPop(bt0PopCallback, &bt1);
    bt2.attachPop(bt0PopCallback, &bt2);
    bt3.attachPop(bt0PopCallback, &bt3);
    dbSerialPrintln("setup done"); 
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
digitalWrite(R1, HIGH);
digitalWrite(R2, HIGH);
digitalWrite(R3, HIGH);
digitalWrite(R4, HIGH);
}

void loop(void)
{   
    nexLoop(nex_listen_list);
}

void bt0PopCallback(void *ptr)
{
    uint32_t dual_state;
    NexDSButton *btn = (NexDSButton *)ptr;
    
    dbSerialPrintln("Callback");
    dbSerialPrint("ptr=");
  
    dbSerialPrintln((uint32_t)ptr); 
    memset(buffer, 0, sizeof(buffer));

// przekaźnik 1
   bt0.getValue(&dual_state); 
   if(dual_state)
   {
   digitalWrite(R1, HIGH);
   }
   else
   {
   allRelaysOff(); 
   digitalWrite(R1, LOW);
   }

// przekaźnik 2
   bt1.getValue(&dual_state);
   if(dual_state)
   {
   digitalWrite(R2, HIGH);
   }
   else
   {
   allRelaysOff(); 
   digitalWrite(R2, LOW);
   }

// przekaźnik 3
   bt2.getValue(&dual_state);
   if(dual_state)
   {
   digitalWrite(R3, HIGH);
   }
   else
   {
   allRelaysOff(); 
   digitalWrite(R3, LOW);
   }
   
// przekaźnik 4
   bt3.getValue(&dual_state);
   if(dual_state)
   {
   digitalWrite(R4, HIGH);
   }
   else
   {
   allRelaysOff(); 
   digitalWrite(R4, LOW);
   }
}

void allRelaysOff()
{
   digitalWrite(R1, HIGH);
   digitalWrite(R2, HIGH);
   digitalWrite(R3, HIGH);
   digitalWrite(R4, HIGH);
}

I would like to switch between relays independently.

OK. Now, can you answer the question. Does writing HIGH to the pin turn the relay on or off?

OKAY. Everything works as it should. The error was in the HMI file.

Now you can switch between switches as you like.

THANK YOU FOR YOUR HELP !