Looking for assistance with single Vpin control of multiple lights

Hi Guys,

Sorry for my bad english i am brazilian and new here.
I wonder if you can help. I make a code for control 4 lamps, with feedback using a ac sensor and relays, and this works fine. But im try make a routine with scenario. Exemple: With 1 vpin i can turn off all lamps in the house, or a group of lamps.
Thank you!

#include<BlynkSimpleEthernet.h>
#include <SimpleTimer.h>
SimpleTimer timer;

// Código para controlar reles com retorno da corrente AC

// RELE 01

int RELE_01 = 9;      // Rele 01 conectado na porta 16
int SENSORAC_01 = 8;   // Sensor AC 01 conectado na porta 4
int RETORNOA_01;       // Retorno A do sensor AC 01
int RETORNOB_01;       // Retorno B do sensor AC 01

// RELE 02

int RELE_02 = 7;        // Rele 02 conectado na porta 5
int SENSORAC_02 = 6;   // Sensor AC 02 conectado na porta 0
int RETORNOA_02;       // Retorno A do sensor AC 02
int RETORNOB_02;       // Retorno B do sensor AC 02

// RELE 03

int RELE_03 = 5;         // Rele 03 conectado na porta 2
int SENSORAC_03 = 4;   // Sensor AC 03 conectado na porta 14
int RETORNOA_03;        // Retorno A do sensor AC 03
int RETORNOB_03;        // Retorno B do sensor AC 03

// RELE 04

int RELE_04 = 3;        // Rele 03 conectado na porta 12
int SENSORAC_04 = 2;   // Sensor AC 03 conectado na porta 13
int RETORNOA_04;        // Retorno A do sensor AC 04
int RETORNOB_04;        // Retorno B do sensor AC 04


// Auto token do app Blynk

char auth[] = "5750791316d3403e83244101c251d9ca";

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth);

  // Configura os pinos e transforma todos reles em “off” antes de iniciar o sistema

  //RELE 01

  pinMode(RELE_01, OUTPUT);
  pinMode(SENSORAC_01, INPUT);
  digitalWrite(RELE_01, 0);

  //RELE 02

  pinMode(RELE_02, OUTPUT);
  pinMode(SENSORAC_02, INPUT);
  digitalWrite(RELE_02, 0);

  //RELE 03

  pinMode(RELE_03, OUTPUT);
  pinMode(SENSORAC_03, INPUT);
  digitalWrite(RELE_03, 0);

  //RELE 04

  pinMode(RELE_04, OUTPUT);
  pinMode(SENSORAC_04, INPUT);
  digitalWrite(RELE_04, 0);

  // Sincroniza o estado do sensor AC armazenado nas portas virtuais e recarrega quando o sistema é reiniciado

  Blynk.syncVirtual(V3);             // Estado do sensor AC 01
  Blynk.syncVirtual(V5);             // Estado do sensor AC 02
  Blynk.syncVirtual(V7);             // Estado do sensor AC 03
  Blynk.syncVirtual(V9);             // Estado do sensor AC 04

  timer.setInterval(200, SENSOR_AC); // Verifica a rotina SENSOR_AC a cada 200 milisegundos
}

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


void SENSOR_AC()
{
  RETORNOB_01 = digitalRead(SENSORAC_01);
  RETORNOB_02 = digitalRead(SENSORAC_02);
  RETORNOB_03 = digitalRead(SENSORAC_03);
  RETORNOB_04 = digitalRead(SENSORAC_04);

  if (RETORNOA_01 != RETORNOB_01)

  { // Se o retorno for igual, não há alteração

    Blynk.virtualWrite(V2, RETORNOB_01);

    // Atualiza o estado do botão no app, se o sensor AC sofrer alteração

    RETORNOA_01 = RETORNOB_01;
  }
  if (RETORNOA_02 != RETORNOB_02)
  {
    Blynk.virtualWrite(V4, RETORNOB_02);
    RETORNOA_02 = RETORNOB_02;
  }
  if (RETORNOA_03 != RETORNOB_03)
  {
    Blynk.virtualWrite(V6, RETORNOB_03);
    RETORNOA_03 = RETORNOB_03;
  }
  if (RETORNOA_04 != RETORNOB_04)
  {
    Blynk.virtualWrite(V8, RETORNOB_04);
    RETORNOA_04 = RETORNOB_04;
  }
}


// Sincroniza as portas virtuais de acordo com o APP
// RELE 01

BLYNK_WRITE(V2)
{
  digitalWrite(RELE_01, !digitalRead(RELE_01)); // Acionamento do botão
  Blynk.virtualWrite(V3, digitalRead(RELE_01));    // Armazena o estado da lampada
}

BLYNK_WRITE(V3)
{
  int pinData = param.asInt();       // Armazena o estado do rele
  digitalWrite(RELE_01, pinData); // Restabelece o estado do rele
}

// Rele 02

BLYNK_WRITE(V4)
{
  digitalWrite(RELE_02, !digitalRead(RELE_02));
  Blynk.virtualWrite(V5, digitalRead(RELE_02));
}

BLYNK_WRITE(V5)
{
  int pinData = param.asInt();
  digitalWrite(RELE_02, pinData);
}

// Rele 03

BLYNK_WRITE(V6)
{
  digitalWrite(RELE_03, !digitalRead(RELE_03));
  Blynk.virtualWrite(V7, digitalRead(RELE_03));
}

BLYNK_WRITE(V7)
{
  int pinData = param.asInt();
  digitalWrite(RELE_03, pinData);
}

// Light  4
BLYNK_WRITE(V8)
{
  digitalWrite(RELE_04, !digitalRead(RELE_04));
  Blynk.virtualWrite(V9, digitalRead(RELE_04));
}
BLYNK_WRITE(V9)
{
  int pinData = param.asInt();
  digitalWrite(RELE_04, pinData);
}

Anyone? :sweat:

Not sure what you need help with… it is just basic programming logic… If this action happens, then do this thing (or group of things).

BLYNK_WRITE(Vx)  // Virtual button on Vx to activate all 4 light functions
{
  int BTN = param.asInt();
  if (BTN == 1) {  // If button is pressed ON, then run all these functions.
    Light1;  // Run Light 1 function
    Light2;  // Run Light 2 function
    Light3;  // Run Light 3 function
    Light4;  // Run Light 4 function
  }
}

void Light1() {
  // Do stuff here to control Light 1
}

void Light2() {
  // Do stuff here to control Light 2
}

void Light3() {
  // Do stuff here to control Light 3
}

void Light4() {
  // Do stuff here to control Light 4
}

2 posts were split to a new topic: Can I get a current sensor to send to BLYNK that the light is on or off and change the status of the button