WLAN-Car equipped with NodeMCU and 3 HC-SR04 Ultrasonic Sensors

Hi guys,

I will start of by describing the current project I am working on.

I build a car equipped with a NodeMCU V2 board and 3 ultrasonic sensors.
This car shall be controlled by the Blynk App.

I designed an interface with 5 buttons.
4 buttons are designated for: forwards/backwards/left-turn/right-turn
By pushing the 5th button the car should switch to an automatic mode. While in this Mode it does the following:

  1. Goes straight forwards until an obstacle appears.
  2. Reverse
  3. Check the left and right side for distances.
  4. Turn to the side with more free space and keep on driving.

The manual control of the car works just fine with the Blynk App.
The automatic mode works as well as long as I keep all the Blynk stuff out of it.
If I try to combine the two things the automatic mode just isn´t working anymore.

Maybe someone could tell me what to do. I am quite new to the programming world. :slight_smile:
If you find any unnecessary code that could be deleted please let me know.

Here is my code:

#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
#include <NewPing.h>

char auth[] = "XXX";
char ssid[] = "XXX";
char pass[] = "XXX";

#define MAX_DISTANCE 200 

//Left Sensor = Sensor_L
#define Trigger_Sensor_L 16
#define Echo_Sensor_L 13

//Middle Sensor = Sensor_M
#define Trigger_Sensor_M 12
#define Echo_Sensor_M 14

//Right Sensor = Sensor_R
#define Trigger_Sensor_R 16
#define Echo_Sensor_R 15

#define SONAR_NUM 3

//Right Motor = Motor_R
int Motor_R_Speed = 5;
int Motor_R_Direction = 2;

//Left Motor = Motor_L
int Motor_L_Speed = 4;
int Motor_L_Direction = 0;

NewPing sonar1(Trigger_Sensor_L, Echo_Sensor_L, MAX_DISTANCE);
NewPing sonar2(Trigger_Sensor_M, Echo_Sensor_M, MAX_DISTANCE);
NewPing sonar3(Trigger_Sensor_R, Echo_Sensor_R, MAX_DISTANCE);

unsigned int Sensor_L = 0;
unsigned int Sensor_M = 0;
unsigned int Sensor_R = 0;
int i = 0;
int Current_Distance_L = 0;
int Current_Distance_M = 0;
int Current_Distance_R = 0;

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(Motor_L_Speed, OUTPUT);
  pinMode(Motor_R_Speed, OUTPUT);
  pinMode(Motor_L_Direction, OUTPUT);
  pinMode(Motor_R_Direction, OUTPUT);
}

void straight()
{ analogWrite(Motor_L_Speed, 850);
  analogWrite(Motor_R_Speed, 850);
  digitalWrite(Motor_L_Direction, HIGH);
  digitalWrite(Motor_R_Direction, HIGH);
}

void backwards()
{ analogWrite(Motor_L_Speed, 1000);
  analogWrite(Motor_R_Speed, 1000);
  digitalWrite(Motor_L_Direction, LOW);
  digitalWrite(Motor_R_Direction, LOW);
}

void right()
{ analogWrite(Motor_L_Speed, 1000);
  analogWrite(Motor_R_Speed, 1000);
  digitalWrite(Motor_L_Direction, HIGH);
  digitalWrite(Motor_R_Direction, LOW);
}

void left()
{ analogWrite(Motor_L_Speed, 1000);
  analogWrite(Motor_R_Speed, 1000);
  digitalWrite(Motor_L_Direction, LOW);
  digitalWrite(Motor_R_Direction, HIGH);
}

void stopping()
{ analogWrite(Motor_L_Speed, 0);
  analogWrite(Motor_R_Speed, 0);
}

int readPing1() { 
  delay(40);
  unsigned int uS = sonar1.ping();
  int cm = uS / US_ROUNDTRIP_CM;
  return cm;
}
int readPing2() { 
  delay(40);
  unsigned int uS = sonar2.ping();
  int cm = uS / US_ROUNDTRIP_CM;
  return cm;
}
int readPing3() { 
  delay(40);
  unsigned int uS = sonar3.ping();
  int cm = uS / US_ROUNDTRIP_CM;
  return cm;
}

void find_path() {
  Current_Distance_L = readPing1();
  Current_Distance_R = readPing3();
  if (Current_Distance_L >= Current_Distance_R) {
    left();
    delay(500);
  } else {
    right();
    delay(500);
  }
}

void automatic() {
  Current_Distance_M = readPing2();
  straight();
  if (Current_Distance_M <= 9)
  { backwards();
    delay(500);
    find_path();
  }
}


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

BLYNK_WRITE(V0)
{
  if (param[0])
    straight();
  else
    stopping();
}

BLYNK_WRITE(V1)
{
  if (param[0])
    right();
  else
    stopping();
}

BLYNK_WRITE(V2)
{
  if (param[0])
    backwards();
  else
    stopping();
}

BLYNK_WRITE(V3)
{
  if (param[0])
    left();
  else
    stopping();
}

BLYNK_WRITE(V4) {
  if (param[0])
    automatic();
  else
    stopping();
}

Too much delays. That will wreak havoc with Blynk. Use BlinkTimer to execute timed things (see the PushData example).

2 Likes