#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = "auth";
char ssid[] = "ssid";
char pass[] = "pass";
WidgetLED ledR1(V1),
ledY1(V2),
ledG1(V3),
ledR2(V4),
ledY2(V5),
ledG2(V6);
BlynkTimer timer;
const int green1 = 5;
const int yellow1 = 18;
const int red1 = 19;
const int green2 = 21;
const int yellow2 = 22;
const int red2 = 23;
const int sensor = 34;
void setup() {
// put your setup code here, to run once:
pinMode(red1, OUTPUT);
pinMode(red2, OUTPUT);
pinMode(yellow1, OUTPUT);
pinMode(yellow2, OUTPUT);
pinMode(green1, OUTPUT);
pinMode(green2, OUTPUT);
pinMode(sensor, INPUT);
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
timer.setInterval(1000L, blinkLedWidget);
}
void loop () {
Blynk.run();
timer.run();
}
BLYNK_WRITE(V0) {
switch (param.asInt())
{
case 1: { // Item 1
int sensorstate = digitalRead(sensor);
if (sensorstate == LOW) {
digitalWrite(red1, HIGH);
ledR1.on();
Serial.println("LED on V1: on"); //RED 1
digitalWrite(green2, HIGH);
ledG2.on();
Serial.println("LED on V6: on"); //GREEN 2
digitalWrite(red2, LOW);
ledR2.off();
Serial.println("LED on V4: off"); //RED 2
digitalWrite(green1, LOW);
ledG1.off();
Serial.println("LED on V3: off"); //GREEN 1
digitalWrite(yellow1, LOW);
ledY1.off();
Serial.println("LED on V2: off"); //YELLOW 1
digitalWrite(yellow2, LOW);
ledY2.off();
Serial.println("LED on V5: off"); //YELLOW 2
delay(1000);
} else if (sensorstate == HIGH) {
digitalWrite(red1, LOW);
ledR1.off();
Serial.println("LED on V1: off"); //RED 1
digitalWrite(green2, LOW);
ledG2.off();
Serial.println("LED on V6: off"); //GREEN 2
digitalWrite(red2, HIGH);
ledR2.on();
Serial.println("LED on V4: on"); //RED 2
digitalWrite(green1, HIGH);
ledG1.on();
Serial.println("LED on V3: on"); //GREEN 1
digitalWrite(yellow1, LOW);
ledY1.off();
Serial.println("LED on V2: off"); //YELLOW 1
digitalWrite(yellow2, LOW);
ledY2.off();
Serial.println("LED on V5: off"); //YELLOW 2
delay(1000);
break;
}
}
case 2: { // Item 2
digitalWrite(red1, LOW);
ledR1.off();
Serial.println("LED on V1: off"); //RED 1
digitalWrite(green2, LOW);
ledG2.off();
Serial.println("LED on V6: off"); //GREEN 2
digitalWrite(red2, LOW);
ledR2.off();
Serial.println("LED on V4: off"); //RED 2
digitalWrite(green1, LOW);
ledG1.off();
Serial.println("LED on V3: off"); //GREEN 1
digitalWrite(yellow1, HIGH);
digitalWrite(yellow2, HIGH);
ledY1.on(),
ledY2.on();
Serial.println("LED on V2: on"),
("LED on V5: on");
delay(500);
digitalWrite(yellow1, LOW);
digitalWrite(yellow2, LOW);
ledY1.off(),
ledY2.off();
Serial.println("LED on V2: off"),
("LED on V5: off");
delay(500);
break;
}
}
}
void blinkLedWidget()
{
}
BLYNK_WRITE(V0)
return 0 or 1, not 1 and 2
So , replace by case 0 and case 1.
but you 'll have disconnection with delay(1000)
If you want something to repeat then you should define a Blynk timer that runs at the desired interval.
The timer will call a function, but that can’t be a BLYNK_WRITE function, so you’ll need to move some of that code out of your BLYNK_WRITE into your timed function.
If you only want actions to repeat in certain situations then add an if
statement into your timed function, which only performs the actions if a flag variable is true.
Pete
I got it. Thanks