Hi there,
Besides the Blynk.run(), I have an other program void xxx() that i want to run in the void loop
How can i not run the Blynk.run() & turn into running the void xxx()
How about this way:
Well, aside form the fact that you are running the function xxx() the wrong way, and thousands of times a second, which may cause Blynk connection issues…
That way would (once corrected - you don’t use void in front of the function while in the void loop() ) run both Blynk.run(); and xxx(); unless the device gets disconnected from the server then only runs the xxx();
I need to control a small car. It has two status: automatic run (avoid obstacles) and remote control via Blynk.
And I want to do something so that the two functions take turn to run. If it receive the signal from the blynk server, it will run under the controller. If not, then it will automatically run. Something like that. Do you have any methodologies to link those two functions together? Thank you so much, sir
There is no magic button that will switch between “Auto” or Manual" It will all come down to how well you can program.
YOu can search this forum for others who have used Blynk to control various vehicles, use keywords, like Car, Rover, RC, Vehicle, etc. As far as the auto navigation mode, well… search Google for that.
You can do a variety of things… make it full automatic and use blynk_write as interrupts for make the rc moves respond to blynk controls… and after a timeout of some seconds without get more blynk writes… come back to the automatic mode… or put a simply button at blynk interface to control manual and automatic mode… be creative
I’d suggest you start with an Arduino Programming 101 course so that you can grasp the basics. You clearly have a lot of enthusiasm, but if you don’t know how to use the available tools then you’ll probably become disillusioned and walk away - which would be a pity.
From your description of the project, and the code you’ve posted, the project is la long way from being completed and you don’t have the skills to do that at the moment. You also haven’t taken on-board @Gunner’s remarks about the void loop running thousands of times per second. This is basic stuff that you need to learn, and this forum isn’t really the place for that.
You’re unlikely to find people on this forum who will write your code for you, but we will help you with Blynk related problems when you’ve learned to create your own working code.
Describe me in a pm your project, the board you use, the sensors and anything else that you’ll need to use. And I try to help you to do it, but keep in mind that I said help you to do it not help you doing it for you.
Hi, as i replied you at pm… i don’t have your hardware and libraries… i make this sketck without check it… try it and say me if is ok or not… and if is not… what is doing wrong ^w^
This skecth will be in autorun mode all time but when you push the widget at blynk app the program automatically will be configured at blynk mode… after a timeout of 2 seconds without blynk interactuation will return to autorun mode automatically one more time.
#include <Arduino.h>
#include <AFMotor.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <Tasker.h> //https://github.com/joysfera/arduino-tasker
#define BLYNK_PRINT Serial
#define EspSerial Serial1
#define ESP8266_BAUD 115200
#define Blynk_TimeOut 2000
#define MotorSpeed 95
#define MotorLowSpeed 50
#define trigLeft 23
#define echoLeft 25
#define trigMid 27
#define echoMid 29
#define trigRight 31
#define echoRight 33
#define DIST 25.0
#define MotorType MOTOR12_64KHZ
char auth[] = "*****";
char ssid[] = "Office1";
char pass[] = "12345678";
float distLeft,distRight,distMid;
AF_DCMotor motor1(1, MotorType);
AF_DCMotor motor2(2, MotorType);
AF_DCMotor motor3(3, MotorType);
AF_DCMotor motor4(4, MotorType);
Tasker Task(true);
ESP8266 wifi(&EspSerial);
double GetDist(int val_int,int val_out)
{
digitalWrite(val_out,LOW);
delayMicroseconds(2);
digitalWrite(val_out,HIGH);
delayMicroseconds(10);
digitalWrite(val_out,LOW);
return double(pulseIn(val_int,HIGH)/58.00);
}
int CheckDist(double value, int count)
{
return value<=DIST*count;
}
void motorForward()
{
Serial.println("motorForward");
motor1.setSpeed(MotorSpeed);
motor1.run(FORWARD);
motor2.setSpeed(MotorSpeed);
motor2.run(FORWARD);
motor3.setSpeed(MotorSpeed);
motor3.run(FORWARD);
motor4.setSpeed(MotorSpeed);
motor4.run(FORWARD);
}
void motorBackward()
{
Serial.println("motorBackward");
motor1.setSpeed(MotorSpeed);
motor1.run(BACKWARD);
motor2.setSpeed(MotorSpeed);
motor2.run(BACKWARD);
motor3.setSpeed(MotorSpeed);
motor3.run(BACKWARD);
motor4.setSpeed(MotorSpeed);
motor4.run(BACKWARD);
}
void motorTurnLeft()
{
Serial.println("motorTurnLeft");
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.setSpeed(MotorSpeed);
motor4.setSpeed(MotorSpeed);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
void motorTurnRight()
{
Serial.println("motorTurnRight");
motor1.setSpeed(MotorSpeed);
motor2.setSpeed(MotorSpeed);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void motorLeftRotate()
{
Serial.println("motorLeftRotate");
motor1.setSpeed(MotorLowSpeed);
motor1.run(BACKWARD);
motor2.setSpeed(MotorLowSpeed);
motor2.run(BACKWARD);
motor3.setSpeed(MotorSpeed);
motor3.run(FORWARD);
motor4.setSpeed(MotorSpeed);
motor4.run(FORWARD);
}
void motorRightRotate()
{
Serial.println("motorRightRotate");
motor1.setSpeed(MotorSpeed);
motor1.run(FORWARD);
motor2.setSpeed(MotorSpeed);
motor2.run(FORWARD);
motor3.setSpeed(MotorLowSpeed);
motor3.run(BACKWARD);
motor4.setSpeed(MotorLowSpeed);
motor4.run(BACKWARD);
}
void motorStop()
{
Serial.println("motorStop");
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
BLYNK_WRITE(V1)
{
Task.cancel(AutoRun); // Cancel autorun mode
Task.setInterval(AutoRun,Blynk_TimeOut); //shedule autorun after Blynk_TimeOut/1000 seconds of blynk inactivity
int x = param[0].asInt();
int y = param[1].asInt();
if (x==y) (!x)?motorStop():(x<0)?motorLeftBack():motorTurnRight();
else if(x>y) (!x)?motorBackward():(!y)?motorTurnRight():motorRightBack();
else (x<0)?motorTurnLeft():motorForward();
}
/*
Explanation:
-----------------
if (x==y) //**(!x) mean x==0 because 0==false and !false==true**
{ // x==0 y==0 // x==-1 y==-1 //x=1= y==1
(!x)?motorStop():(x<0)?motorLeftBack():motorTurnRight();
}
else if(x>y)
{ //x==0 y==-1 // **x==1 y==0** //x==1 y==-1 **(!x) was false mean x!=0 and x must be >y then x==1
(!x)?motorBackward():(!y)?motorTurnRight():motorRightBack();
}
else
{
(x<0)?motorTurnLeft():motorForward();
}
-----------------
Due to:
if(x== 0 && y== 0) motorstop();
if(x==-1 && y==-1) motorLeftBack();
if(x== 1 && y== 1) motorTurnRight();
if(x== 0 && y==-1) motorBackward();
if(x== 1 && y== 0) motorTurnRight();
if(x== 1 && y==-1) motorRightBack();
if(x==-1 && y== 0) motorTurnLeft();
if(x==-1 && y== 1) motorTurnLeft();
if(x== 0 && y== 1) motorForward();
*/
void AutoRun()
{
Task.setInterval(AutoRun,500);// ensure that autorun is sheduled every 500ms
double distLeft, distRight, distMid;
int left=0, right=0, mid=0, state=0;
distLeft=GetDist(echoLeft,trigLeft);
left=CheckDist(distLeft,2);
distRight=GetDist(echoRight,trigRight);
right=CheckDist(distRight,2);
distMid=GetDist(echoMid,trigMid);
mid=CheckDist(distMid,4);
state = 2*left + 4*mid + right;
switch (state)
{
case 4:
(distLeft >= distRight)?motorTurnLeft():motorTurnRight();
break;
case 5:
motorBackward();
delay(500); // you must avoid the use of delay... for the 1st version of the sketch i leave it... but it can be done without any delay at all...
motorTurnLeft();
break;
case 6:
motorBackward();
delay(500);
motorTurnRight();
break
case 7:
motorBackward();
delay(500);
(distLeft >= distRight)?motorLeftRotate():motorRightRotate();
break;
default:
motorForward();
break;
}
}
void setup()
{
Serial.begin(115200);
EspSerial.begin(ESP8266_BAUD);
Blynk.begin(auth, wifi, ssid, pass);
pinMode(trigLeft,OUTPUT);
pinMode(echoLeft,INPUT);
pinMode(trigRight,OUTPUT);
pinMode(echoRight,INPUT);
pinMode(trigMid,OUTPUT);
pinMode(echoMid,INPUT);
Task.setInterval(AutoRun,500);
}
void loop()
{
Blynk.run();
Task.loop();
}