Using Virtual pins value in a void loop as a variable

Hi,
I’m making a big project for my last year in secondary school in Belgium.
I use all sorts of sensors connected to an Arduino uno to check water quality in a hydroponics lobster system , this Arduino is connected to an ESP32 and connected via Blynk, so I can check all the values on my phone and the web.

I want to make an automation if the values are out off a certain range, for example lower than 25°C and higher than 30°C, then a virtual pin is set to alarm with a signal tower, ( Green, orange , red and white).

I got the automation part figured out, but using the virtual pins with an “if-statement” and using the values put out by BLYNK_WRITE(V10) in void loop() seems impossible.

Simply explained: V10 corresponds with OK, V11 WARNING, V12 ALARM, V13 WAIT

If the values are okay, the green light stays on, if there is a warning the orange light goes, an alarm andthe red light blinks but the green light can’t go on when there is a WARNING or an ALARM.

I’ve simplified the code for whats needed, so its easier to understand without all the serial communications happening with the Arduino and pushing values to Blynk, this is only one way. (Blynk → ESP32)

#define BLYNK_TEMPLATE_ID "***"
#define BLYNK_TEMPLATE_NAME "***"
#define BLYNK_AUTH_TOKEN "***"
#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";

int OK = 0;
int WARNING = 0;
int  ALARM = 0;
int WAIT = 0;

int GREEN = 18;
int ORANGE = 19;
int RED = 21;
int WHITE = 22;


BLYNK_WRITE(V10) // Executes when the value of virtual pin 10 changes
{
  if(param.asInt() == 1)
  {
    OK = 1;
  }
  else
  {
   OK = 0;   
  }
}

BLYNK_WRITE(V11) // Executes when the value of virtual pin 11 changes
{
  if(param.asInt() == 1)
  {
    WARNING = 1;
  }
  else
  {
   WARNING = 0;   
  }
}

BLYNK_WRITE(V12) // Executes when the value of virtual pin 12 changes
{
  if(param.asInt() == 1)
  {
    ALARM = 1;
  }
  else
  {
   ALARM = 0;   
  }
}

BLYNK_WRITE(V13) // Executes when the value of virtual pin 13 changes
{
  if(param.asInt() == 1)
  {
    WAIT = 1;
  }
  else
  {
   WAIT = 0;   
  }
}


unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
const long interval = 500;


// This function creates the timer object. It's part of Blynk library 
BlynkTimer timer; 

void myTimer() 
{
}



void setup()
{
  //Connecting to Blynk Cloud

    Serial.begin(9600);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass); 
  
  // Setting interval to send data to Blynk Cloud to 1000ms. 
  // It means that data will be sent every second
  timer.setInterval(1000L, myTimer);
  pinMode(GREEN, OUTPUT);
  pinMode(ORANGE, OUTPUT);
  pinMode(RED, OUTPUT);
  pinMode(WHITE, OUTPUT); 
}

void loop()
{
  Blynk.run(); 
  
  timer.run();
   
  Serial.println(a);
  currentMillis = millis();

  if (ALARM == 1) {
    if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
      digitalWrite(RED, !digitalRead(RED));
      digitalWrite(ORANGE, HIGH);
      digitalWrite(GREEN, HIGH);
      digitalWrite(WHITE, HIGH);
      
    }
   }
   
   else if (WARNING == 1) {
    digitalWrite(ORANGE, LOW);
    digitalWrite(GREEN, HIGH);
    digitalWrite(WHITE, HIGH);
    digitalWrite(RED, HIGH);
  }
  
  else if (WAIT == 1) {
    if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
      digitalWrite(WHITE, !digitalRead(WHITE));
      digitalWrite(ORANGE, HIGH);
      digitalWrite(GREEN, HIGH);
      digitalWrite(RED, HIGH);
      
    }
  } 
  
  else {
    digitalWrite(GREEN,! (OK == 1 && WARNING == 0 && ALARM == 0));
    digitalWrite(ORANGE,! (WARNING == 1 && OK == 0 && ALARM == 0));
    digitalWrite(RED, HIGH);
    digitalWrite(WHITE, HIGH);
  }      
}
}


Hope someone here could help me since there is a deadline on the Project.
Thanks in advance

Well, first of all this code should t be in your void loop…

Instead of doing millis() comparisons in your void loop you should be using a BlynkTimer, and as you already have one that calls the empty myTimer function then your code (minus the millis() comparisons) should be in there.

I suspect that once all of this millis stuff is scrapped then you’ll see the results you were hoping for.

Pete.

I’ve changed it like u said and it still didn’t want to compile all the way, an error with int OK =0;
I’ve tried multiple options, placing int twice, defining it in the BLYNK_WRITE, but the only thing that worked was changing the variable to something that was not “OK”.

So I changed it to “OKAY” and that seems to work!

Here is the edited code if anyone would ever have the same problem.

#define BLYNK_TEMPLATE_NAME "***"
#define BLYNK_AUTH_TOKEN "***"
#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "***";
char pass[] = "***";


int WARNING = 0;
int ALARM = 0;
int WAIT = 0;
int OKAY =0;

int GREEN = 18;
int ORANGE = 19;
int RED = 21;
int WHITE = 22;


BLYNK_WRITE(V10) // Executes when the value of virtual pin 10 changes
{
  if(param.asInt() == 1)
  {
    OKAY = 1;
  }
  else if(param.asInt() != 1) 
  { 
    OKAY = 0;   
  }
}

BLYNK_WRITE(V11) // Executes when the value of virtual pin 11 changes
{
  if(param.asInt() == 1)
  {
    WARNING = 1;
  }
  else
  {
   WARNING = 0;   
  }
}

BLYNK_WRITE(V12) // Executes when the value of virtual pin 12 changes
{
  if(param.asInt() == 1)
  {
    ALARM = 1;
  }
  else
  {
   ALARM = 0;   
  }
}

BLYNK_WRITE(V13) // Executes when the value of virtual pin 13 changes
{
  if(param.asInt() == 1)
  {
    WAIT = 1;
  }
  else
  {
   WAIT = 0;   
  }
}



// This function creates the timer object. It's part of Blynk library 
BlynkTimer timer; 

void myTimer() 
{
  if (ALARM == 1) {
      digitalWrite(RED, !digitalRead(RED));
      digitalWrite(ORANGE, HIGH);
      digitalWrite(GREEN, HIGH);
      digitalWrite(WHITE, HIGH);
      
    }
   
   
   else if (WARNING == 1) {
    digitalWrite(ORANGE, LOW);
    digitalWrite(GREEN, HIGH);
    digitalWrite(WHITE, HIGH);
    digitalWrite(RED, HIGH);
  }
  
  else if (WAIT == 1) {
      digitalWrite(WHITE, !digitalRead(WHITE));
      digitalWrite(ORANGE, HIGH);
      digitalWrite(GREEN, HIGH);
      digitalWrite(RED, HIGH);
      
    }
   
  
  else {
    digitalWrite(GREEN,! (OKAY == 1 && WARNING == 0 && ALARM == 0));
    digitalWrite(ORANGE,! (WARNING == 1 && OKAY == 0 && ALARM == 0));
    digitalWrite(RED, HIGH);
    digitalWrite(WHITE, HIGH);
  }      
}



void setup()
{
  //Connecting to Blynk Cloud

    Serial.begin(9600);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass); 
  
  // Setting interval to send data to Blynk Cloud to 1000ms. 
  // It means that data will be sent every second
  timer.setInterval(1000L, myTimer);
  pinMode(GREEN, OUTPUT);
  pinMode(ORANGE, OUTPUT);
  pinMode(RED, OUTPUT);
  pinMode(WHITE, OUTPUT); 
}

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

Thanks for the quick reply, I'll try to implement this code with the rest of my code!