Newping 3 Ping))) sensor w/ LED Fade

Hey everyone,
So I’m creating a light box where there are 3 seperate super high intensity LEDs each running off a seperate ping))) sensor. There are a total of three and need them to brighten as the person approaches, but do this independently of each other. I have gotten them to read and output to the LEDs but somewhere in the middle the signals are not being sent to their corresponding LED.

Ive not worked with the Newping library before and really need some help.
Here is the code as of now.

#include <NewPing.h>

#define SONAR_NUM     3 // Number or sensors.
#define MAX_DISTANCE 40 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 10 // 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];
unsigned int cm2[SONAR_NUM]; // Where the ping distances are stored.
uint8_t currentSensor = 0;          // Keeps track of which sensor is active.
int  fadeValue, fadeValue2, fadeValue3;
const int ledPin1 = 3;
const int ledPin2 = 5;
const int ledPin3 = 6;

NewPing sonar[SONAR_NUM] = {     // Sensor object array.
  NewPing(4, 4, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(7, 7, MAX_DISTANCE),
  NewPing(8, 8, 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.
    pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
}

void loop() {
  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(); // 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).
    }
  }
  // The rest of your code would 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.
  for (uint8_t i = 0; i < SONAR_NUM; i++) {
    Serial.print(i);
    Serial.print("=");
    Serial.print(cm[i]);
    Serial.print("cm ");
   
 fadeValue = map(cm[i] , 0, 40, 0, 254);
 fadeValue2 = map(cm[i] , 0, 40, 0, 254);
 fadeValue3 = map(cm[i] , 0, 40, 0, 254);


 analogWrite(ledPin1, fadeValue);  // Writes the fadeValue to pin 9
  analogWrite(ledPin2, fadeValue2);
   analogWrite(ledPin3, fadeValue3);


  Serial.println();
}
}

Ive gotten like two hours of sleep and feel like im going crazy trying to figure this out
thanks

HI,

You have the same problem that I had. Basically you need analogWrite. But I think you will need additionally some hardware that can handle more amperage if you are planning on using powerful LED-s.

Study Arrays example.

Look at my topics last page, there is something that should help. http://forum.arduino.cc/index.php?topic=201463.msg1495879#msg1495879

Raimo

but somewhere in the middle the signals are not being sent to their corresponding LED.

Your serial debug output might help you with this.

So this is a revised code, the sensors are all working properly.
However, im still having a problem where the signal outputting to two of the LEDs at the same time and the third doesn’t light up at all no matter what sensor im in front of.
And thoughts on how to tell each led to react to their seperate sensor.

#include <NewPing.h>

#define SONAR_NUM 3 // Number or sensors.
#define MAX_DISTANCE 40 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 10 // 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(4, 4, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(7, 7, MAX_DISTANCE),
  NewPing(8, 8, MAX_DISTANCE),
};

int ledPins[SONAR_NUM] = {
  3, 5, 6 }; // an array of pin numbers to which pwm output is written
int  fadeValue, fadeValue2, fadeValue3;
const int ledPin1 = 3;
const int ledPin2 = 5;
const int ledPin3 = 6;

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.
    pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
  }

  for (uint8_t i = 0; i < SONAR_NUM; i++) { // pwm output pin counter
    pinMode(ledPins[i], OUTPUT);
  }
 
  {               
  // initialize the digital pin as an output.
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
}
 
  Serial.println();
}

void loop()
{
  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(); // 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.
}

// If ping received, set the sensor distance to array.
void echoCheck()
{
  if (sonar[currentSensor].check_timer())
    cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
}

// Sensor ping cycle complete, do something with the results. Location option 1 for code processing

void oneSensorCycle() { // Sensor ping cycle complete, 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 ");
   
 fadeValue = map(cm[i] , 0, 40, 0, 254);
 fadeValue2 = map(cm[i] , 0, 40, 0, 254);
 fadeValue3 = map(cm[i] , 0, 40, 0, 254);


 analogWrite(ledPin1, fadeValue);  // Writes the fadeValue to pin 9
  analogWrite(ledPin2, fadeValue2);
   analogWrite(ledPin3, fadeValue3);


  Serial.println();
  }
}

Is this an RGB LED (with 4 pins)?

If so, what are the values of your dropping resistors?

If not, What colour are your LEDs, and what are the dropping resistor values?

This is the part of code witch counts sensors.

for (uint8_t i = 0; i < SONAR_NUM; i++) { // pwm output pin counter
    pinMode(pwmPins[i], OUTPUT);

Sensors will be triggered/read at the same order you defined them in the beginning of your code:

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

This is where you convert you digital signal to analog and give pxm output pin new values:

analogWrite(pwmPins[i], cm[i]);

This is the place you define you output pin location on the board

int pwmPins[SONAR_NUM] = {
  9, 10 }; // an array of pin numbers to which pwm output is written

I think you need to simplify your code, you are defining the same things many times.

Fade example:

int led = 9;           /* the pin that the LED is attached to <- in your code you have already defined output in the array: int pwmPins[SONAR_NUM] = {
  9, 10 }; // an array of pin numbers to which pwm output is written
*/
int brightness = 0;    // how bright the LED is <- add this
int fadeAmount = 5;    // how many points to fade the LED by  <- add this

// the setup routine runs once when you press reset:
void setup()  {
  // declare pin 9 to be an output:    here you define OUTPUT pins ->  pinMode(pwmPins[i], OUTPUT);
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop()  {
  // set the brightness of pin 9:
  analogWrite(led, brightness);    

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }    
  // wait for 30 milliseconds to see the dimming effect    
  delay(30);                            
}

I copied some of the lines from my own code so you need to replace pwm-> led etc

And thoughts on how to tell each led to react to their seperate sensor.

Study Arrays example

You need to be fading the LEDs where the code says " // Other code that DOESN'T analyze ping results can go here." NOT in the code that deals with polling the ping sensors.

Alright im am super close now… two of them are working independently but the pin 3 led isnt lighting up at all still…
here is my condensed simplified code. what am i missing?

#include <NewPing.h>

#define SONAR_NUM 3 // Number or sensors.
#define MAX_DISTANCE 150 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 10 // 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(4, 4, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(7, 7, MAX_DISTANCE),
  NewPing(8, 8, MAX_DISTANCE),
};

int ledPins[SONAR_NUM] = {
  3, 5, 6 }; // an array of pin numbers to which pwm output is written
int  fadeValue, fadeValue2, fadeValue3;
const int ledPin1 = 3;
const int ledPin2 = 5;
const int ledPin3 = 6;

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.
    pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
  }

 
  Serial.println();
}

void loop()
{
  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(); // 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.
}

// If ping received, set the sensor distance to array.
void echoCheck()
{
  if (sonar[currentSensor].check_timer())
    cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
}

// Sensor ping cycle complete, do something with the results. Location option 1 for code processing

void oneSensorCycle() { // Sensor ping cycle complete, 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 ");
   
 fadeValue = map(cm[i] , 0, 150, 0, 254);
 fadeValue2 = map(cm[i] , 0, 150, 0, 254);
 fadeValue3 = map(cm[i] , 0, 150, 0, 254);


 analogWrite(ledPins[i], cm[i]);


int ledPins[SONAR_NUM] = {
  3, 5, 6 }; // an array of pin numbers to which pwm output is written
int  fadeValue, fadeValue2, fadeValue3;
const int ledPin1 = 3;
const int ledPin2 = 5;
const int ledPin3 = 6;

  Serial.println();
  }
}

Alright im am super close now.

Perhaps if you are using lightyears as a unit of measure.

What is this stuff in oneSensorCycle() for?

int ledPins[SONAR_NUM] = {
  3, 5, 6 }; // an array of pin numbers to which pwm output is written
int  fadeValue, fadeValue2, fadeValue3;
const int ledPin1 = 3;
const int ledPin2 = 5;
const int ledPin3 = 6;
NewPing sonar[SONAR_NUM] = { // Sensor object array.
  NewPing(4, 4, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(7, 7, MAX_DISTANCE),
  NewPing(8, 8, MAX_DISTANCE),
};

Typically different pins are use for the trigger pin and echo pin. Why are you using the same pin?

 fadeValue = map(cm[i] , 0, 150, 0, 254);
 fadeValue2 = map(cm[i] , 0, 150, 0, 254);
 fadeValue3 = map(cm[i] , 0, 150, 0, 254);

What is this for? You never fade anything.

Why are you keeping your serial output from us? Is it safe to assume that it is because you really don't want help?