NewPing Library: HC-SR04, SRF05, SRF06, DYP-ME007, Parallax PING))) - v1.7

Thanks for your help Tim, I have now attached the sensor to D3 and I am able to get readings via serial. Could you or anybody else help me change this code to work with your library:

#include <ArduinoRobot.h>
#include <Wire.h>
#include <SPI.h>

int sensorPin = M1;  // pin is used by the sensor

void setup() {
  // initialize the Robot, SD card, and display
  Serial.begin(9600);
  Robot.begin();
  Robot.beginTFT();
  Robot.beginSD();
  Robot.displayLogos();

  // draw a face on the LCD screen
  setFace(true);
}

void loop() {
  // If the robot is blocked, turn until free
  while (getDistance() < 40) { // If an obstacle is less than 20cm away
    setFace(false); //shows an unhappy face
    Robot.motorsStop(); // stop the motors
    delay(1000); // wait for a moment
    Robot.turn(90); // turn to the right and try again
    setFace(true); // happy face
  }
  // if there are no objects in the way, keep moving
  Robot.motorsWrite(255, 255);
  delay(100);
}

// return the distance in cm
float getDistance() {
  // read the value from the sensor
  int sensorValue = Robot.analogRead(sensorPin);
  //Convert the sensor input to cm.
  float distance_cm = sensorValue * 1.27;
  return distance_cm;
}

// make a happy or sad face
void setFace(boolean onOff) {
  if (onOff) {
    // if true show a happy face
    Robot.background(0, 0, 255);
    Robot.setCursor(44, 60);
    Robot.stroke(0, 255, 0);
    Robot.setTextSize(4);
    Robot.print(":)");
  } else {
    // if false show an upset face
    Robot.background(255, 0, 0);
    Robot.setCursor(44, 60);
    Robot.stroke(0, 255, 0);
    Robot.setTextSize(4);
    Robot.print("X(");
  }
}

This is meant to be used with an analog sensor like a Maxbotix EZ10.

Thanks for your help so far!
Jay.

P.S. All of the LCD stuff isn't important, it is just the sensor working with the motors I need working. Thanks again!

jaylfc:
Thanks for your help Tim, I have now attached the sensor to D3 and I am able to get readings via serial. Could you or anybody else help me change this code to work with your library:

#include <ArduinoRobot.h>

#include <Wire.h>
#include <SPI.h>

int sensorPin = M1;  // pin is used by the sensor

void setup() {
 // initialize the Robot, SD card, and display
 Serial.begin(9600);
 Robot.begin();
 Robot.beginTFT();
 Robot.beginSD();
 Robot.displayLogos();

// draw a face on the LCD screen
 setFace(true);
}

void loop() {
 // If the robot is blocked, turn until free
 while (getDistance() < 40) { // If an obstacle is less than 20cm away
   setFace(false); //shows an unhappy face
   Robot.motorsStop(); // stop the motors
   delay(1000); // wait for a moment
   Robot.turn(90); // turn to the right and try again
   setFace(true); // happy face
 }
 // if there are no objects in the way, keep moving
 Robot.motorsWrite(255, 255);
 delay(100);
}

// return the distance in cm
float getDistance() {
 // read the value from the sensor
 int sensorValue = Robot.analogRead(sensorPin);
 //Convert the sensor input to cm.
 float distance_cm = sensorValue * 1.27;
 return distance_cm;
}

// make a happy or sad face
void setFace(boolean onOff) {
 if (onOff) {
   // if true show a happy face
   Robot.background(0, 0, 255);
   Robot.setCursor(44, 60);
   Robot.stroke(0, 255, 0);
   Robot.setTextSize(4);
   Robot.print(":)");
 } else {
   // if false show an upset face
   Robot.background(255, 0, 0);
   Robot.setCursor(44, 60);
   Robot.stroke(0, 255, 0);
   Robot.setTextSize(4);
   Robot.print("X(");
 }
}




This is meant to be used with an analog sensor like a Maxbotix EZ10.

Thanks for your help so far!
Jay.

P.S. All of the LCD stuff isn't important, it is just the sensor working with the motors I need working. Thanks again!

To use NewPing, all you would do is include the library, setup the constructor, and send a ping. The basic NewPing example shows how to do this.

But, basically you would include the library like this:

#include <ArduinoRobot.h>

Setup the constructor:

NewPing sonar(sensorPin, sensorPin, 100);

And initiate a ping and get the result, which in your code would look something like this:

  while (sonar.ping_cm() < 40) { // If an obstacle is less than 40cm away

However, the sensor would need to be connected to the motor board, not the control board. I don't have an Arduino Robot to be able to tell you how to write your code. My guess is that you need two sketches, one for the control board and one for the motor board, and your sketch would need to communicate between the two boards to accomplish this. I have no first-hand usage of the Arduino Robot so there's no way for me to write working code for you. I tried, and it wouldn't compile.

I do know that you would need to connect the ultrasonic senor to one of the pins on the motor board (TK1-TK4). Other than that, you're on your own to write your own code. It should be VERY easy if you know how to program Arduino sketches and have any experience with using the Arduino Robot. If this is your first sketch, maybe you should start with something really basic first.

Tim

Thanks Tim,

I actually managed it before seeing this post but. I have actually soldered the sensor to the control board as this passes the commands to the motor board. The only problem I seem to be having is that once it detects an object within the desired range the pinging stops as do the measurements to the serial monitor. So when it is supposed to rotate and then carry on doing the same again it just seems to end and then the motors go into a wiggling action?

Here is my code:

#include <ArduinoRobot.h>
#include <NewPing.h>
#include <SPI.h>
#include <Wire.h>

#define TRIGGER_PIN  D3
#define ECHO_PIN  D3// Arduino pin tied to both trigger and echo pins on the ultrasonic sensor.
#define MAX_DISTANCE 200 // 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 pin and maximum distance.

void setup() {
  // initialize the Robot, SD card, and display
  Serial.begin(9600);
  Robot.begin();
  Robot.beginTFT();
  Robot.beginSD();
  Robot.displayLogos();

  // draw a face on the LCD screen
  setFace(true);
}

void loop() 
{
    {
    unsigned int cm = sonar.ping_cm();
    Serial.print("Distance: ");
    Serial.print(cm);
    Serial.println("cm");
    delay(500);
    }
     
 
  // If the robot is blocked, turn until free
  while (sonar.ping_cm() < 20) { // If an obstacle is less than 20cm away
    setFace(false); //shows an unhappy face
    Robot.motorsStop(); // stop the motors
    delay(500); // wait for a moment
    Robot.turn(45); // turn to the right and try again
    setFace(true); // happy face
  }
  // if there are no objects in the way, keep moving
  Robot.motorsWrite(155, 155);
  delay(100);
}


// make a happy or sad face
void setFace(boolean onOff) {
  if (onOff) {
    // if true show a happy face
    Robot.background(0, 0, 255);
    Robot.setCursor(44, 60);
    Robot.stroke(0, 255, 0);
    Robot.setTextSize(4);
    Robot.print(":)");
  } else {
    // if false show an upset face
    Robot.background(255, 0, 0);
    Robot.setCursor(44, 60);
    Robot.stroke(0, 255, 0);
    Robot.setTextSize(4);
    Robot.print("X(");
  }
}

Thanks again!

Jay.

jaylfc:
Thanks Tim,

I actually managed it before seeing this post but. I have actually soldered the sensor to the control board as this passes the commands to the motor board. The only problem I seem to be having is that once it detects an object within the desired range the pinging stops as do the measurements to the serial monitor. So when it is supposed to rotate and then carry on doing the same again it just seems to end and then the motors go into a wiggling action?

Thanks again!

Jay.

My guess is that it has something to do with the while loop where you never set a ping variable and never output serial results. Like usual, it's just doing what you're telling it to do.

I don't know why you have two nestled curly brackets after loop(), either or what that would even do. Also, you're doing multiple pings and probably shouldn't be. You're doing one to set the variable cm and print the results, then another ping in the while statement that's actually being used to detect an object. My guess is that your loop() routine should be more like this:

void loop() {
  unsigned int cm = sonar.ping_cm(); // Do a ping and set the distance in centimeters to variable cm
  Serial.println(cm); // Output the ping distance in centimeters
  if (cm < 20) { // If an obstacle is less than 20cm away
    Robot.motorsStop(); // stop the motors
    setFace(false); //shows an unhappy face
    Robot.turn(45); // turn to the right and try again
    delay(500); // wait for a moment while it turns (this isn't required if Robot.turn() is a blocking command)
  } else {
    Robot.motorsWrite(155, 155); // if there are no objects in the way, keep moving
    setFace(true); // happy face
    delay(50); // Delay between pings
  }
}

But, I don't ultimately know what you're trying to accomplish, nor have an Arduino Robot to test this with. But, this code makes more sense than what you had and it compiles. Also, this has nothing really to do with NewPing. This is more just simple programming debugging. You were creating a while loop where it wasn't setting a new distance variable nor sending that value to the serial port, so of course it won't output anything for as long as it's stuck in the while loop.

Tim

teckel:

eggie5:
Hi I'm trying to use your library with arduino due and the 1.5 version direct seem to have support. I noticed on the Google code page a note said that 1.6 will have support but it's not released yet. I tried to find the source on the Google code page but u don't seem to host the source code on it. Is there due support available?

Version 1.6 does support the Due. Well, I believe it does. I don't have a Due to verify this.

Tim

Hi Tim

I'm willing and able to test version 1.6 (even a not released version) on the Due. Do you know from where I can download it? A quick google search did not returned anything.

Thanks
Dan.

First, thank you for this valuable Ping Library,

I am attempting to use the same trigger ping for three HC-SR04 sensor on a Pro-Mini Arduino and so far, things appear to be working fine on the bench. My question is, are there any know issues with using the same trigger pin for two or more sensors?

Thank you.

I've never tried using the same trigger pin for multiple sensors nor is the library designed to work this way. What NewPing would do would be to actually try to initiate a ping three times each sensor. The sensor would probably ignore this, but there's no guarantee. Also, I would be concerned with the higher likelihood of echos giving bad results or the pulsing from multiple sensors confusing the sensors.

It would be better (avoiding echos and cross-talk issues) and use fewer pins to just wire a single pin to each sensor's trigger and echo pin. In other words, you would use 3 pins, each pin going to each sensor's trigger and echo pins. This would use 3 pins instead of (I assume) the 4 pins you're currently using. Yes, you would need to ping each sensor individually, but using the non-blocking ping_timer() method your microcontroller could be doing other things while the ping sound is traveling. I would consider this a more "sound" way to implement 3 sensors.

May I ask, why do you want to ping all three at the same time? Would it really matter if each ping was 30ms apart? You could still ping 3 sensors 10 times a second while still "multi-tasking" and doing other processes in your sketch. Insight on your project and the reasoning would be helpful.

Tim

teckel:
I've never tried using the same trigger pin for multiple sensors nor is the library designed to work this way. What NewPing would do would be to actually try to initiate a ping three times each sensor. The sensor would probably ignore this, but there's no guarantee. Also, I would be concerned with the higher likelihood of echos giving bad results or the pulsing from multiple sensors confusing the sensors.

It would be better (avoiding echos and cross-talk issues) and use fewer pins to just wire a single pin to each sensor's trigger and echo pin. In other words, you would use 3 pins, each pin going to each sensor's trigger and echo pins. This would use 3 pins instead of (I assume) the 4 pins you're currently using. Yes, you would need to ping each sensor individually, but using the non-blocking ping_timer() method your microcontroller could be doing other things while the ping sound is traveling. I would consider this a more "sound" way to implement 3 sensors.

May I ask, why do you want to ping all three at the same time? Would it really matter if each ping was 30ms apart? You could still ping 3 sensors 10 times a second while still "multi-tasking" and doing other processes in your sketch. Insight on your project and the reasoning would be helpful.

Tim

Thank you for the very quick response Tim,

The primary reason is the limitation of available pins on the Pro-Mini. I have each sensor angled outside of each others echo zone. so there is little or no interferences that I can detect at this point. I may have to upgrade to a Arduino mega before this project is finish.

DSC02417.JPG

How many pins are you using? 4? One pin for triggers on all sensors and 3 pins to get the results from each sensor? If so, you could just use 3 pins by using the method I describe.

Tim

teckel:
How many pins are you using? 4? One pin for triggers on all sensors and 3 pins to get the results from each sensor? If so, you could just use 3 pins by using the method I describe.

Tim

Please forgive me Tim, I am not understanding, I am using four pins as you have stated but I don't understand how to wire the sensor to only use three pins. This surely would be better because I can add a forth sensor pointing to the rear.

Here is the setup code I am presently using.

#define LED_STATUS_PIN 13
#define SONAR_NUM     3 // Number or sensors.
#define MAX_DISTANCE 300 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 66 // 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.
byte currentSensor = 0;          // Keeps track of which sensor is active.

NewPing sonar[SONAR_NUM] = {     // Sensor object array.
NewPing(7, 10, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
NewPing(7, 11, MAX_DISTANCE),
NewPing(7,  5, MAX_DISTANCE),
...
for (uint8_t i = 0; i < SONAR_NUM; i++) {       // Loop through all the sensors.
    if (millis() >= pingTimer[i]) {               // Is it this sensor's time to ping?
      pingTimer[i] += PING_INTERVAL * SONAR_NUM;  // Set next time this sensor will be pinged.
      if (i == 0 && currentSensor == SONAR_NUM - 1)
      {
          oneSensorCycle();
          serialCom(); 
      }          // 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] = 999;                      // 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).
    }
  }

NewPing supports the ability to use one pin per sensor. You simply wire a single pin to the trigger and the echo pins on the sensor. That's it! In your sketch, you would set the trigger and echo pin as the same pin. The magic all happens in the library. It would look something like this:

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

This would ping each device independently, and not all at once like you're doing now. The way you're doing it could cause bad echo problems.

Tim

teckel:
NewPing supports the ability to use one pin per sensor. You simply wire a single pin to the trigger and the echo pins on the sensor. That's it! In your sketch, you would set the trigger and echo pin as the same pin. The magic all happens in the library. It would look something like this:

NewPing sonar[SONAR_NUM] = {     // Sensor object array.

NewPing(10, 10, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
NewPing(11, 11, MAX_DISTANCE),
NewPing(5,  5, MAX_DISTANCE),
NewPing(7,  7, MAX_DISTANCE)
};




This would ping each device independently, and not all at once like you're doing now. The way you're doing it could cause bad echo problems.

Tim

Absolutely Fantastic!!!

Thank you Tim.

I used newping libray but It error compiling...

sketch_may15a.cpp.o: In function __static_initialization_and_destruction_0': C:\Program Files\Arduino/sketch_may15a.ino:10: undefined reference to NewPing::NewPing(unsigned char, unsigned char, int)'
C:\Program Files\Arduino/sketch_may15a.ino:10: undefined reference to NewPing::NewPing(unsigned char, unsigned char, int)' C:\Program Files\Arduino/sketch_may15a.ino:10: undefined reference to NewPing::NewPing(unsigned char, unsigned char, int)'
sketch_may15a.cpp.o: In function loop': C:\Program Files\Arduino/sketch_may15a.ino:18: undefined reference to NewPing::ping_cm()'

Please hlep me!

phamtuan1151:
I used newping libray but It error compiling...

sketch_may15a.cpp.o: In function __static_initialization_and_destruction_0': C:\Program Files\Arduino/sketch_may15a.ino:10: undefined reference to NewPing::NewPing(unsigned char, unsigned char, int)'
C:\Program Files\Arduino/sketch_may15a.ino:10: undefined reference to NewPing::NewPing(unsigned char, unsigned char, int)' C:\Program Files\Arduino/sketch_may15a.ino:10: undefined reference to NewPing::NewPing(unsigned char, unsigned char, int)'
sketch_may15a.cpp.o: In function loop': C:\Program Files\Arduino/sketch_may15a.ino:18: undefined reference to NewPing::ping_cm()'

Please hlep me!

Sounds like you didn't install the library correctly. Did you decompress it (keeping the directory structure in tact) and put it in your arduino's libraries folder? I believe there's now also an option in Arduino 1.0.5 to import a library.

Also, you should really always provide your sketch (or mention if you're using an example sketch). But, these errors are basically saying that it can't find the library AT ALL. So, you didn't install it correctly (or you just need to close Arduino and open it again). If you do this and verify the library is installed correctly in your libraries folder like the rest of your libraries and you still have a problem, please indicate the sketch you're using or paste the sketch to your reply.

Tim

Hi,
Library Install HOW-To HERE:

Hi Tim, Thanks for your work!

I am doing an installation that works with two sr04, and I am quite satisfied with the results I am achieving so far.
The code I am using is this:

#include <NewPing.h>

#define SONAR_NUM      2
#define MAX_DISTANCE 350
#define PING_INTERVAL 33

int rele1 = 7;
int rele2 = 8;


unsigned long pingTimer[SONAR_NUM];
uint8_t currentSensor = 0;

NewPing sonar[SONAR_NUM] = {
  NewPing(13, 12, MAX_DISTANCE),

  NewPing(4, 2, MAX_DISTANCE)
};

void setup() {
  pinMode (rele1, OUTPUT);
  pinMode (rele2, OUTPUT);
  
  Serial.begin(115200);
  pingTimer[0] = millis() + 75;
  for (uint8_t i = 1; i < SONAR_NUM; i++)
    pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
}

void loop() {
  for (uint8_t i = 0; i < SONAR_NUM; i++) {
    if (millis() >= pingTimer[i]) {
      pingTimer[i] += PING_INTERVAL * SONAR_NUM;
      sonar[currentSensor].timer_stop();
      currentSensor = i;
      sonar[currentSensor].ping_timer(echoCheck);
    }
  }
  // Other code that *DOESN'T* analyze ping results can go here.
}

void echoCheck() {
  if (sonar[currentSensor].check_timer())
    pingResult(currentSensor, sonar[currentSensor].ping_result / US_ROUNDTRIP_CM);
}

void pingResult(uint8_t sensor, int cm){
 // The following code would be replaced with your code that does something with the ping result.

int i;
float f;
f = cm;

i = (int) f/2;

if ( i > 10) 
{ digitalWrite (rele1, HIGH);
 digitalWrite (rele2, HIGH);
}else{
i = 0;
digitalWrite (rele1, LOW);
 digitalWrite (rele2, LOW);


Serial.println(i);

}
}

and I just wanted to turn on a pair of relays based on the sensors activity, and so far i got it to work almost the way i want, the sensors will work almost in parallel (slightly different angles to give'em more viewing angle). I found the cast operator to be more friendly to deal with the cm array, but I am not sure if its the proper way of doing it.

as of the relays, i want them to turn on when there's somebody in the range of the sensors, otherwise it must be turned off, any advice or suggestion will be much apreciated, and what I got so far is, they turn on on presence, but turn off only when somebody is 10 cm or closer, but not when out of range, i tried to stop and turn the ping on again as suggested on page 19, but I'm not achieving the results I seek

and Teckel, excelent job, and thanks a bunch for your activity in this thread, as an arduino lover and a maker, I am obliged to thank people like you!

fernandossala:
I am doing an installation that works with two sr04, and I am quite satisfied with the results I am achieving so far.

and I just wanted to turn on a pair of relays based on the sensors activity, and so far i got it to work almost the way i want, the sensors will work almost in parallel (slightly different angles to give'em more viewing angle). I found the cast operator to be more friendly to deal with the cm array, but I am not sure if its the proper way of doing it.

as of the relays, i want them to turn on when there's somebody in the range of the sensors, otherwise it must be turned off, any advice or suggestion will be much apreciated, and what I got so far is, they turn on on presence, but turn off only when somebody is 10 cm or closer, but not when out of range, i tried to stop and turn the ping on again as suggested on page 19, but I'm not achieving the results I seek

and Teckel, excelent job, and thanks a bunch for your activity in this thread, as an arduino lover and a maker, I am obliged to thank people like you!

I'm not sure why you're creating the floating variable "f" and why you're then dividing the distance by two. That will just add a lot of overhead to your code. But, a distance of "0" means out of range. So, if you want to turn something on when in rage and off when out of range your code would look something like this (I cleaned it up a bit too):

#include <NewPing.h>

#define SONAR_NUM      2
#define MAX_DISTANCE 350
#define PING_INTERVAL 33

#define RELE1 7
#define RELE2 8

unsigned long pingTimer[SONAR_NUM];
uint8_t currentSensor = 0;

NewPing sonar[SONAR_NUM] = {
  NewPing(13, 12, MAX_DISTANCE),
  NewPing(4, 2, MAX_DISTANCE)
};

void setup() {
    Serial.begin(115200);

    pinMode (RELE1, OUTPUT);
    pinMode (RELE2, OUTPUT);
    pingTimer[0] = millis() + 75;
    for (uint8_t i = 1; i < SONAR_NUM; i++)
      pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
}

void loop() {
  for (uint8_t i = 0; i < SONAR_NUM; i++) {
    if (millis() >= pingTimer[i]) {
      pingTimer[i] += PING_INTERVAL * SONAR_NUM;
      sonar[currentSensor].timer_stop();
      currentSensor = i;
      sonar[currentSensor].ping_timer(echoCheck);
    }
  }
  // Other code that *DOESN'T* analyze ping results can go here.
}

void echoCheck() {
  if (sonar[currentSensor].check_timer())
    pingResult(currentSensor, sonar[currentSensor].ping_result / US_ROUNDTRIP_CM);
}

void pingResult(uint8_t sensor, int cm){
  if ( cm == 0 ) { 
    digitalWrite (RELE1, LOW);
    digitalWrite (RELE2, LOW);
  } else {
    digitalWrite (RELE1, HIGH);
    digitalWrite (RELE2, HIGH);
  }

  Serial.println(cm);
}

The problem with this code however is that you have two sensors. One may detect an object and the other may not. So, it could turn on and off so quickly you can't even tell. You probably need to use the original multi-sensor sketch that stores the results in the array and then process the results where you can make logic decisions based upon results of maybe one sensor detecting an object and the other not, or distances that are wildly different.

Basically, while my above sketch will work the way you asked, it won't really work the way you want. Going back to the original sketch and storing the results in the array and then using different logic is probably what you really want.

Tim

When I first tried the example sketches, I printed every single piece of code I found could be handy to work with, the objects in the array cm [0] and cm [1] where the variables I found most important (all other I just couldn't print), but, again I couldn't do much with them as I have poor code skills, I was looking for some piece of code that could convert whatever I was looking at to something I could do some math with , later I found I was merging the results into one, so I decided I needed to divide the result by two. Ok, not a smart decision, I know...

And with your sketch, I did came to similar results as yours before, and what happens is, when something approaches to the sensor, it turns on, but never turns off, therefore, there's no 0 coming out when there's no activity. I thought this was a good approuch to achieve my goal, which is, when there's activity on the sensor, turn on, else, turn off
thanks

Fernando

fernandossala:
When I first tried the example sketches, I printed every single piece of code I found could be handy to work with, the objects in the array cm [0] and cm [1] where the variables I found most important (all other I just couldn't print), but, again I couldn't do much with them as I have poor code skills, I was looking for some piece of code that could convert whatever I was looking at to something I could do some math with , later I found I was merging the results into one, so I decided I needed to divide the result by two. Ok, not a smart decision, I know...

And with your sketch, I did came to similar results as yours before, and what happens is, when something approaches to the sensor, it turns on, but never turns off, therefore, there's no 0 coming out when there's no activity. I thought this was a good approuch to achieve my goal, which is, when there's activity on the sensor, turn on, else, turn off
thanks

Fernando

Without totally writing the sketch for you (what fun would that be?) I can suggest that you start again with the standard 15 sensor sample sketch that puts the results in an array. Then, analyze the results in the array and decide what you want to do with the data. The distance variables are simply cm[0] and cm[1]. You wouldn't want to add them and divide by two, as that would not give accurate results. Instead, you would need some better logic. For example, maybe one OR the other needs to be a positive value to consider something in range. So, you would do something like:

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.
  if (cm[0] || cm[1]) {  // At least one sensor detected something in range, activate relays.
    digitalWrite (RELE1, HIGH);
    digitalWrite (RELE2, HIGH);
  } else {  // Both sensors can't detect anything, turn relays off.
    digitalWrite (RELE1, LOW);
    digitalWrite (RELE2, LOW);
  }
}

Hope that helps!

Tim

teckel:

fernandossala:
When I first tried the example sketches, I printed every single piece of code I found could be handy to work with, the objects in the array cm [0] and cm [1] where the variables I found most important (all other I just couldn't print), but, again I couldn't do much with them as I have poor code skills, I was looking for some piece of code that could convert whatever I was looking at to something I could do some math with , later I found I was merging the results into one, so I decided I needed to divide the result by two. Ok, not a smart decision, I know...

And with your sketch, I did came to similar results as yours before, and what happens is, when something approaches to the sensor, it turns on, but never turns off, therefore, there's no 0 coming out when there's no activity. I thought this was a good approuch to achieve my goal, which is, when there's activity on the sensor, turn on, else, turn off
thanks

Fernando

Without totally writing the sketch for you (what fun would that be?) I can suggest that you start again with the standard 15 sensor sample sketch that puts the results in an array. Then, analyze the results in the array and decide what you want to do with the data. The distance variables are simply cm[0] and cm[1]. You wouldn't want to add them and divide by two, as that would not give accurate results. Instead, you would need some better logic. For example, maybe one OR the other needs to be a positive value to consider something in range. So, you would do something like:

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.
  if (cm[0] || cm[1]) {  // At least one sensor detected something in range, activate relays.
    digitalWrite (RELE1, HIGH);
    digitalWrite (RELE2, HIGH);
  } else {  // Both sensors can't detect anything, turn relays off.
    digitalWrite (RELE1, LOW);
    digitalWrite (RELE2, LOW);
  }
}




Hope that helps!

Tim

It works like a champion!
Thanks a bunch, next time, i will send pictures and videos of the final project!
Best Regards,
Fernando