HC-SR04 coding?!!

Hi! I´m playing around with a pair of DC motors, a L293D chip and three HC-SR04 sensors - ie a robot car!!

First I used the more traditional code for the HC-SR04 sensors - which worked as I wanted with ok accuracy.

const int leftForward = 2;
const int leftBackward = 3;
const int rightForward = 4;
const int rightBackward = 5;

const int rightPWM = 10;
const int leftPWM = 6;

const int trigPin = 12;
const int echoPin = 11;
long duration; 
int distance;

const int trigPin2 = 13;
const int echoPin2 = 9;
long duration2; 
int distance2;

const int trigPin3 = 8;
const int echoPin3 = 7;
long duration3; 
int distance3;

bool lastTurn = false;
                
void setup() 
  {
  pinMode(leftForward , OUTPUT);
  pinMode(leftBackward , OUTPUT);
  pinMode(rightForward , OUTPUT);
  pinMode(rightBackward , OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
  pinMode(rightPWM, OUTPUT);
  pinMode(leftPWM, OUTPUT);
  }
void loop() 
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);  
  duration = pulseIn(echoPin, HIGH);
  distance = duration*0.034/2;

  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);  
  duration2 = pulseIn(echoPin2, HIGH);
  distance2 = duration2*0.034/2;

  digitalWrite(trigPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin3, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin3, LOW);  
  duration3 = pulseIn(echoPin3, HIGH);
  distance3 = duration3*0.034/2;
   
    if ( distance < 30 )        //Backa och sväng undan om det kommer nåt i vägen rakt framifrån.
    {
    digitalWrite(rightPWM, HIGH);
    digitalWrite(leftPWM, HIGH);
    digitalWrite(leftForward , LOW);
    digitalWrite(leftBackward , HIGH);
    digitalWrite(rightForward , LOW);
    digitalWrite(rightBackward , HIGH);
    delay(800);
    //lastTurn = !lastTurn;     //opposite - toggla mellan true och false
    if (lastTurn == false )
    {
    digitalWrite(rightPWM, HIGH);
    digitalWrite(leftPWM, HIGH);
    digitalWrite(leftForward , LOW);
    digitalWrite(leftBackward , LOW);
    digitalWrite(rightForward , HIGH);
    digitalWrite(rightBackward , LOW);
    delay(600);
    lastTurn = true;
    }
    else if (lastTurn == true )
    {
    digitalWrite(rightPWM, HIGH);
    digitalWrite(leftPWM, HIGH);
    digitalWrite(leftForward , HIGH);
    digitalWrite(leftBackward , LOW);
    digitalWrite(rightForward , LOW);
    digitalWrite(rightBackward , LOW);
    delay(600);
    lastTurn = false;
    }
    }
    else if ( distance2 < 20 )      //Sväng vänster om det kommer nåt för nära till höger!!
    {
    digitalWrite(rightPWM, HIGH);
    digitalWrite(leftPWM, HIGH);
    digitalWrite(leftForward , LOW);
    digitalWrite(leftBackward , LOW);
    digitalWrite(rightForward , HIGH);
    digitalWrite(rightBackward , LOW);
    delay(400);
    }
    else if ( distance3 < 20)      //Sväng höger om det kommer nåt för nära till vänster!!
    {
    digitalWrite(rightPWM, HIGH);
    digitalWrite(leftPWM, HIGH);
    digitalWrite(leftForward , HIGH);
    digitalWrite(leftBackward , LOW);
    digitalWrite(rightForward , LOW);
    digitalWrite(rightBackward , LOW);
    delay(400);
    }
    else                          //Kör framåt!!
    {
    analogWrite(rightPWM, 155);
    analogWrite(leftPWM, 162);
    digitalWrite(leftForward , HIGH);
    digitalWrite(leftBackward , LOW);
    digitalWrite(rightForward , HIGH);
    digitalWrite(rightBackward , LOW);
    
    }
  }

I then wanted to try the NewPing library - but ended up with a code not directly following the recommended syntax from Tim Eckel. Now I get a mix of both ok but also some strange reactions from the motors.

My question is if this code although is ok, and if I´m still using the benefits of the NewPing library by coding this way??

#include <NewPing.h>

const int leftForward = 2;
const int leftBackward = 3;
const int rightForward = 4;
const int rightBackward = 5;

const int rightPWM = 10;
const int leftPWM = 6;

const int trigPin = 12;
const int echoPin = 11;
//long duration; 
int distance;

const int trigPin2 = 13;
const int echoPin2 = 9;
//long duration2; 
int distance2;

const int trigPin3 = 8;
const int echoPin3 = 7;
//long duration3; 
int distance3;

NewPing frontSensor(trigPin, echoPin);
NewPing rightSensor(trigPin2, echoPin2);
NewPing leftSensor(trigPin3, echoPin3);

bool lastTurn = false;
                
void setup() 
  {
  pinMode(leftForward , OUTPUT);
  pinMode(leftBackward , OUTPUT);
  pinMode(rightForward , OUTPUT);
  pinMode(rightBackward , OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
  pinMode(rightPWM, OUTPUT);
  pinMode(leftPWM, OUTPUT);
  }
void loop() 
{
  distance = frontSensor.ping_cm();
  distance2 = rightSensor.ping_cm();
  distance3 = leftSensor.ping_cm();
   
    if ( distance < 30 )        //Backa och sväng undan om det kommer nåt i vägen rakt framifrån.
    {
    digitalWrite(rightPWM, HIGH);
    digitalWrite(leftPWM, HIGH);
    digitalWrite(leftForward , LOW);
    digitalWrite(leftBackward , HIGH);
    digitalWrite(rightForward , LOW);
    digitalWrite(rightBackward , HIGH);
    delay(800);
    //lastTurn = !lastTurn;     //opposite - toggla mellan true och false
    if (lastTurn == false )
    {
    digitalWrite(rightPWM, HIGH);
    digitalWrite(leftPWM, HIGH);
    digitalWrite(leftForward , LOW);
    digitalWrite(leftBackward , LOW);
    digitalWrite(rightForward , HIGH);
    digitalWrite(rightBackward , LOW);
    delay(600);
    lastTurn = true;
    }
    else if (lastTurn == true )
    {
    digitalWrite(rightPWM, HIGH);
    digitalWrite(leftPWM, HIGH);
    digitalWrite(leftForward , HIGH);
    digitalWrite(leftBackward , LOW);
    digitalWrite(rightForward , LOW);
    digitalWrite(rightBackward , LOW);
    delay(600);
    lastTurn = false;
    }
    }
    else if ( distance2 < 20 )      //Sväng vänster om det kommer nåt för nära till höger!!
    {
    digitalWrite(rightPWM, HIGH);
    digitalWrite(leftPWM, HIGH);
    digitalWrite(leftForward , LOW);
    digitalWrite(leftBackward , LOW);
    digitalWrite(rightForward , HIGH);
    digitalWrite(rightBackward , LOW);
    delay(400);
    }
    else if ( distance3 < 20)      //Sväng höger om det kommer nåt för nära till vänster!!
    {
    digitalWrite(rightPWM, HIGH);
    digitalWrite(leftPWM, HIGH);
    digitalWrite(leftForward , HIGH);
    digitalWrite(leftBackward , LOW);
    digitalWrite(rightForward , LOW);
    digitalWrite(rightBackward , LOW);
    delay(400);
    }
    else                          //Kör framåt!!
    {
    analogWrite(rightPWM, 155);
    analogWrite(leftPWM, 162);
    digitalWrite(leftForward , HIGH);
    digitalWrite(leftBackward , LOW);
    digitalWrite(rightForward , HIGH);
    digitalWrite(rightBackward , LOW);
    
    }
  }

Hm......maybe the ELSE condition needs a (separate) delay of at least 30ms for the sensors to catch up??

It is the ELSE condition that behaves strange with the NewPing lib - the other conditions are ok?!!

Describe the "strange reactions".

What do you suppose are the benefits of the NewPing library? If the old program works, use it.

Note that with multiple sonar rangers, you will have problems with late echos from the first sensor ping leading to false responses by the second sensor, unless you wait for all echos from the first sensor to die down.

It was mainly the ELSE condition in my code, Robot going forward, that didn´t react the right way using the NewPing lib but worked ok the traditional way. I think it was only one wheel running?!!

When the ELSE condition is true the time between the pinging of the three sensors maybe gets a little bit tight I thought - but I haven´t yet corrected and tried it. The other conditions have delays built in the code!!

The benefits that interested me with the NewPing was for example:

  • Doesn't lag for a full second if no ping echo is received like all other ultrasonic libraries.

  • More accurate distance calculation (cm, inches & microseconds).

and

  • Doesn't use pulseIn, which is slow and gives incorrect results with some ultrasonic sensor models.

But as you said jremington - "If the old program works, use it."

What I also wondered was if my way of coding the NewPing was ok when I didn´t really followed the syntax Tim Eckel uses on teckel12 / Arduino New Ping / wiki / Home — Bitbucket . Maybe a stupid question that doesn´t matter?? :slight_smile:

Lots of delay()s in that code. It seems like you're reacting to a sensor doing some multi-second sequences, during which no further ultrasound measurements are done.

Get rid of those delay() calls, use millis() based timing and a finite state machine instead for your sequences.

Thanks wvmarle!

New things (for me) to learn!! :smiley: ;D