Reading multi virtual pins

How can I read virtual pins such as ( v1 , v2 ,…) and drag thier values in a void loop ,

the idea is :
two leds represnts two IR sensors whitch they give ( 0 , 1 ) , and two switches releted to these sensors , if the switch 1 is On; the led 1 should trun off and keep traking led 2 if the object is there or no to show it in a Blynk as Off or ON .
But i have for loop as a timer inside BLYNK_WRITE(V1) , so this makes a delay to read a values of other virtual pins because of for loop, so how can i avoid this delay by reading pins togather , shall i use timer instead of for loops , or can i read these pins togather inside a void loop witch is error to write a function inside a function. ?

i know that following code is for reading V1 from Blynk :

BLYNK_WRITE(V1)
{
 int pinValue1 = param.asInt();
 Serial.println(pinValue1);
} ```

void setup {
....
}
void loop {
....
}

I keep reading your post… and honestly do not understand what you are asking…

With BLYNK_WRITE() functions

Whaaa?? Are you referring to using Global Variables?

You cannot have multiple void loops() in a sketch.

Honestly, it appears you are unsure of basic programming let alone basic Blynk.

I recommend you spend some time with the simple examples in the Sketch Builder… along with lots of reading of the Documentation and Help Center.

And please don’t pepper other topics (old or new) with your same questions.

can you make any use of this ?

I have a knowledge of C++. Then it was first time to ask here, you should be helpful instead of writing that strange comment dear gunner ! read the code below , the idea is very simple and I ask Blynkers !!

anyway, to make it easy to understand , ( in Blynk ) if i turn a switch ON which has virtual pin ( V1 ) it should be related to the LED 1 in Blynk " represnted sensor 1" which has ( V2 ) to make it OFF for a few seconds. so I wrote :

int detect1 = 5;              // sensor 1 
int detect2 = 2 ;            // sensor 2
int buttonState1 = 0;       // initial state to compare it with sensor 1 value
int buttonState2 = 0;

BLYNK_WRITE(V1){
 int pinValue1 = param.asInt();
 Serial.println(pinValue1);

     if (pinValue1 ){

Serial.println("Sensor1 is reserved");
Blynk.virtualWrite(V2, 0);
Blynk.virtualWrite(V1, 1);

  
  for (int i = 0; i < 8 ; i++) {
      buttonState1 = digitalRead(detect1);
      buttonState2 = digitalRead(detect2);
        if(buttonState1 == LOW ){
          Blynk.virtualWrite(V1, 0);
 i = 10;
return; 
} 
Serial.println(i);

if (buttonState2 == LOW) {
     Serial.print("sensor 2 has an object");
     Blynk.virtualWrite(V4, 0);   // V4 in Blynk for sensor 2

  } else {
      Serial.print(" sensor 2 has no object ");
      Blynk.virtualWrite(V4, 1);
 
  }
  
    delay(900);
  }
  
Blynk.virtualWrite(V1, 0);
Blynk.virtualWrite(V2, 1);
    
 }
 }  

I have many sensors that are related (to each other).

THE SIMPLE idea is to read V1 , V2 , etc, individually by using:
BLYNK_WRITE(V1) { .... }
BLYNK_WRITE(V2) { ....}

as you know this method takes a time to do what it has inside it because of for loop clear ! so the timer is needed here if it processes it very fast.
so how can I use fast and simple code instead of using for loops , i mean is there a timer ? can I call it when many conditions happen ? such as :

  if (pinValue1 ==1 && pinValue2 == 1 ){

Serial.println("Sensor 1 is reserved");
Serial.println("Sensor 2 is reserved");
Blynk.virtualWrite(V2, 0);
Blynk.virtualWrite(V4, 0);
Blynk.virtualWrite(V1, 1);
Blynk.virtualWrite(V3, 1);

I don’t read minds :stuck_out_tongue_winking_eye: or pamper strange posts.

I have done this as a hacked (slowed down) syncAll… no reason same principle can’t work for whatever strangeness you are doing, only using Blynk.virtualWrite(Vcount, data);

void BlynkSyncAllHack() { // Timed Lambda Function
  timer.setTimer(10L, []() {  // Run 127 times as per int at end of function
  Blynk.syncVirtual(Vcount);  // Generate a vPin sync for each count
  Vcount++;
}, 127);  // END Timer Function
}
2 Likes

Well, having read your first post, and this post, I have absolutely no idea what it is you’re trying to achieve.
Your code makes no sense to me whatsoever.

What is the purpose of the for loop? It simply does the same thing 8 times, but isn’t using the “i” integer variable at all in any of the processing within the loop.

Why are you doing Blynk.virtualWrite(V1 within the BLYNK_WRITE(V1) function (which is called automatically every time the value if V1 changes, or you do a Blynk.syncVirtual(V1))?

Why do you have a delay(900) in here?

Pete.

2 Likes
pinValue1 == 1
Blynk.virtualWrite(V2, 0);
Blynk.virtualWrite(V1, 1);

switch 1 (V1) is ( ON ) -------- LED 1 ( OFF ) assume it’s for booking this place which IR sensor covers by using switch.

then for loop as a ( timer ) to let 'LED1" ( V2 ) OFF as booked and keep tracking it for few seconds, if the object reached to the sensor 1 DURING these seconds , ( object will make the sensor’s value == 0 means that detect1 == LOW )
buttonState1 = digitalRead(detect1);
detect1 = LOW ; // NOW

if(buttonState1 == LOW ){
          Blynk.virtualWrite(V1, 0);

switch ( V1 ) should be OFF
Blynk.virtualWrite(V1, 0);
keep tracking sensor 2 if there is an object while sensor1 place has booked ( LED 2 will turn ON or OFF in blynk anytime if object is there )

don’t forget that you let sensor 1 running few seconds for booking, if the object is not there in its time , your place will be available to other object so
switch 1 (V1) is ( OFF ) -------- LED 1 ( ON ) :

Blynk.virtualWrite(V1, 0);
Blynk.virtualWrite(V2, 1);
    delay(900);

is for showing sensor 2 result slowly

I’m sure something is being lost in translation here, as I don’t understand any of your explanation. Are you using Google translate?

What does “booking” or “booked” mean in the context of this project?

You mention an IR sensor. Is this project something to do with an IR detector or transmitter? If so, where does this fit in to what you’re doing.

Using delays in Blynk functions isn’t the way to do things. They block code execution and will ultimately lead to Blynk disconnections. You should use a timeout timer instead.

Pete.

i don’t use translator :smile:

i am sorry if if i could not explain it clearly , i want to use switch to book a park or a place , this place covered by IR sensor to tell me if there is an object.

sensors in Blynk app represnted as LED and took ( V2 , V4 )
sitwches uses ( V1 , V2 )

i want to turn on the swtich 1 to book place 1
this means ( switch ON ) ( LED OFF ) for few seconds (( i want here to remove for loop that was controlling the time and booking place 1 ; and use another method as timer , or may calling a function that make switch ON and LED OFF for specific seconds and do it dirctly withoooout forgeting other parks or places of tracking ))

so i read a V1 , V2 individually by using :

BLYNK_WRITE(V1){
.. 
// for loop for  seconds forgeting switch 2 that takes (V2 )
 }

then after for loop finished it goes to void loop until you turn ON switch 2 that it reads it by

 BLYNK_WRITE(V2){ 
// for loop again
} 

i see this code was working but it’s stupid and this is my logic in C++ :no_mouth:
i need to use timer instead of for loop and (( if i turn ON any switches, code will take no time to run them BOTH and do timers togather ! ))

PLEASE I NEED A HELP HERE , and if there is another way to explain it by vidoes just give me a permission , this is a picture of an example :

Blynk%20example

Maybe you should put a video on YouTube then post a link to it here.

Pete.

I am sooo lost here :stuck_out_tongue:

I think I need to book off on this one… by my translation of “book”… AKA schedule off shift, AKA run away :stuck_out_tongue_winking_eye:

1 Like

I’m glad it’s not just me!

Pete.

well please check out this code that uses timer setTimer(long d, timer_callback f, int n) .

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


char auth[] = "..";
char ssid[] = "..";
char pass[] = "..";

int detect1 = 5;     // sensor 1 in nodemcu 
int detect2 = 2 ;    // sensor 2
int result1 = 0;     
int result2 = 0;
int buttonState1 = 0;        
int buttonState2 = 0;


BlynkTimer timer;   // this is in Timer example for Blynk ,do I need it ??
int Timer1;
int Timer2;


BLYNK_WRITE(V1){
 int pinValue1 = param.asInt();
 Serial.println(" Park 1 reserved "); 
   
 buttonState1 = digitalRead(detect1);  // if detect is low, it means car is there

 if ( pinValue1 == 1 ){
    Timer1 = timer.setTimer(1000L, park1 , 5); // does it mean 5 seconds ??
 }
// if that timer means 5 seconds i want the switch turns OFF after 5 seconds , it means that reserving a park for 5 seconds, please write a timer here 
// i guess it would be such as : 
     Timer2 = timer.setTimeout(5000, turnOFF);
}

void park1() {
Blynk.virtualWrite(V2, 0);    // LED in Blynk app is OFF
Blynk.virtualWrite(V1, 1);    // Button as Switch is ON

buttonState1 = digitalRead(detect1); // detect=0 means , car is there , he arrived to his park
  if ( buttonState1 == LOW ){
    Blynk.virtualWrite(V1, 0);  // turn Switch off
  }
}

void turnOFF () {
Blynk.virtualWrite(V1, 0); 
}

 BLYNK_WRITE(V2){
 int pinValue2 = param.asInt();
 Serial.println(" Park 2 reserved "); 
// I will use the same process of #V1 after solving Error compiling  
}

void setup()
{
  
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode (detect1 , INPUT );
  pinMode (detect2 , INPUT );

}

// this is running all the time to track parks

void loop()
{
   result1 = digitalRead(detect1);    // 0 means busy .... 1 means  Available
   result2 = digitalRead (detect2);
   
   if ( result1 == LOW )
   {
    Serial.println("PARK 1 BUSY");
    Blynk.virtualWrite(V2 , 0 );
   } else {
    Serial.println("PARK 1 Available");
    Blynk.virtualWrite(V2, 1);
   }

   if ( result2 == LOW )
   {
    Serial.println("PARK 2 BUSY");
    Blynk.virtualWrite(V4 , 0 );
   } else {
    Serial.println("PARK 2 Available");
    Blynk.virtualWrite(V4, 1);
   }

   Serial.println(".............................");
   delay(1000);   
   Blynk.run();
} 

I was using FOR LOOP as a timer inside BLYNK_WRITE(V1) , that means if ( switch 1 ) is used “ON” , iterations in for loop acts as timer.

suppose that i want to let ( switch 2 ) turns ON ( reserve Park 2) while (switch 1) is active too during its iterations, the problem was here , for loop takes its time and don’t look what is happning with other switches , so I want to replace for loop with a timer if there’s fast processing.

I got Error compiling :
blynk

" Error compiling for board NodeMCU 1.0 (ESP-12E Module). "

WHY?!

Just asking for connection issues. Read this…

http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/keep-your-void-loop-clean

And you are MISSING timer.run(); in the void loop() thus NO timers are working anyhow.

And the number 5 means how many times the timer will call it’s assigned function… thus park1 will run every second for a total of five times, then stop running until that timer command is called again.

1 Like

that’s right !
timerrun