HC-SR04 and new ping. how to action event?

Hi guys.
I have done loads of reading and followed many misleading/incomplete sketches and i havent found what im looking for..

Basically i want to build some garage traffic lights with 3 leds/arduino nano and the HC-SR04.
Ive read the ping 1.5 sketch and understand how to mod it for the nano but i cant work out how to actually take the measurement data and get it to switch the leds on.eg <4m green, <2m yellow, <1m red

Do i use the serial print and then use the "cm" to reference?
ie if "cm"<400 then...

sorry this is basic prog language but as ive said ive looked and cant find anything that actually does something with the measurement other than serial print it.

thanks in advance

Floydie:
Do i use the serial print and then use the "cm" to reference?
ie if "cm"<400 then...

Hi,

Serialprinting in examples is often used as a proof of concept, as long as you see distance changing on the monitor, you know the distance sensor is working.

You can indeed use if - else statements depending on the distance and light the right led.
Assuming cm is the variable holding distance (and pins 2,3 and 4 being declared as output), the next code should work.

  if (cm >= 400){ // Distance more as 4 mtr, no lights
    digitalWrite(2,LOW); //Green led
    digitalWrite(3,LOW); //Yellow Led
    digitalWrite(4,LOW); //Red led
  } 
  else if (cm >= 200){ // Distance between 4 and 2 meter, green light
    digitalWrite(2,HIGH);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
  }
  else  if (cm >= 100){ // Distance between 2 and 1 meter, yellow light
    digitalWrite(2,LOW);
    digitalWrite(3,HIGH);
    digitalWrite(4,LOW);
  }  
  else { // Distance less as a mtr, red light
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,HIGH);
  }

Many thanks for that! thats certainly a great base for me to tinker with and understand the code.
cheers!

so this should work?

// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------

#include <NewPing.h>

#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 410 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
pinMode(3, OUTPUT);
pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
}

void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println(“cm”);
if (uS / US_ROUNDTRIP_CM >= 400){ // Distance more as 4 mtr, no lights
digitalWrite(2,LOW); //Green led
digitalWrite(3,LOW); //Yellow Led
digitalWrite(4,LOW); //Red led
}
else if (uS / US_ROUNDTRIP_CM >= 200){ // Distance between 4 and 2 meter, green light
digitalWrite(2,HIGH);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
}
else if (uS / US_ROUNDTRIP_CM >= 100){ // Distance between 2 and 1 meter, yellow light
digitalWrite(2,LOW);
digitalWrite(3,HIGH);
digitalWrite(4,LOW);
}
else { // Distance less as a mtr, red light
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,HIGH);
}
}

I must say I haven't done anything with hc-sr04 sensors yet, nor all different libraries related.

When (uS / US_ROUNDTRIP_CM) does present the right distance in cm it should indeed work.

It doesn't matter whether you use real or fake distances to test/debug your code by the way, you can... check whether your code works, even with no leds or sensor attached.

In the next bit of code Arduino will check fake distances from 500cm to 0cm in 25 cm steps using a for()- loop, light the led needed (if present) and show results on the serial monitor.

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  pinMode(3, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);
}

void loop() {
  // Create fake distances with a for( ) -loop 
  for(int Distance = 500; Distance >=0; Distance= Distance -25){
    // check/show what happens at different distances
    if (Distance >= 400){ // Distance more as 4 mtr, no lights
      digitalWrite(2,LOW); //Green led
      digitalWrite(3,LOW); //Yellow Led
      digitalWrite(4,LOW); //Red led
      Serial.print (Distance);
      Serial.println( " CM, no lights");
    } 
    else if (Distance >= 200){ // Distance between 4 and 2 meter, green light
      digitalWrite(2,HIGH);
      digitalWrite(3,LOW);
      digitalWrite(4,LOW);
      Serial.print (Distance);
      Serial.println( " CM, light = green");

    }  
    else  if (Distance >= 100){ // Distance between 2 and 1 meter, yellow light
      digitalWrite(2,LOW);
      digitalWrite(3,HIGH);
      digitalWrite(4,LOW);
      Serial.print (Distance);
      Serial.println( " CM, light = yellow");

    }  
    else 
    { // Distance less as a mtr, red light
      digitalWrite(2,LOW);
      digitalWrite(3,LOW);
      digitalWrite(4,HIGH);
      Serial.print (Distance);
      Serial.println( " CM, light = RED !!!");
    }
    delay(500); // In order to actually see them, Leds need to be on for a while.
  }
  
  // next loop will never end, to just show results only once
  while (1==1){
  }
}

This way you can check what will happen at different distances quickly and determine whether the code will work when presented with real data.

the problem with newping , when there is bad reading it wil give you a 0 number ,
so if you have bad readings it wil be always bellow everything because of number 0 reading !
i solved this by using ,, when reading is > 0 then buffer is incoming ,,

and then use the incoming for reading ,,

example , it read 100 110 111 0 111 0 111
now when you use the buffer ,,
it wil be 100 110 111 111 111 ,, no zero i use this method on my quadcopter to onley use real readings ! ??? maybe its handy for your sketch

#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 410 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
double incoming;
double usable;
void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  pinMode(3, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);
}

void loop() {
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  incoming = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  if (incoming > 0 )usable = incoming/ US_ROUNDTRIP_CM; 
  
  Serial.print("Ping: ");
  Serial.print(usable ); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
 if (usable >= 400){ // Distance more as 4 mtr, no lights
    digitalWrite(2,LOW); //Green led
    digitalWrite(3,LOW); //Yellow Led
    digitalWrite(4,LOW); //Red led
  } 
  else if (usable >= 200){ // Distance between 4 and 2 meter, green light
    digitalWrite(2,HIGH);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
  }
  else  if (usable  >= 100){ // Distance between 2 and 1 meter, yellow light
    digitalWrite(2,LOW);
    digitalWrite(3,HIGH);
    digitalWrite(4,LOW);
  }  
  else { // Distance less as a mtr, red light
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,HIGH);
  }  
}