Line follower catch data with blynk and stoped

hi, I need help with my project, currently, I was doing a line follower robot it work fine before i added the timer in blynk application to catch the data I needed. when I applied this code the robot will start reach when I press the button in phone blynk but there is an interval time for each reaction for example when the line follower detect need to turn right it will turn but in normal condition it will stop after the middle sensor detect the line i have out of idea thus is there a better idea to solve this

#define BLYNK_TEMPLATE_ID "--" //Template id
#define BLYNK_DEVICE_NAME "--" //device name
#define BLYNK_PRINT Serial

//wifi connection
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "--"; // Blynk Token
char ssid[] ="Wifi Name";
char pass[] ="Wifi Password ";

BlynkTimer timer;

//must use GPIO pin
#define RightMotorSpeed 5
#define RightMotorDir   0
#define LeftMotorSpeed  4
#define LeftMotorDir    2

//motor PWM control 100%-1023 75%-767 50% 511 25%-255
int minSpeed = 127;
int maxSpeed = 511;
int noSpeed = 0;

int LMS,RMS;
int error;

int getVal(){
  int val=map(analogRead(A0),15,775,0,255);
    return val; 
  }
  
BLYNK_WRITE(V0){
  int StartB = param.asInt(); // read button
  if (StartB == 1) {
    error=getVal();
    if (error>20 && error<=50)//sharp left
    {MturnL();RMS=noSpeed;LMS=maxSpeed;}
    else if (error>=50 && error<110)//left
    {MturnL();RMS=noSpeed;LMS=maxSpeed;}
    else if (error>=110 && error<140)//online
    {Mfoward();RMS=maxSpeed;LMS=maxSpeed;}
    else if (error>=140 && error<160 )//right
    {MturnR();RMS=minSpeed;LMS=minSpeed;}
    else if (error>=160 && error<245)//sharp right
    {MturnR();RMS=noSpeed;LMS=maxSpeed;}
    else if (error>=245){Mstop();}
    else{Mstop();RMS=noSpeed;LMS=noSpeed;}
  }
  else {
    Mstop();
  }
}

void setup() {
  pinMode(RightMotorSpeed, OUTPUT);
  pinMode(LeftMotorSpeed, OUTPUT);
  pinMode(RightMotorDir, OUTPUT);
  pinMode(LeftMotorDir, OUTPUT);

  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L,updateerror);
  
 /*/start up motor
  digitalWrite(RightMotorSpeed, LOW);
  digitalWrite(LeftMotorSpeed, LOW);
  digitalWrite(RightMotorDir, HIGH);
  digitalWrite(LeftMotorDir,HIGH);
*/
}  

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

void updateerror()
{
  Blynk.virtualWrite(V3,error);
  Blynk.virtualWrite(V2,RMS);
  Blynk.virtualWrite(V1,LMS);
}

void Mfoward()
  {
    digitalWrite(RightMotorDir,HIGH); 
    digitalWrite(LeftMotorDir,HIGH);
    analogWrite(RightMotorSpeed,maxSpeed);
    analogWrite(LeftMotorSpeed,maxSpeed);
  }
 
// move forward right
void MfowardR()
  {
    digitalWrite(RightMotorDir,HIGH);
    digitalWrite(LeftMotorDir,HIGH);
    analogWrite(RightMotorSpeed,minSpeed);
    analogWrite(LeftMotorSpeed,maxSpeed);
  }

// turn right
void MturnR()
  {
    digitalWrite(RightMotorDir,HIGH);
    digitalWrite(LeftMotorDir,HIGH);
    analogWrite(RightMotorSpeed,noSpeed);
    analogWrite(LeftMotorSpeed,maxSpeed);
  }

// move forward left
void MfowardL()
  {
    digitalWrite(RightMotorDir,HIGH);
    digitalWrite(LeftMotorDir,HIGH);
    analogWrite(RightMotorSpeed,maxSpeed);
    analogWrite(LeftMotorSpeed,minSpeed);
  }

void MturnL()
  {
    digitalWrite(RightMotorDir,HIGH);
    digitalWrite(LeftMotorDir,HIGH);
    analogWrite(RightMotorSpeed,maxSpeed);
    analogWrite(LeftMotorSpeed,noSpeed);
  }
void Mstop() // Stop
  {
    analogWrite(RightMotorSpeed,noSpeed);
    analogWrite(LeftMotorSpeed,noSpeed);
  }

@winterlim please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

1 Like

You seem to think that when the widget attached to pin V0 is placed in the On position, the BLYNK_WRITE(V0) callback function will continue to execute in a loop until the V0 widget is firmed Off.

This is wrong. BLYNK_WRITE(V0) will execute once, when the value of the widget attached to pin V0 changes.

You need to put the majority of the code that is in BLYNK_WRITE(V0) into a function, and call that function with a timer. You also need to use global variables in BLYNK_WRITE(V0) so that you know whether to suspend the processing of the timed loop, based on the status of the V0 widget.

Pete.

1 Like

sorry i will update the new code that i have write.

#define BLYNK_TEMPLATE_ID "--" //Template id
#define BLYNK_DEVICE_NAME "---" //device name
#define BLYNK_PRINT Serial

//wifi connection
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "---"; // Blynk Token
char ssid[] ="Wifi Name";
char pass[] ="Wifi Password ";

BlynkTimer timer;

//must use GPIO pin
#define RightMotorSpeed 5
#define RightMotorDir   0
#define LeftMotorSpeed  4
#define LeftMotorDir    2

//motor PWM control 100%-1023 75%-767 50% 511 25%-255
int minSpeed = 127;
int maxSpeed = 511;
int noSpeed = 0;

int LMS,RMS;
int error;
int StartB;

int getVal(){
  int val=map(analogRead(A0),15,775,0,255);
    return val; 
  }
  
BLYNK_WRITE(V0){
  StartB = param.asInt(); // read button
  if (StartB == 1) {
  robotstart();
  }
  else{}
}

void setup() {
  pinMode(RightMotorSpeed, OUTPUT);
  pinMode(LeftMotorSpeed, OUTPUT);
  pinMode(RightMotorDir, OUTPUT);
  pinMode(LeftMotorDir, OUTPUT);

  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L,updateerror);
 
}  

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

void updateerror()
{
  Blynk.virtualWrite(V3,error);
  Blynk.virtualWrite(V2,RMS);
  Blynk.virtualWrite(V1,LMS);
}

void robotstart()
{
     error=getVal();
    if (error>20 && error<=50)//sharp left
    {MturnL();RMS=noSpeed;LMS=maxSpeed;}
    else if (error>=50 && error<110)//left
    {MturnL();RMS=noSpeed;LMS=maxSpeed;}
    else if (error>=110 && error<140)//online
    {Mfoward();RMS=maxSpeed;LMS=maxSpeed;}
    else if (error>=140 && error<160 )//right
    {MturnR();RMS=minSpeed;LMS=minSpeed;}
    else if (error>=160 && error<245)//sharp right
    {MturnR();RMS=noSpeed;LMS=maxSpeed;}
    else if (error>=245){Mstop();}
    else{Mstop();RMS=noSpeed;LMS=noSpeed;}
}

void Mfoward()
  {
    digitalWrite(RightMotorDir,HIGH); 
    digitalWrite(LeftMotorDir,HIGH);
    analogWrite(RightMotorSpeed,maxSpeed);
    analogWrite(LeftMotorSpeed,maxSpeed);
  }
 
// move forward right
void MfowardR()
  {
    digitalWrite(RightMotorDir,HIGH);
    digitalWrite(LeftMotorDir,HIGH);
    analogWrite(RightMotorSpeed,minSpeed);
    analogWrite(LeftMotorSpeed,maxSpeed);
  }

// turn right
void MturnR()
  {
    digitalWrite(RightMotorDir,HIGH);
    digitalWrite(LeftMotorDir,HIGH);
    analogWrite(RightMotorSpeed,noSpeed);
    analogWrite(LeftMotorSpeed,maxSpeed);
  }

// move forward left
void MfowardL()
  {
    digitalWrite(RightMotorDir,HIGH);
    digitalWrite(LeftMotorDir,HIGH);
    analogWrite(RightMotorSpeed,maxSpeed);
    analogWrite(LeftMotorSpeed,minSpeed);
  }

void MturnL()
  {
    digitalWrite(RightMotorDir,HIGH);
    digitalWrite(LeftMotorDir,HIGH);
    analogWrite(RightMotorSpeed,maxSpeed);
    analogWrite(LeftMotorSpeed,noSpeed);
  }
void Mstop() // Stop
  {
    analogWrite(RightMotorSpeed,noSpeed);
    analogWrite(LeftMotorSpeed,noSpeed);
  }

Code snippets don’t really give useful information.

Pete.

actually for the problem that I currently face is using blynk to catch data without affecting the ongoing loop of the line follower robot. the start and stop function I write just for testing. sorry, I didn’t state my problem clearly. Is that possible to have the basic performance of the line follower robot going and at the same time uploading the sensor data of the robot into blynk?

I guess so, but it depends on how your line-follower is constructed, and what data you want to upload to Blynk and how frequently.
The worst case scenario may be that you need to use an ESP32 and multi-threading.

There are a number of line follower examples that were written for Blynk Legacy. Have you looked at those.

Pete.

my line follower already work fine without blynk code, thanks for the help as currently i plan to use another microcontroller to do the process so that it was separately unaffected by each other. i just thinking that is that possible the apply it in one microcontroller that why i ask

It rather depends on the microcontroller.

Pete.