Can BlynkTimer works at the same time?

Hello! and a very good day :smile:

I am using: Arduino mega, ESP8266 Wifi Shield, Ultrasonic Sensors, Line Sensors. The blynk app is on an iphone. Currently on the blynk server.

Here is the code I currently have. Without blynk the code works perfectly. however I would like to do some controlling over the robot which is why I opted for blynk. But, with blynk the sensors can’t be read simultaneously. Thus distoring the robot. May I know what can I do? Is there a blynk time function that can run the sensors simultaneously? Or would local server help?

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include "CytronMotorDriver.h"

// Auth Token in the Blynk App.
char auth[] = "5IBnOryca8MRxGRKl3Q5L-UIi150HjGe";

// WiFi credentials.
char ssid[] = "arisssa_2.4g@unifi";
char pass[] = "qistina2017";

// Software Serial
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(10, 11); // RX, TX

// ESP8266 baud rate:
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);

// BLYNK Timer
BlynkTimer timer;
int StartDoTimer;

//Pin declaration for SHIELD-2AMOTOR
CytronMD motor1(PWM_DIR, 5, 4);  // PWM 1 = Pin 3, DIR 1 = Pin 4.
CytronMD motor2(PWM_DIR, 6, 7); // PWM 2 = Pin 9, DIR 2 = Pin 10.

//Pin declaration for LSS05
#define LSS05_O1  A1 // Pin A1 is connected to O1 (LSS05)
#define LSS05_O2  A2 // Pin A2 is connected to O2 (LSS05)
#define LSS05_O3  A3 // Pin A3 is connected to O3 (LSS05)
#define LSS05_O4  A4 // Pin A4 is connected to O4 (LSS05)
#define LSS05_O5  A5 // Pin A5 is connected to O5 (LSS05)

// Ultrasonic Pin Declaration
#define Lechopin A14 // echo pin
#define Ltrigpin A15// Trigger pin
#define Mechopin A12 // echo pin
#define Mtrigpin A13// Trigger pin
#define Rechopin A10 // echo pin
#define Rtrigpin A11// Trigger pin
long duration, distance, RightSensor, BackSensor, FrontSensor, LeftSensor;
int setdist = 20;

// BLYNK system
int buttonA;
WidgetLCD lcd(V3);

void setup()
{
  // Debug Console
  Serial.begin (9600);
  EspSerial.begin(ESP8266_BAUD);
  Blynk.begin(auth, wifi, ssid, pass);

  // put your setup code here, to run once:
  //LSS05 Auto-Calibrating Line Sensor Pin Setup
  pinMode(LSS05_O1, INPUT); // Set O1 as input
  pinMode(LSS05_O2, INPUT); // Set O2 as input
  pinMode(LSS05_O3, INPUT); // Set O3 as input
  pinMode(LSS05_O4, INPUT); // Set O4 as input
  pinMode(LSS05_O5, INPUT); // Set O5 as input

  //Ultrasonic Pin Setup
  pinMode (Ltrigpin, OUTPUT);
  pinMode (Lechopin, INPUT);
  pinMode (Mtrigpin, OUTPUT);
  pinMode (Mechopin, INPUT);
  pinMode (Rtrigpin, OUTPUT);
  pinMode (Rechopin, INPUT);

  // Setup a function to be called every HALF second
  StartDoTimer = timer.setInterval(100L, System);
  timer.disable(StartDoTimer); // Turn off StartDoTimer
}

void System()
{
  int sensor = 0;
  sensor = LSS05Reading(); // Reading LSS05 sensor

  Linefollow();

  //Ultrasonic
  SonarSensor(Rtrigpin, Rechopin);
  RightSensor = distance;
  SonarSensor(Ltrigpin, Lechopin);
  LeftSensor = distance;
  SonarSensor(Mtrigpin, Mechopin);
  FrontSensor = distance;

  Serial.print(LeftSensor);
  Serial.print(" - ");
  Serial.print(FrontSensor);
  Serial.print(" - ");
  Serial.println(RightSensor);

  Blynk.virtualWrite(V0, FrontSensor);
  Blynk.virtualWrite(V1, LeftSensor);
  Blynk.virtualWrite(V2, RightSensor);
}

int LSS05Reading()
{
  int LSS05Data = 0;

  LSS05Data = digitalRead(LSS05_O5) +
              (digitalRead(LSS05_O4) * 2) +
              (digitalRead(LSS05_O3) * 4) +
              (digitalRead(LSS05_O2) * 8) +
              (digitalRead(LSS05_O1) * 16);

  Serial.print("Sensor reading: ");
  if (LSS05Data < 0b00010) {
    Serial.print("0000");
  }
  else if (LSS05Data < 0b00100) {
    Serial.print("000");
  }
  else if (LSS05Data < 0b01000) {
    Serial.print("00");
  }
  else if (LSS05Data < 0b10000) {
    Serial.print("0");
  }
  Serial.println(LSS05Data, BIN);

  return LSS05Data;
}

void Linefollow()
{
  int sensor = LSS05Reading();

  if (sensor == 0b01110 ||
      sensor == 0b00100)
  {
    motor1.setSpeed(60);   // Motor 1 runs forward at 50% speed.
    motor2.setSpeed(60);  // Motor 2 runs backward at 50% speed.
  }
  else if (sensor == 0b01100)
  {
    motor1.setSpeed(60);   // Motor 1 runs forward at 50% speed.
    motor2.setSpeed(40);  // Motor 2 runs backward at 50% speed.
  }
  else if (sensor == 0b01000)
  {
    motor1.setSpeed(60);   // Motor 1 runs forward at 50% speed.
    motor2.setSpeed(30);  // Motor 2 runs backward at 50% speed.
  }
  else if (sensor == 0b10000 ||
           sensor == 0b11000 ||
           sensor == 0b11100 ||
           sensor == 0b11110)
  {
    motor1.setSpeed(60);   // Motor 1 runs forward at 50% speed.
    motor2.setSpeed(0);  // Motor 2 runs backward at 50% speed.
  }

  else if (sensor == 0b00110)
  {
    motor1.setSpeed(40);   // Motor 1 runs forward at 50% speed.
    motor2.setSpeed(60);  // Motor 2 runs backward at 50% speed.
  }
  else if (sensor == 0b00010)
  {
    motor1.setSpeed(30);   // Motor 1 runs forward at 50% speed.
    motor2.setSpeed(60);  // Motor 2 runs backward at 50% speed.
  }
  else if (sensor == 0b00001 ||
           sensor == 0b00011 ||
           sensor == 0b00111 ||
           sensor == 0b01111)
  {
    motor1.setSpeed(0);   // Motor 1 runs forward at 50% speed.
    motor2.setSpeed(60);  // Motor 2 runs backward at 50% speed.
  }
}

void SonarSensor(int trigPin, int echoPin)
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
}

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

BLYNK_WRITE(V8) // Button A = Location A
{
  int buttonA = param.asInt(); // read button
  if (param.asInt() == 1)
  {
    lcd.clear();
    lcd.print(4, 0, "Heading to");
    lcd.print(4, 1, "Location A");
    Serial.println("V1 is ON");
    timer.enable(StartDoTimer);
  }
  else if (param.asInt() == 0)
  {
    Serial.println("V1 is OFF");
    timer.disable(StartDoTimer);

    motor1.setSpeed(0);   // Motor 1 runs forward at 50% speed.
    motor2.setSpeed(0);  // Motor 2 runs backward at 50% speed.
  }
}

With or without Blynk, the Arduino (and most other MCUs) can’t read two digital pins simultaneously. The hardware only has a single threaded processor, and can’t perform simultaneous tasks.
It is capable of performing one task quite quickly after another, and this doesn’t change when Blynk is added to the equation.

One limitation of Blynk is the frequency of virtualWrite operations. At the moment, your code is doing three virtualWrites per loop of the System function, and when the timer is enabled this is being called 10 times per second - giving 30 virtualWrite attempts per second.
The practical limit is 10 virtualWrites per second, so you are attempting to flood the Blynk server with data.

Assuming that you aren’t running an old version of the Blynk library, the BlynkTimer will intervene in this process and prevent the server from being flooded in this way - which may be causing some of the symptoms that you are experiencing.

However, it’s difficult to tell because of the lack of specific information provided, and because you haven’t shared your non-Blynk code.

Pete.

Oh my thank you for replying so fast! This is my code without blynk. Which is supposed to follow line and then if it spots an obstacle it will avoid it.

#include <ESP8266_Lib.h>
#include "CytronMotorDriver.h"

// Software Serial
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(10, 11); // RX, TX

// ESP8266 baud rate:
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);

//Pin declaration for SHIELD-2AMOTOR
CytronMD motor1(PWM_DIR, 5, 4);  // PWM 1 = Pin 3, DIR 1 = Pin 4.
CytronMD motor2(PWM_DIR, 6, 7); // PWM 2 = Pin 9, DIR 2 = Pin 10.

//Pin declaration for LSS05
int LeftSen = A1;
int LeftMSen = A2;
int MidSen = A3;
int RightMSen = A4;
int RightSen = A5;

// Ultrasonic Pin Declaration
#define Lechopin A14 // echo pin
#define Ltrigpin A15// Trigger pin
#define Mechopin A12 // echo pin
#define Mtrigpin A13// Trigger pin
#define Rechopin A10 // echo pin
#define Rtrigpin A11// Trigger pin
long duration, distance, RightSensor, BackSensor, FrontSensor, LeftSensor;
int setdist = 20;

void setup()
{
  // put your setup code here, to run once:
  // Debug Console
  Serial.begin (9600);
  EspSerial.begin(ESP8266_BAUD);

  // put your setup code here, to run once:
  //LSS05 Auto-Calibrating Line Sensor Pin Setup
  pinMode(LeftSen, INPUT);
  pinMode(LeftMSen, INPUT);
  pinMode(MidSen, INPUT);
  pinMode(RightMSen, INPUT);
  pinMode(RightSen, INPUT);

  //Ultrasonic Pin Setup
  pinMode (Ltrigpin, OUTPUT);
  pinMode (Lechopin, INPUT);
  pinMode (Mtrigpin, OUTPUT);
  pinMode (Mechopin, INPUT);
  pinMode (Rtrigpin, OUTPUT);
  pinMode (Rechopin, INPUT);
}

int LSS05Reading()
{
  int LSS05Data = 0;

  LSS05Data = digitalRead(LSS05_O5) +
              (digitalRead(LSS05_O4) * 2) +
              (digitalRead(LSS05_O3) * 4) +
              (digitalRead(LSS05_O2) * 8) +
              (digitalRead(LSS05_O1) * 16);

  Serial.print("Sensor reading: ");
  if (LSS05Data < 0b00010) {
    Serial.print("0000");
  }
  else if (LSS05Data < 0b00100) {
    Serial.print("000");
  }
  else if (LSS05Data < 0b01000) {
    Serial.print("00");
  }
  else if (LSS05Data < 0b10000) {
    Serial.print("0");
  }
  Serial.println(LSS05Data, BIN);

  return LSS05Data;
}

void Linefollow()
{
  int sensor = LSS05Reading();

  if (sensor == 0b01110 ||
      sensor == 0b00100)
  {
    motor1.setSpeed(60);   // 
    motor2.setSpeed(60);  //
  }
  else if (sensor == 0b01100)
  {
    motor1.setSpeed(60);   // 
    motor2.setSpeed(30);  //.
  }
  else if (sensor == 0b01000)
  {
    motor1.setSpeed(60);   // 
    motor2.setSpeed(25);  // 
  }
  else if (sensor == 0b10000 ||
           sensor == 0b11000 ||
           sensor == 0b11100 ||
           sensor == 0b11110)
  {
    motor1.setSpeed(60);   // 
    motor2.setSpeed(0);  // 
  }

  else if (sensor == 0b00110)
  {
    motor1.setSpeed(30);   // 
    motor2.setSpeed(60);  // 
  }
  else if (sensor == 0b00010)
  {
    motor1.setSpeed(25);   // 
    motor2.setSpeed(60);  // 
  }
  else if (sensor == 0b00001 ||
           sensor == 0b00011 ||
           sensor == 0b00111 ||
           sensor == 0b01111)
  {
    motor1.setSpeed(0);   // .
    motor2.setSpeed(60);  // 
  }
}

void SonarSensor(int trigPin, int echoPin)
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
}

void loop()
{
  SonarSensor(Rtrigpin, Rechopin);
  RightSensor = distance;
  SonarSensor(Ltrigpin, Lechopin);
  LeftSensor = distance;
  SonarSensor(Mtrigpin, Mechopin);
  FrontSensor = distance;

  Serial.print(LeftSensor);
  Serial.print(" - ");
  Serial.print(FrontSensor);
  Serial.print(" - ");
  Serial.println(RightSensor);

  if (FrontSensor > setdist)
  {
    Linefollow();
  }
  else if (FrontSensor < setdist && RightSensor > setdist && LeftSensor > setdist)
  {
    Stopp();
    delay(200);
    Left();
    delay(600);
    Forward();
    delay(200);
    Linefollow();
  }
  else if (FrontSensor < setdist && RightSensor < setdist && LeftSensor > setdist)
  {
    Stopp();
    delay(200);
    Left();
    delay(600);
    Forward();
    delay(300);
    Linefollow();
  }
  else if (FrontSensor < setdist && RightSensor > setdist && LeftSensor < setdist)
  {
    Stopp();
    delay(200);
    Right();
    delay(600);
    Forward();
    delay(300);
    Linefollow();
  }
  else if (FrontSensor < setdist && RightSensor < setdist && LeftSensor < setdist)
  {
    Stopp();
    delay(300);
    Backward();
    delay(1000);
    Right();
    delay(700);
    Forward();
    delay(400);
    Linefollow();
  }
}

void Forward()
{
  motor1.setSpeed(60);   
  motor2.setSpeed(60);  
}

void Left()
{
  motor1.setSpeed(60);   
  motor2.setSpeed(0);  
}

void Right()
{
  motor1.setSpeed(0);   
  motor2.setSpeed(60);  
}

void Backward()
{
  motor1.setSpeed(-60);  
  motor2.setSpeed(-60);  
}

void Stopp()
{
  motor1.setSpeed(0);   
  motor2.setSpeed(0);  
}

I took your advice regarding the VirtualWrite operations and eliminated them which work with the code before. However, when I add in the avoding obstacle patterns it just go crazy again. My guess is im flooding blynk again :upside_down_face: Would adding another timer help?

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include "CytronMotorDriver.h"

// Auth Token in the Blynk App.
char auth[] = "5IBnOryca8MRxGRKl3Q5L-UIi150HjGe";

// WiFi credentials.
char ssid[] = "arisssa_2.4g@unifi";
char pass[] = "qistina2017";

// Software Serial
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(10, 11); // RX, TX

// ESP8266 baud rate:
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);

// BLYNK Timer
BlynkTimer timer;
int StartDoTimer;
int Sendsensor;

//Pin declaration for SHIELD-2AMOTOR
CytronMD motor1(PWM_DIR, 5, 4);  // PWM 1 = Pin 3, DIR 1 = Pin 4.
CytronMD motor2(PWM_DIR, 6, 7); // PWM 2 = Pin 9, DIR 2 = Pin 10.

//Pin declaration for LSS05
#define LSS05_O1  A1 // Pin A1 is connected to O1 (LSS05)
#define LSS05_O2  A2 // Pin A2 is connected to O2 (LSS05)
#define LSS05_O3  A3 // Pin A3 is connected to O3 (LSS05)
#define LSS05_O4  A4 // Pin A4 is connected to O4 (LSS05)
#define LSS05_O5  A5 // Pin A5 is connected to O5 (LSS05)

// Ultrasonic Pin Declaration
#define Lechopin A14 // echo pin
#define Ltrigpin A15// Trigger pin
#define Mechopin A12 // echo pin
#define Mtrigpin A13// Trigger pin
#define Rechopin A10 // echo pin
#define Rtrigpin A11// Trigger pin
long duration, distance, RightSensor, BackSensor, FrontSensor, LeftSensor;
int setdist = 20;

// BLYNK system
int buttonA;
WidgetLCD lcd(V3);

void setup()
{
  // Debug Console
  Serial.begin (9600);
  EspSerial.begin(ESP8266_BAUD);
  Blynk.begin(auth, wifi, ssid, pass);

  // put your setup code here, to run once:
  //LSS05 Auto-Calibrating Line Sensor Pin Setup
  pinMode(LSS05_O1, INPUT); // Set O1 as input
  pinMode(LSS05_O2, INPUT); // Set O2 as input
  pinMode(LSS05_O3, INPUT); // Set O3 as input
  pinMode(LSS05_O4, INPUT); // Set O4 as input
  pinMode(LSS05_O5, INPUT); // Set O5 as input

  //Ultrasonic Pin Setup
  pinMode (Ltrigpin, OUTPUT);
  pinMode (Lechopin, INPUT);
  pinMode (Mtrigpin, OUTPUT);
  pinMode (Mechopin, INPUT);
  pinMode (Rtrigpin, OUTPUT);
  pinMode (Rechopin, INPUT);

  // Setup a function to be called every HALF second
  StartDoTimer = timer.setInterval(100L, System);
  timer.disable(StartDoTimer); // Turn off StartDoTimer
}

void System()
{
  int sensor = 0;
  sensor = LSS05Reading(); // Reading LSS05 sensor

  //Ultrasonic
  SonarSensor(Rtrigpin, Rechopin);
  RightSensor = distance;
  SonarSensor(Ltrigpin, Lechopin);
  LeftSensor = distance;
  SonarSensor(Mtrigpin, Mechopin);
  FrontSensor = distance;

  Serial.print(LeftSensor);
  Serial.print(" - ");
  Serial.print(FrontSensor);
  Serial.print(" - ");
  Serial.println(RightSensor);

  if (FrontSensor > setdist)
  {
    Linefollow();
  }
  else if (FrontSensor < setdist && RightSensor > setdist && LeftSensor > setdist)
  {
    Stopp();
    delay(200);
    Left();
    delay(600);
    Forward();
    delay(200);
    Linefollow();
  }
  else if (FrontSensor < setdist && RightSensor < setdist && LeftSensor > setdist)
  {
    Stopp();
    delay(200);
    Left();
    delay(600);
    Forward();
    delay(300);
    Linefollow();
  }
  else if (FrontSensor < setdist && RightSensor > setdist && LeftSensor < setdist)
  {
    Stopp();
    delay(200);
    Right();
    delay(600);
    Forward();
    delay(300);
    Linefollow();
  }
  else if (FrontSensor < setdist && RightSensor < setdist && LeftSensor < setdist)
  {
    Stopp();
    delay(300);
    Backward();
    delay(1000);
    Right();
    delay(700);
    Forward();
    delay(400);
    Linefollow();
  }
}

int LSS05Reading()
{
  int LSS05Data = 0;

  LSS05Data = digitalRead(LSS05_O5) +
              (digitalRead(LSS05_O4) * 2) +
              (digitalRead(LSS05_O3) * 4) +
              (digitalRead(LSS05_O2) * 8) +
              (digitalRead(LSS05_O1) * 16);

  Serial.print("Sensor reading: ");
  if (LSS05Data < 0b00010) {
    Serial.print("0000");
  }
  else if (LSS05Data < 0b00100) {
    Serial.print("000");
  }
  else if (LSS05Data < 0b01000) {
    Serial.print("00");
  }
  else if (LSS05Data < 0b10000) {
    Serial.print("0");
  }
  Serial.println(LSS05Data, BIN);

  return LSS05Data;
}

void Linefollow()
{
  int sensor = LSS05Reading();

  if (sensor == 0b01110 ||
      sensor == 0b00100)
  {
    motor1.setSpeed(60);   // Motor 1 runs forward at 50% speed.
    motor2.setSpeed(60);  // Motor 2 runs backward at 50% speed.
  }
  else if (sensor == 0b01100)
  {
    motor1.setSpeed(60);   // Motor 1 runs forward at 50% speed.
    motor2.setSpeed(30);  // Motor 2 runs backward at 50% speed.
  }
  else if (sensor == 0b01000)
  {
    motor1.setSpeed(60);   // Motor 1 runs forward at 50% speed.
    motor2.setSpeed(25);  // Motor 2 runs backward at 50% speed.
  }
  else if (sensor == 0b10000 ||
           sensor == 0b11000 ||
           sensor == 0b11100 ||
           sensor == 0b11110)
  {
    motor1.setSpeed(60);   // Motor 1 runs forward at 50% speed.
    motor2.setSpeed(0);  // Motor 2 runs backward at 50% speed.
  }

  else if (sensor == 0b00110)
  {
    motor1.setSpeed(30);   // Motor 1 runs forward at 50% speed.
    motor2.setSpeed(60);  // Motor 2 runs backward at 50% speed.
  }
  else if (sensor == 0b00010)
  {
    motor1.setSpeed(25);   // Motor 1 runs forward at 50% speed.
    motor2.setSpeed(60);  // Motor 2 runs backward at 50% speed.
  }
  else if (sensor == 0b00001 ||
           sensor == 0b00011 ||
           sensor == 0b00111 ||
           sensor == 0b01111)
  {
    motor1.setSpeed(0);   // Motor 1 runs forward at 50% speed.
    motor2.setSpeed(60);  // Motor 2 runs backward at 50% speed.
  }
}

void SonarSensor(int trigPin, int echoPin)
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
}

void Forward()
{
  motor1.setSpeed(60);   // Motor 1 runs forward at 50% speed.
  motor2.setSpeed(60);  // Motor 2 runs backward at 50% speed.
}

void Left()
{
  motor1.setSpeed(60);   // Motor 1 runs forward at 50% speed.
  motor2.setSpeed(0);  // Motor 2 runs backward at 50% speed.
}

void Right()
{
  motor1.setSpeed(0);   // Motor 1 runs forward at 50% speed.
  motor2.setSpeed(60);  // Motor 2 runs backward at 50% speed.
}

void Backward()
{
  motor1.setSpeed(-60);   // Motor 1 runs forward at 50% speed.
  motor2.setSpeed(-60);  // Motor 2 runs backward at 50% speed.
}

void Stopp()
{
  motor1.setSpeed(0);   // Motor 1 runs forward at 50% speed.
  motor2.setSpeed(0);  // Motor 2 runs backward at 50% speed.
}

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

BLYNK_WRITE(V8) // Button A = Location A
{
  int buttonA = param.asInt(); // read button
  if (param.asInt() == 1)
  {
    lcd.clear();
    lcd.print(4, 0, "Heading to");
    lcd.print(4, 1, "Location A");
    Serial.println("V1 is ON");
    timer.enable(StartDoTimer);
  }
  else if (param.asInt() == 0)
  {
    Serial.println("V1 is OFF");
    timer.disable(StartDoTimer);

    motor1.setSpeed(0);   //
    motor2.setSpeed(0);  //
  }
}

Thank you so so much for your help!

Sarah

Are you saying that the non-Blynk code doesn’t do what it’s supposed to?

Pete.

Ohh no no, the non blynk code works fine, and now also the Blynk one :smile: Thanks to you! for clarifying on the virtualWrite limitations & better understanding of how the BlynkTimer works. Thank you so much!