Using virtual ports data from app

Hi everyone I’m a new entusiast user of Blynk app, I trying to use the value that setup with a slider widget form blynk app reading the virtual port V1, I’m using the example code and I can print the value of the slider in the serial monitor and also I define a variable for calculate the ON time even can use an IF to switch on a led in the pin 2 but, what I want to do is that the value from slider sets the ON time the led and then the led switch to off, taking the OFF time from another slider from the app. Then I want that happens continuosly by all day. The led have to switch from ON to OFF allways, and the times ON and OFF had to be sets from the sliders from the app.
My problem is that setup the ON time with the slider, then led switch ON but the led never switch to OFF when the ON time run out. Then the app fails and the arduino too.

:frowning: Please help!!!

Code:
/*************************************************************

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************

  You can use this sketch as a debug tool that prints all incoming values
  sent by a widget connected to a Virtual Pin 1 in the Blynk App.

  App project setup:
    Slider widget (0...100) on V1
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "c046b3f28xxxxxxxxxd4d2cfb74d";

#define W5100_CS  10
#define SDCARD_CS 4

int WaveGen1=2;
int TWaveGen1=0;

// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin 1
BLYNK_WRITE(V1)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  // You can also use:
  // String i = param.asStr();
  // double d = param.asDouble();
  Serial.print("V1 Slider value is: ");
  Serial.println(pinValue);
  TWaveGen1=pinValue*1000;
  Serial.print("Tiempo de enc: ");
  Serial.println(TWaveGen1);
}
void setup()
{
  // Debug console
  Serial.begin(9600);

  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
  pinMode(WaveGen1,OUTPUT);

  Blynk.begin(auth);

}

void loop()
{
  Blynk.run();
  if (TWaveGen1>0){
    digitalWrite(WaveGen1,HIGH);
    delay(TWaveGen1);
    digitalWrite(WaveGen1,LOW);
  }
}

Guess you didn’t read all those directions you had to delete in order to put your question in this topic :wink:

I fixed your code formatting for you this time.


Meanwhile, you shouldn’t run processing loops in the void loop() use timers instead if needed.

And your if() processing should be in the BLYNK_WRITE() function itself, not separated out where the non-globally declared variables will be ignored.

https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/scope/

For example (I don’t think this code of yours will work?? Probably cause a heartbeat disconnection with the delay()… but this is the placement idea I am referring to)

BLYNK_WRITE(V0)
{   
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  // You can also use:
  // String i = param.asStr();
  // double d = param.asDouble();
  Serial.print("V1 Slider value is: ");
  Serial.println(pinValue);
  TWaveGen1=pinValue*1000;
  Serial.print("Tiempo de enc: ");
  Serial.println(TWaveGen1);

  if (TWaveGen1>0){
    digitalWrite(WaveGen1,HIGH);
    delay(TWaveGen1);
    digitalWrite(WaveGen1,LOW);
  }
}

@AdrianStivalet see PUSH_DATA for a proper Blynk loop().

Ok goint to that. Thanks

Hi Costas! can you help me to understand PUSH_DATA, in my point of view the PUSH_DATA it uses when I want to write from arduino to a virtual port, en my case I want the oposite, write from blynk to a virtual port then to arduino.

PUSH_DATA is an example to demonstrate how to use BlynkTimer (aka Arduino SimpleTimer) to call functions at sensibly timed intervals rather than thousands of times a second, like your loop().

Ahhh ok, so I can use the virtualWrite to use it in the direction that I need, using the BlynkTimer to set the time interval.
Thanks again