HC-SR04

Why I can not read ping0 and ping1

code :

#include <NewPing.h>

#define SONAR_NUM 3 // Number or sensors.
#define MAX_DISTANCE 200 // Max distance in cm.
#define PING_INTERVAL 33 // Milliseconds between pings.

byte dir = 0;

unsigned long pingTimer[SONAR_NUM]; // When each pings.
unsigned int cm[SONAR_NUM]; // Store ping distances.
uint8_t currentSensor = 0; // Which sensor is active.

NewPing sonar[SONAR_NUM] = { // Sensor object array.
NewPing (10, 13, MAX_DISTANCE),
NewPing (11, 12, MAX_DISTANCE),
NewPing (5, 4, MAX_DISTANCE)
};

const int dangerThresh = 870; // 15cm × 58
const byte speed = 100;
const byte EA = 6;
const byte IA = 7;
const byte EB = 9;
const byte IB = 8;

void setup() {
Serial.begin(9600);
pinMode(IA, OUTPUT);
pinMode(IB, OUTPUT);
pingTimer[0] = millis() + 75; // First ping start in ms.
for (uint8_t i = 1; i < SONAR_NUM; i++)
pingTimer = pingTimer[i - 1] + PING_INTERVAL;
}
void stop() {

  • analogWrite(EA, 0);*
  • digitalWrite(IA, LOW);*
  • analogWrite(EB, 0);*
  • digitalWrite(IB, LOW);*
    }
    void forward() {
  • analogWrite(EA, speed);*
  • digitalWrite(IA, HIGH);*
  • analogWrite(EB, speed);*
  • digitalWrite(IB, HIGH);*
    }
    void backward() {
  • analogWrite(EA, speed);*
  • digitalWrite(IA, LOW);*
  • analogWrite(EB, speed);*
  • digitalWrite(IB, LOW);*
    }
    void turnRight() {
  • analogWrite(EA, speed);*
  • digitalWrite(IA, LOW);*
  • analogWrite(EB, speed);*
  • digitalWrite(IB, HIGH);*
    }
    void turnLeft() {
  • analogWrite(EA, speed);*
  • digitalWrite(IA, HIGH);*
  • analogWrite(EB, speed);*
  • digitalWrite(IB, LOW); *
    }
    void echoCheck() { // If ping echo, set distance to array.
  • if (sonar[currentSensor].check_timer())*
  • cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;*
    }
    void oneSensorCycle() { // Do something with the results.
  • for (uint8_t i = 0; i < SONAR_NUM; i++) {*
  • Serial.print("ping");*
  • Serial.print(i);*
  • Serial.print("=");*
    _ Serial.print(cm*);_
    _
    Serial.print("cm ");_
    _
    }_
    _
    Serial.println();_
    _
    }_
    void loop() {
    _
    const byte speed = 100;_
    _
    delay(50);_
    _
    loopAllPings();_
    _
    }_
    void loopAllPings() {
    for (uint8_t i = 0; i < SONAR_NUM; i++) {
    _ if (millis() >= pingTimer) { // Is it this sensor's time to ping?_
    pingTimer += PING_INTERVAL * SONAR_NUM; // Set next time this sensor will be pinged.
    if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
    sonar[currentSensor].timer_stop(); // Make sure previous timer is canceled before starting a new ping (insurance).
    _ currentSensor = i; // Sensor being accessed.
    cm[currentSensor] = 0; // Make distance zero in case there's no ping echo for this sensor._

    sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
    _ }
    }*_

* if (cm[0] > dangerThresh && cm[1] > dangerThresh && cm[2] > dangerThresh){ //0 0 0*
* if (dir != 0) { *
* dir = 0;*
* stop(); *
* delay(50);*
* }*
* forward();*
* }*
* else if(cm[0] > dangerThresh && cm[1] > dangerThresh && cm[2] < dangerThresh){ //0 0 1*
* if (dir != 1) { *
* dir = 1;*
* stop(); *
* delay(50);*
* }*
* turnLeft();*
* delay(50); *
* }*
* else if(cm[0] > dangerThresh && cm[1] < dangerThresh && cm[2] < dangerThresh){ //0 1 1*
* if (dir != 1) { *
* dir = 1;*
* stop(); *
* delay(50);*
* }*
* turnLeft();*
* delay(50); *
* }*
* else if(cm[0] < dangerThresh && cm[1] > dangerThresh && cm[2] > dangerThresh){ //1 0 0*
* if (dir != 1) { *
* dir = 1;*
* stop(); *
* delay(50);*
* }*
* turnRight();*
* delay(50); *
* }*
* else if(cm[0] < dangerThresh && cm[1] < dangerThresh && cm[2] > dangerThresh){ //1 1 0*
* if (dir != 1) { *
* dir = 1;*
* stop(); *
* delay(50);*
* }*
* turnLeft();*
* delay(50); *
* }*
* else if(cm[0] > dangerThresh && cm[1] < dangerThresh && cm[2] > dangerThresh){ //0 1 0*
* if (dir != 1) { *
* dir = 1;*
* stop(); *
* delay(50);*
* }*
* turnLeft();*
* delay(50); *
* }*
* else if(cm[0] > dangerThresh && cm[1] < dangerThresh && cm[2] < dangerThresh){ //1 0 1*
* if (dir != 0) { *
* dir = 0;*
* stop(); *
* delay(50);*
* }*
* forward();*
* delay(50); *
* }*
* else if(cm[0] < dangerThresh && cm[1] < dangerThresh && cm[2] < dangerThresh){ //1 1 1*
* if (dir != 1) { *
* dir = 1;*
* stop(); *
* delay(50);*
* }*
* turnRight();*
* delay(50); *
* }*
}

j1182478:
Why I can not read ping0 and ping1

Most likely because there is no ping0 or ping1 in your code.

By the way,

Please post code between code [/] tags for much better readability!

but I can read ping2
others ping is 0

Maybe you should post the code you're having problems with, as the code you posted doesn't have a ping2 in it either, so whatever the posted code is supposed to do, it won't give results for a ping2.
A schematic of your wiring and explanation on what you're actually trying to do with your code may also help.

My result is

ping0=11cm ping1=0cm ping2=11cm
ping0=11cm ping1=0cm ping2=11cm
ping0=11cm ping1=0cm ping2=10cm
ping0=11cm ping1=0cm ping2=10cm
ping0=11cm ping1=0cm ping2=10cm
ping0=0cm ping1=12cm ping2=11cm
ping0=0cm ping1=11cm ping2=10cm
ping0=0cm ping1=11cm ping2=10cm
ping0=0cm ping1=11cm ping2=10cm
ping0=0cm ping1=0cm ping2=10cm
ping0=11cm ping1=0cm ping2=10cm
ping0=11cm ping1=0cm ping2=10cm
ping0=11cm ping1=0cm ping2=10cm
ping0=11cm ping1=0cm ping2=10cm
ping0=0cm ping1=12cm ping2=10cm
ping0=0cm ping1=12cm ping2=10cm
ping0=0cm ping1=13cm ping2=11cm
ping0=0cm ping1=13cm ping2=11cm
ping0=0cm ping1=13cm ping2=10cm

Why ping0 and ping1 can not read at the same time

Please post your code, in code tags

The delay of 50 mS in your loop is probably the cause of the missing results. The timers expire at 33 mS so at least one timer will expire during that delay.
Because you have not used code tags in your post, all the subscripts [ i ] have vanished.

@wvmarle: look carefully at oneSensorCycle()

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Also rewrite your code with all the motor control code removed, lets do your code in stages.

So first write code to get one sensor working.

Then write code to get the other sensor working,

Then combine them and get the code working

Then write code for the output control and get the code working.

Then combine you codes and get that working.

It may sound slow and boring, but you will end up with better developed code that is easier to debug.

Thanks.. Tom.. :slight_smile: