Go Down

Topic: HC-SR04 NewPing Help (Read 72 times) previous topic - next topic

KeelinTy

Apr 24, 2018, 08:37 pm Last Edit: Apr 24, 2018, 08:38 pm by KeelinTy
So im using a newping example code to get 4 sensors running. My issue is, Whenever something is within lets say 40cm, I want it to turn on led 1 and 2 and do a tone,


Heres the code so far

Code: [Select]
#include <NewPing.h>

#define led1 11  //Right LED
#define led2 10 // Left LED
 
#define SONAR_NUM     4 // Number or sensors.
#define MAX_DISTANCE 20 // Max distance in cm.
#define PING_INTERVAL 33 // Milliseconds between pings.
 
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(13, 12, MAX_DISTANCE),
  NewPing(9, 8, MAX_DISTANCE),
  NewPing(7, 6, MAX_DISTANCE),
  NewPing(5, 4, MAX_DISTANCE),
};
 
void setup() {
  Serial.begin(115200);
  pingTimer[0] = millis() + 75; // First ping start in ms.
  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;
      if (i == 0 && currentSensor == SONAR_NUM - 1)
        oneSensorCycle(); // Do something with results.
      sonar[currentSensor].timer_stop();
      currentSensor = i;
      cm[currentSensor] = 0;
      sonar[currentSensor].ping_timer(echoCheck);
      
    }
  }
  // The rest of your code would go here.
}
 
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(i);
    Serial.print("=");
    Serial.print(cm[i]);
    Serial.print("cm ");
  
  }
  Serial.println();
  
}



So my question is, what the heck do I do, Ive tried an if statement with a few different things and I just can't get it to work.


slipstick

Within 40cm of all of the sensors or of any one or some combination? How do you plan on making a tone? Some sort of buzzer/speaker? What sort and where is it connected?

If you had posted what you have tried I might have been able to guess some answers.

Steve

KeelinTy

Within 40cm of all of the sensors or of any one or some combination? How do you plan on making a tone? Some sort of buzzer/speaker? What sort and where is it connected?

If you had posted what you have tried I might have been able to guess some answers.

Steve
It would be 40cm on all sensors, for tone I would just use
Code: [Select]
tone(2,1000,100); as I have a piezo speaker on pin 2.

I have the sensors arranged so they create a wider field of view, maybe 130 degrees

Im wanting it so that if anything comes within 40cm on the sensor, the leds will turn on and tone will activate

Kinda like
Code: [Select]
if (distance# < 40 && distance# > 0){  //# is the distance for each sensor, ex distance is for sensor1
digitalWrite(led1, HIGH);                              // and distance1 is for sensor 2.
tone(2,1000,100);
delay(50);
tone(2,1000,100);
delay(20);



I just don't know where the distance is stored in the code that will work with something like that.

The distance of each sensor is stored in an array and im just so confused with this

I would like to have this working by tomorrow

slipstick

You really do need to work out what your code is doing. You're always going to have problems if you use code with out understanding it. In this case the distances are held in the cm[] array. cm[0] is distance for sensor 1, cm[1] for sensor 2 etc. (array numbering always starts at 0 ).

So is that the missing bit of information you needed? There's a good write-up on arrays at https://www.arduino.cc/reference/en/language/variables/data-types/array/

Steve

Go Up