How to make sure that when you turn on one button, the rest are turned off?

Knowing if your relays are active HIGH or active LOW is the information we need.

Pete.

Show your updated code

! does mean not, but the value of brele1 in else section is 0 (i.e. false) so you making it true

Yes, you’re correct. It’s writing 1 (HIGH) to the relay in both cases.
It should of course be…

  brele1 = param.asInt();
  if (brele1 ==1)
    {
     digitalWrite(RELE1,brele1);
    }
    else
    {
      digitalWrite(RELE1,brele1);
    }

Which is why you’re a Blynk developer and I’m not!

Pete.

Ahh huh! Tripped me up…

Seemed logical to me when I was sitting on the sofa writing pseudo code on the iPad and half watching TV at the same time!

Pete.

Thanks a lot to everyone, I solved my problem. The relay closes at LOW

1 Like

If you got it going the way you want that’s great. Since you asked how to integrate into your script for your board try something like this. I’m assuming a lot here with normally closed vs normally open. Also there was a lot of re-declarations for variables in your script that wasn’t needed. By re-declarations I mean your renaming D1 to a variable name RELE1 and using that instead. By skipping that you can loop through it just fine. The same goes with virtual pins (V1 to brele1). I am not saying you can’t loop through variables with part string and part number, but sticking to numbers just skips to the point. However I have not worked directly with ESP8266WIFI, but I did download all the libraries and board data into my Arduino IDE and verified compiled just fine.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "6Vc4Oz-NCAzKp10lPHFiFMQxLMdRfkFA";
char ssid[] = "tenet";
char pass[] = "30121969";

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth,ssid,pass);
  
  
  for(int i=1; i<9; i++)//1 through 8. Plus one on the total.
  {
    pinMode(i,OUTPUT); 
    digitalWrite(i,HIGH);
    Blynk.virtualWrite(i,LOW);
  }
}



BLYNK_WRITE(1)
{
  int virtualPin = param.asInt();
  if (virtualPin == 1)
  {
    turnOffBtns(1);
  }
}

BLYNK_WRITE(2)
{
  int virtualPin = param.asInt();
  if (virtualPin == 1)
  {
    turnOffBtns(2);
  }
}

BLYNK_WRITE(3)
{
  int virtualPin = param.asInt();
  if (virtualPin == 1)
  {
    turnOffBtns(3);
  }
}

BLYNK_WRITE(4)
{
  int virtualPin = param.asInt();
  if (virtualPin == 1)
  {
    turnOffBtns(4);
  }
}

BLYNK_WRITE(5)
{
  int virtualPin = param.asInt();
  if (virtualPin == 1)
  {
    turnOffBtns(5);
  }
}

BLYNK_WRITE(6)
{
  int virtualPin = param.asInt();
  if (virtualPin == 1)
  {
    turnOffBtns(6);
  }
}

BLYNK_WRITE(7)
{
  int virtualPin = param.asInt();
  if (virtualPin == 1)
  {
    turnOffBtns(7);
  }
}

BLYNK_WRITE(8)
{
  int virtualPin = param.asInt();
  if (virtualPin == 1)
  {
    turnOffBtns(8);
  }
}

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




    
    
    
void turnOffBtns(int btnSwitchOn)
{
    int i = 0;
    int j = 0;
    int btnSwitches[8] = {1,2,3,4,5,6,7,8};// Which virtual pins are to be linked together? Remember to change "int btnSwitches[8]" to the correct amount of buttons.

    for(i=0; i<128; i++) {//Check all 128 virtual pins
        for(j=0; j<8; j++) { //Only check the correct virtual pins from array list.
                            
            if(i == btnSwitches[j]){//btnSwitches check buttons that we list only to turn off.  We check only those we really want out of total virtual pins
                    
                if(i == btnSwitchOn){//Turn on the one we want if it's a match with the virtual pin that was passed into function
                    Blynk.virtualWrite(btnSwitchOn,HIGH);//Turn Blynk Button To On State.
                    digitalWrite(i,LOW);//Switch Relay "LOW is ON"

                } else {// Turn off all other button/virtual pins from list if it's not the virtual pin that was passed into the function..
                    Blynk.virtualWrite(i,LOW);//Turn Blynk Button To Off State.
                    digitalWrite(i,HIGH);//Switch Relay "HIGH is OFF".
                }
            }
        }
    }
}

One thing about this script is that you would always have to match your digital pins to your virtual pins in blynk or something will be a missmatch. In that case you would then want to re-declare your digital pins on board to be something that matches and that may take a function actually to properly work or another array… In your case it all matches so your fine. And the more I think about it simply adding another array for digital board pins would solve mismatches if there was a need for that.

Anyways I can blab all day in circles about the thousands of ways to accomplish something…

2 Likes