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);
}