Ultasonic sensors

Hi there need some advice, i found this code and i would like to use it for my sensors, but i want to know if it would work so that all my sensors start at the same time but i dont understand the code that well, if you could please tell me first of all if it will work and second what would i need to change in the code?

Thank you

#include <NewPing.h>

#define SONAR_NUM 3 // Number of sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 0 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).

unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned int cm[SONAR_NUM]; // Where the ping distances are stored.
uint8_t currentSensor = 0; // Keeps track of which sensor is active.

NewPing sonar[SONAR_NUM] = { // Sensor object array.
NewPing(6, 7, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
NewPing(8, 9, MAX_DISTANCE),
NewPing(11, 12, MAX_DISTANCE)
};

void setup() {
Serial.begin(115200);
pingTimer[0] = millis() + 75; // First ping starts at 75ms, gives time for the Arduino to chill before starting.
for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor - Can i change this to make them start at the same time?????
pingTimer = pingTimer[i - 1] + PING_INTERVAL;
}
void loop() {

  • for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.*
    _ 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).
    _ }
    }
    // Other code that DOESN'T analyze ping results can go here.
    }
    void echoCheck() { // If ping received, set the sensor distance to array._

    if (sonar[currentSensor].check_timer())
    cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
    _}
    void oneSensorCycle() { // Sensor ping cycle complete, do something with the results.
    // The following code would be replaced with your code that does something with the ping results._
    for (uint8_t i = 0; i < SONAR_NUM; i++) {
    _ Serial.print(i);
    Serial.print("=");
    Serial.print(cm);
    Serial.print("cm ");
    }
    Serial.println();
    }_

    *

Now can you see why we ask you to use CODE TAGS when posting code?

Yes i see why it does help alot. :slight_smile:

Undermentioned:
Yes i see why it does help alot. :slight_smile:

Well now you could go back to first post and Modify it to put the code in tags so it's not half italics

ok some explain ,,

int incoming1;  // int for incoming 
int incoming2;  // int for incoming 
int incoming3;  // int for incoming 
int incoming4;  // int for incoming 
int sensor;   // in also for incoming 
int sensors ; // counter for sensors 
#include <NewPing.h>   // this is a lib you can download for your sonic range sensor https://code.google.com/p/arduino-new-ping/
#define MAX_DISTANCE 500 // here i put the sensor max reading ,, 5 metrs  !!!
void setup() {
  Serial.begin(9600); 
}

void loop (){
  delay (1000);
  sensor= sensor +1;
  if (sensor >=5)sensor=0; // counts til 5 and wil go back ,, so it changes to other sensors ,,
  if (sensor =1){
  NewPing sonar(2, 3, MAX_DISTANCE); // sensor pins for incoming sensor 1 
  incoming1 = sonar.ping();  // here it wil ping a signal and recieve 
  sensor = incoming1/ US_ROUNDTRIP_CM;  // here it converts the value to cm 
  Serial.print ("cm =");  /// prints out the reading value
  Serial.println (sensor); 
  Serial.print ("sensor digital reading value incoming 1  ");// prints out the reading value in cm 
  Serial.println (incoming1); 
}
 if (sensor =2){
  NewPing sonar(4, 5, MAX_DISTANCE); // sensor pins for incoming sensor 2 
  incoming2 = sonar.ping();  // here it wil ping a signal and recieve 
  sensor = incoming2/ US_ROUNDTRIP_CM;  // here it converts the value to cm 
  Serial.print ("cm =");  /// prints out the reading value
  Serial.println (sensor); 
  Serial.print ("sensor digital reading value incoming 2  ");// prints out the reading value in cm 
  Serial.println (incoming2); 
}
 if (sensor =3){
  NewPing sonar(6, 7, MAX_DISTANCE); // sensor pins for incoming sensor 3 
  incoming3 = sonar.ping();  // here it wil ping a signal and recieve 
  sensor = incoming3/ US_ROUNDTRIP_CM;  // here it converts the value to cm 
  Serial.print ("cm =");  /// prints out the reading value
  Serial.println (sensor); 
  Serial.print ("sensor digital reading value incoming 3  ");// prints out the reading value in cm 
  Serial.println (incoming3);
 }
 if (sensor =4){
  NewPing sonar(8, 9, MAX_DISTANCE); // sensor pins for incoming sensor 4 
  incoming4 = sonar.ping();  // here it wil ping a signal and recieve 
  sensor = incoming4/ US_ROUNDTRIP_CM;  // here it converts the value to cm 
  Serial.print ("cm =");  /// prints out the reading value
  Serial.println (sensor); 
  Serial.print ("sensor digital reading value incoming 4  ");// prints out the reading value in cm 
  Serial.println (incoming4);  
 }
 if (sensor =0){
    Serial.print ("sensor digital reading value incoming 1  ");// prints out the reading value in cm 
  Serial.println (incoming1); 
  Serial.print ("sensor digital reading value incoming 2  ");// prints out the reading value in cm 
  Serial.println (incoming2); 
  Serial.print ("sensor digital reading value incoming 3  ");// prints out the reading value in cm 
  Serial.println (incoming3);
  Serial.print ("sensor digital reading value incoming 4  ");// prints out the reading value in cm 
  Serial.println (incoming4);
  Serial.print ("all outputs  ");// prints out the reading value in cm 
}
}

Thank you Dexter, it explains alot, but the sensors are not sending and receiving at the same time are they? i attached a pic of what im doing will it work for that?

if (sensor =1){

Oops.

What does oops mean awol haha?

He's pointing out than when you want to test equivalence you need to use == not just =.

if (sensor ==1){

oops ??

i use the sensor ,, for counting ,,
you can also put this in series ,, so you wil never read them al at same time ,,
line after line ,,
now it counts sensor .. 1 then reads sensor 1 ,, then go to sensor 2 ,read 2 ,,
or did i doo something wrong ?
oke the counter ,, i found it :sweat_smile:

int incoming1;  // int for incoming 
int incoming2;  // int for incoming 
int incoming3;  // int for incoming 
int incoming4;  // int for incoming 
int sensor;   // in also for incoming 
int sensors ; // counter for sensors 
#include <NewPing.h>   // this is a lib you can download for your sonic range sensor https://code.google.com/p/arduino-new-ping/
#define MAX_DISTANCE 500 // here i put the sensor max reading ,, 5 metrs  !!!
void setup() {
  Serial.begin(9600); 
}

void loop (){
  delay (1000);
  Serial.println (sensor); 
  sensor= sensor ++;
  if (sensor >=5)sensor=0; // counts til 5 and wil go back ,, so it changes to other sensors ,,
  if (sensor =1){
  NewPing sonar(2, 3, MAX_DISTANCE); // sensor pins for incoming sensor 1 
  incoming1 = sonar.ping();  // here it wil ping a signal and recieve 
  sensor = incoming1/ US_ROUNDTRIP_CM;  // here it converts the value to cm 
  Serial.print ("cm =");  /// prints out the reading value
  Serial.println (sensor); 
  Serial.print ("sensor digital reading value incoming 1  ");// prints out the reading value in cm 
  Serial.println (incoming1); 
}
 if (sensor =2){
  NewPing sonar(4, 5, MAX_DISTANCE); // sensor pins for incoming sensor 2 
  incoming2 = sonar.ping();  // here it wil ping a signal and recieve 
  sensor = incoming2/ US_ROUNDTRIP_CM;  // here it converts the value to cm 
  Serial.print ("cm =");  /// prints out the reading value
  Serial.println (sensor); 
  Serial.print ("sensor digital reading value incoming 2  ");// prints out the reading value in cm 
  Serial.println (incoming2); 
}
 if (sensor =3){
  NewPing sonar(6, 7, MAX_DISTANCE); // sensor pins for incoming sensor 3 
  incoming3 = sonar.ping();  // here it wil ping a signal and recieve 
  sensor = incoming3/ US_ROUNDTRIP_CM;  // here it converts the value to cm 
  Serial.print ("cm =");  /// prints out the reading value
  Serial.println (sensor); 
  Serial.print ("sensor digital reading value incoming 3  ");// prints out the reading value in cm 
  Serial.println (incoming3);
 }
 if (sensor =4){
  NewPing sonar(8, 9, MAX_DISTANCE); // sensor pins for incoming sensor 4 
  incoming4 = sonar.ping();  // here it wil ping a signal and recieve 
  sensor = incoming4/ US_ROUNDTRIP_CM;  // here it converts the value to cm 
  Serial.print ("cm =");  /// prints out the reading value
  Serial.println (sensor); 
  Serial.print ("sensor digital reading value incoming 4  ");// prints out the reading value in cm 
  Serial.println (incoming4);  
 }
  if (sensor =5){
  Serial.println ("");
  Serial.print ("sensor digital reading value incoming 1  ");// prints out the reading value in cm 
  Serial.println (incoming1); 
  Serial.print ("sensor digital reading value incoming 2  ");// prints out the reading value in cm 
  Serial.println (incoming2); 
  Serial.print ("sensor digital reading value incoming 3  ");// prints out the reading value in cm 
  Serial.println (incoming3);
  Serial.print ("sensor digital reading value incoming 4  ");// prints out the reading value in cm 
  Serial.println (incoming4);
  Serial.print ("all outputs  ");// prints out the reading value in cm 
}
}

and this is in series ,,

int incoming1;  // int for incoming 
int incoming2;  // int for incoming 
int incoming3;  // int for incoming 
int incoming4;  // int for incoming 
int sensor;   // in also for incoming 
int sensor1;   // in also for incoming 
int sensor2;   // in also for incoming 
int sensor3;   // in also for incoming 
int sensor4; // counter for sensors 
#include <NewPing.h>   // this is a lib you can download for your sonic range sensor https://code.google.com/p/arduino-new-ping/
#define MAX_DISTANCE 500 // here i put the sensor max reading ,, 5 metrs  !!!
void setup() {
  Serial.begin(9600); 
}

void loop (){
  delay (1000);
  NewPing sonar1(2, 3, MAX_DISTANCE); // sensor pins for incoming sensor 1 
  incoming1 = sonar1.ping();  // here it wil ping a signal and recieve 
  sensor1 = incoming2/ US_ROUNDTRIP_CM;
  NewPing sonar2(4, 5, MAX_DISTANCE); // sensor pins for incoming sensor 2 
  incoming2 = sonar2.ping();  // here it wil ping a signal and recieve 
  sensor2 = incoming2/ US_ROUNDTRIP_CM;  // here it converts the value to cm 
  NewPing sonar3(6, 7, MAX_DISTANCE); // sensor pins for incoming sensor 3 
  incoming3 = sonar3.ping();  // here it wil ping a signal and recieve 
  sensor3 = incoming3/ US_ROUNDTRIP_CM;  // here it converts the value to cm 
  NewPing sonar4(8, 9, MAX_DISTANCE); // sensor pins for incoming sensor 4 
  incoming4 = sonar4.ping();  // here it wil ping a signal and recieve 
  sensor4 = incoming4/ US_ROUNDTRIP_CM;  // here it converts the value to cm 
  Serial.println ("");
  Serial.print ("sensor digital reading value incoming 1  ");// prints out the reading value in cm 
  Serial.println (incoming1); 
  Serial.print (" cm "); 
  Serial.println (sensor1); 
  Serial.print ("sensor digital reading value incoming 2  ");// prints out the reading value in cm 
  Serial.println (incoming2); 
  Serial.print (" cm "); 
  Serial.println (sensor2); 
  Serial.print ("sensor digital reading value incoming 3  ");// prints out the reading value in cm 
  Serial.println (incoming3);
  Serial.print (" cm "); 
  Serial.println (sensor3); 
  Serial.print ("sensor digital reading value incoming 4  ");// prints out the reading value in cm 
  Serial.println (incoming4);
  Serial.print (" cm "); 
  Serial.println (sensor4); 
  Serial.print ("all outputs  ");// prints out the reading value in cm 
}

Dexter ive got three sensors two need to recieve and one only sends signal, so they need to start there cycle at the same time if im correct so that they dont time out or so they are in sync.

if (sensor =2)

Oops again.

 if (sensor =3)

and again . . .

another problem in that:

 int sensor;   // in also for incoming 
int sensors ; // counter for sensors

you are using sensor for this counter:

sensor= sensor ++;

and this value...

sensor = incoming2/ US_ROUNDTRIP_CM;  // here it converts the value to cm

change the name of your counter to something like

int counter;
void loop (){
  delay (1000);
  NewPing sonar1(2, 3, MAX_DISTANCE); // sensor pins for incoming sensor 1 
  incoming1 = sonar1.ping();  // here it wil ping a signal and recieve 
  sensor1 = incoming2/ US_ROUNDTRIP_CM;
  NewPing sonar2(4, 5, MAX_DISTANCE); // sensor pins for incoming sensor 2 
  incoming2 = sonar2.ping();  // here it wil ping a signal and recieve 
  sensor2 = incoming2/ US_ROUNDTRIP_CM;  // here it converts the value to cm 
  NewPing sonar3(6, 7, MAX_DISTANCE); // sensor pins for incoming sensor 3 
  incoming3 = sonar3.ping();  // here it wil ping a signal and recieve 
  sensor3 = incoming3/ US_ROUNDTRIP_CM;  // here it converts the value to cm 
  NewPing sonar4(8, 9, MAX_DISTANCE); // sensor pins for incoming sensor 4 
  incoming4 = sonar4.ping();  // here it wil ping a signal and recieve 
  sensor4 = incoming4/ US_ROUNDTRIP_CM;  // here it converts the value to cm

This is why arrays and for loops were invented.

it works on my side :drooling_face:

maybe this is not the way you learn it ,, but it works

Undermentioned:
I've got three sensors. Two need to recieve and one only sends signal, so they need to start there cycle at the same time, if I'm correct, so that they don't time out or so they are in sync.

Connect the three Trigger pins together. Remove the senders from the two units that you want to be Receive Only. Trigger all three by pulsing the Trigger pins. Connect the two Echo pins to two digital input pins and alternate between reading the two inputs.

If you bought units that have a combined Trigger and Echo pin, spend $6 to get three of the the HC-SR04 model:

http://www.fasttech.com/product/1012007

ok you want one transducer to transmit a beacon ,, others to read ,,
i tryed this method once ,,
you can do as above ,, but you stil use 4 x a input ,, if you want diverisity ,,
my english is not super ,, so i hope you understand ,,
you have to keep in mind that you wil have milis seconds delay ,, these delays are also measured
so if you can measure those 4 x 1 meter ,,
check the diferents of al 4 ,, then calibrate them whit other value ,,
example
sensor 1 reads 1000 good reading because this is the first one picking up the beacon !
sensor 2 reads 920 same reading but second one to pick up the beacon ,, so delays the beacon ,other result
sensor 3 reads 830
sensor 4 reads 740
it can also be reversed ,, depents on hook up ,, then it wil be higher readings ,,
beacons tx ,, bounces back ,, this time is calculated ,,
you wil get something like this ,, you have to use some sum to let it say
easy method i use ,,

sensor 1 reads 1000
sensor 2 reads 920 =+80 =1000
sensor 3 reads 830=+170=1000
sensor 4 reads 740=+260=1000
you get it this is a easy way to calibrate this :%

but i think when you use the echo pin it wil start calculating when it got asked for it ,
so this wil also delay ,, the beacon

AWOL:
This is why arrays and for loops were invented.

it is a very good suggestion.

Take a look at how much more simple your code can be if you used arrays and for loops:

(untested)

#include <NewPing.h> 
#define MAX_DISTANCE 500 
//
int counter; 
int sensorPins [4] [2] = {{2,3},{4,5},{6,7},{8,9}};
int incoming [4];
int myRead;
//
void setup() 
{
  Serial.begin(9600); 
}

void loop ()
{
  delay (1000);
  Serial.println (counter); 
  counter++;
  if (counter > 3) counter = 0; 
  incoming [counter] = updateDistance(sensorPins[counter][1], sensorPins[counter][2]);
  Serial.print ("cm =");
  Serial.println (incoming [counter]); 
  Serial.print ("sensor digital reading value incoming ");// prints out the reading value in cm 
  Serial.println (myRead);
  if (counter = 3)
  {
    for (int i = 0; i < 4; i++)
    {
      Serial.print(F("sensor digital reading value incoming "));
      Serial.print(i);
      Serial.print(F("  "));
      Serial.println(incoming[i]);
      Serial.print(F("all outputs  "));
    }
  }
}

int updateDistance(int pin1, int pin2)
{
  NewPing sonar(pin1, pin2, MAX_DISTANCE); 
  myRead = sonar.ping();  
  int myValue = myRead / US_ROUNDTRIP_CM; 
  return myValue;
}

yea realy simpel for some one ,, :%LOL

why explain hard

if i want to learn ,, your code ,,
:sweat_smile:
why is what , explain ,,
this is why i prefer my way ,, if i explain !
thank you all :%

if this al so simpel ,, why do a noob like me have to build a code for him ,,
and if i read the answers ,,, looks like he could bite the dust !!
you taken the time to make the sketch ,,
why now and not before ,
i just try to help him a bit ,,
maybe this was not needed if everybody was a bit nicer to each other ,,
its stil open source ,,
but people want money for sketches ,, and things i see here
i not gone refer to people ,, butt ok ,,
i do my funky sketches for free ,,,, and if they are happy
i am happy :smiley:

is your milkshake better than mine ?? now ??
some one ask a thing you wil get 3 answers ,, others wil do some kind of debug on your post ,,
i dont need to be debuged , to be noticed what i forget ,,
this was not the subject !!
it starts to be the number 1 subject here !! on the board ,,
a bigg loss when whe have so much experts ,, !!
and stil so much un answered !
and yes my english is also not wat it should be !

and this is not refered to
BulldogLowell ,, butt i see some in other post ,, and this was the last drop ,.,lol :grin:

I agree dexter some people dont make it easy for new people that are still learning, but im very thank full to everyone that helps.

John would i use pulsein to read the 2 inputs?