Good afternoon, I had an idea to make wifi controlled car and see what’s happening in my room while I’m not at home.
Everything works fine while the internet connection is stable, but when the connection is lost my code crashes.
the problem is that Arduino freezes when the connection is lost, all my sensors stop working, but the car continuous to ride. And this is the big problem because, in this case, in 80 % of cases my car bump into
wall or other things that stay on the floor.
my question is: what can I do to eliminate this freeze? And after 15 seconds code starts to work again, but very slow, and 15 seconds is a lot of time. Is it possible to reduce this interval?
Thanks for the help, and sorry for mistakes (if they are present)
And maybe some advice?
this is my code(if needed)
/*
Скетч к проекту "WiFi машинка на Arduino"
Автор: GUGO Technologies 2020
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
//char auth[] = "CEl-cpfVJcZ0OgdjNOvKFktWlUkvGMit";
char auth[] = "";
// Your WiFi credentials.
// Set password to "" for open networks.
//char ssid[] = "";
//char pass[] = "";
char ssid[] = "";
char pass[] = "";
//char ssid[] = "";
//char pass[] = "";
// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial3
// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX
// Your ESP8266 baud rate:
#define ESP8266_BAUD 38400
ESP8266 wifi(&EspSerial);
///motors///
#define MOTOR_MIN 0
int MOTOR_MAX; // максимальный сигнал на мотор (max 255)
#define JOY_MIN 0
#define JOY_MAX 1023
#define IN1 8
#define IN2 9 // IN2 обязательно должен быть ШИМ пином!!!
#define IN3 7
#define IN4 6 // IN4 обязательно должен быть ШИМ пином!!!
#include "GyverMotor.h"
GMotor motorR(IN1, IN2);
GMotor motorL(IN3, IN4);
int X, Y;
int Speed, Sides;
int signalX0, signalY0;
////avoidance////
#include "GyverFilters.h"
#include <NewPing.h>
#define MAX_DISTANCE 150 // Maximum distance we want to ping for (in centimetres). Maximum sensor distance is rated at 400-500cm.
NewPing sonar1(3, 2, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
NewPing sonar2(4, 5, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
GMedian3 filter1, filter2 ;
GFilterRA filter3, filter4;
int distance1, distance2;
unsigned long timer1;
boolean Flag1, Flag2, Flag3, Flag4, Flag5;
BLYNK_WRITE(V1) {
if (Flag4 == 0){
X = param[0].asInt();
Y = param[1].asInt();
}
}
BLYNK_WRITE(V2) {
int MAX = param.asInt();
MOTOR_MAX = MAX;
}
#define BLYNK_GREEN "#23C48E"
#define BLYNK_BLUE "#04C0F8"
#define BLYNK_YELLOW "#ED9D00"
#define BLYNK_RED "#D3435C"
#define BLYNK_DARK_BLUE "#5F7CD8"
WidgetLED led1(V3);
WidgetLED led2(V4);
BlynkTimer timer;
BLYNK_WRITE(V5) {
if (Flag4 == 1){
//acceleration force applied to axis x
int X0 = param[0].asFloat();
//acceleration force applied to axis y
int Y0 = param[1].asFloat();
Y0 = map((Y0), JOY_MIN, 5, MOTOR_MIN , 1023 ); // сигнал по У
X0 = map((X0), JOY_MIN, 5, MOTOR_MIN , 1023 );
Y = constrain(abs(Y0), MOTOR_MIN, 1023);
X = constrain(abs(X0), MOTOR_MIN, 1023);
if (X0 > 0) {
X = -X;
}
if (Y0 > 0) {
Y = -Y;
}
// Do something with x and y
//Serial.print("X = ");
//Serial.print(X);
//Serial.print("; Y = ");
//Serial.println(Y);
// Serial.print("; Z = ");
//Serial.println(z);
}
}
BLYNK_WRITE(V6) {
Flag4 = param.asInt();
}
void setup() {
Serial.begin(38400);
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(10);
//Blynk.begin(auth, wifi, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 80);
Blynk.begin(auth, wifi, ssid, pass, "", 8080);
////sonar////
filter3.setStep(5);
filter4.setStep(5);
/////led part//////
led1.on();
led2.on();
timer.setInterval(1000L, led);
}
void loop() {
Blynk.run();
Serial.print(Blynk.run());
avoidance();
motors();
//timer.run();
}
void avoidance(){
if (millis() - timer1 > 80) {
int a, c ;
byte b, d;
a = filter1.filtered(sonar1.ping_cm());
c = filter2.filtered(sonar2.ping_cm());
b = abs(sonar1.ping_cm() - a); // расчёт изменения с предыдущим
d = abs(sonar2.ping_cm() - c);
if (b > 1) filter3.setCoef(0.7); // если большое - резкий коэффициент
else filter3.setCoef(0.2);
if (d > 1) filter4.setCoef(0.7);
else filter4.setCoef(0.2);
distance1 = filter3.filteredTime(a);
distance2 = filter4.filteredTime(c);
Serial.print(distance1);
Serial.print(" ");
Serial.println(distance2);
timer1 = millis();
}
if (distance1 < 70 && distance1 > 40 && Y > 320 || distance1 < 70 && distance1 > 40 && Y < -320 || distance2 < 70 && distance2 > 40 && Y > 320 || distance2 < 70 && distance2 > 40 && Y < -320) Flag1 = 1;
else Flag1 = 0;
if (distance1 > 0 && distance1 <= 40 && Y > 320 || distance2 > 0 && distance2 <= 40 && Y < -320) Flag2 = 1;
else Flag2 = 0;
}
void led(){
if (distance1 > 70 ) led1.setColor(BLYNK_GREEN);
if (distance2 > 70 ) led2.setColor(BLYNK_GREEN);
if (distance1 <= 70 && distance1 > 40) led1.setColor(BLYNK_YELLOW);
if (distance2 <= 70 && distance2 > 40 ) led2.setColor(BLYNK_YELLOW);
if (distance1 <= 40 ) led1.setColor(BLYNK_RED);
if (distance2 <= 40 ) led2.setColor(BLYNK_RED);
Blynk.virtualWrite(V7,distance1);
Blynk.virtualWrite(V8,distance2);
}
void motors() {
if (X == 0 && Y == 0 || Flag2 == 1 || Flag5 == 0) { // если мы в "мёртвой" зоне
motorR.setMode(STOP); // не двигаемся
motorL.setMode(STOP);
Speed = 0;
Sides = Speed;
}
else {
signalY0 = map((Y), JOY_MIN, JOY_MAX, MOTOR_MIN , MOTOR_MAX ); // сигнал по У
signalX0 = map((X), JOY_MIN, JOY_MAX, MOTOR_MIN , MOTOR_MAX ); // сигнал по Х
if (signalY0 < 0) {
signalY0 = signalY0 * -1; //Конвертируем сигнал в положительный
}
if (signalX0 < 0) {
signalX0 = signalX0 * -1; //Конвертируем сигнал в положительный
}
if (signalY0 >= signalX0) {
Speed = signalY0 + (signalX0 / 2); //делим сигнал X сигнал по полам для достижения большей плавности
}
if (signalY0 <= signalX0) {
Speed = (signalY0 / 2) + signalX0; //делим сигнал Y сигнал по полам для достижения большей плавности
}
Speed = constrain(abs(Speed), 0, MOTOR_MAX);
Sides = Speed - signalX0;
if ( Y < 320 && Y > -320 ) {
Sides = -Speed; //поворот на 360 градусов
}
if (Y < -320 ) {
Speed = -Speed; // задний ход
Sides = -Sides;
}
if (X < 0) {
int swap = Speed; //переворот двух значений переменных для противоположной стороны
Speed = Sides;
Sides = swap;
}
// Serial.print(Speed);
//Serial.print(" ");
//Serial.println(Sides);
if (Speed > 0) motorL.setMode(FORWARD);
else motorL.setMode(BACKWARD);
//управление моторами
if (Sides > 0) motorR.setMode(FORWARD);
else motorR.setMode(BACKWARD);
Speed = constrain(abs(Speed), MOTOR_MIN, MOTOR_MAX);
Sides = constrain(abs(Sides), MOTOR_MIN, MOTOR_MAX);
}
if (Flag1 == 0) {
motorR.setSpeed(Sides); // задаем скорость
motorL.setSpeed(Speed);
}
if (Flag1 == 1) {
motorR.setSpeed(Sides / 2); // задаем скорость
motorL.setSpeed(Speed / 2);
}
}