Controlling 3 Leds w/ 3 ping sensors

Hello,
So ive been working with 3 high power LEDs and contructed a control program that runs the same fade to all 3.
However, i all 3 to function differently. They are run with NEWping and im not sure if its possible to have led pin 9 to get brighter as someone approaches. led pin 5 to just light up, and led pin 6 to dim upon approach.
I’d really like to accomplish this without having to buy 2 more arduino unos

also the lights are pretty glitchy as in they are on then every once and a while they will blink. not sureif thats fixable either.

here is the code

#include <NewPing.h>

#define SONAR_NUM 3 // Number or sensors.
#define MAX_DISTANCE 130 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 29 // 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] = {
  9, 5, 6 }; // an array of pin numbers to which pwm output is written
int  fadeValue, fadeValue2, fadeValue3;
const int ledPin1 = 9;
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, 2000);
 fadeValue2 = map(cm[i] , 0, 150, 0, 2000);
 fadeValue3 = map(cm[i] , 0, 150, 0, 2000);


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


  Serial.println();
  }
}

I see from your post count that you are new. It is customary when posting for technical help here on the forum to include links to the vendors of products that are not arduino microcontrollers. There are just too many peripherals and devices that can be interfaced to a uC to expect us to be familiar with all of them If you are asking a question about one of the many arduino microcontrollers then there is no need to post datasheets or links because we are already familiar with those. NewPing however, is not an arduino microcontroller so you need to post a link to where you purchased it and/or post any datasheet for said device as well as a link to the library which you are obviously using. In your case, you have posted code that references distances related to the sonar operation but have not provided a diagram that shows the location and distances between your sensor and "someone" in your statement:

They are run with NEWping and im not sure if its possible to have led pin 9 to get brighter as someone approaches.

This question is not possible to answer without knowing where this "someone" is (in cm) in relation to your sensor which has a max distance of 130 cm (4 feet, 3.18 inches). Please make a drawing that shows the relationships (distances) between said people and sensor. In addition, your SITREP (Mil-Speak for Situation Report) is a little on the vague side:

However, i all 3 to function differently. They are run with NEWping and im not sure if its possible to have led pin 9 to get brighter as someone approaches. led pin 5 to just light up, and led pin 6 to dim upon approach. I'd really like to accomplish this without having to buy 2 more arduino unos

also the lights are pretty glitchy as in they are on then every once and a while they will blink. not sureif thats fixable either.

Can you please perform the standard Calibration procedure for your sensor and report the results. (this means leave "someone" out of the report and just tell us what are the results of a standard calibration test of the sensors using ping pong paddles or something of that shape that would guarantee a return signal . Once we know if the sensor is working properly , as advertised in the datasheet , then we can address the issue of whether or not your application is within the allowable specs of the device.

Im currently away from the device so i can’t provide the serial port info but i assure you they are functioning properly. That was the first thing i checked they read the correct distance.

Library for NewPing))) : http://playground.arduino.cc/Code/NewPing#.UzOAUxb0ihM

The controller is an Arduino Uno

The ping sensor is from parallax : http://www.parallax.com/product/28015

Attached is a diagram of the setup the distance of approach is the same for all 3 faces.

diagram.png

Fine, so they are working . But you don’t see a problem with this?

#define MAX_DISTANCE 130 // Maximum distance (in cm) to ping.

(see dimensions in drawing. )
The drawing does not indicate how close the person gets to the sensor.

That was the first thing i checked they read the correct distance.

Do you have any data ? (Serial Monitor capture file ?) (Distance test parameters ? (how you tested it?)

diagram.png

At this point in time im not particularly worried about the glitches until i can figure out how to separate the operation of the code. For all intensive purposes lets say the range of the sensor is 150 cm to lets say 20cm. i understand that basic problem with the code that it dictates the max distance is at 130, thats a simple fix changing it to 150 it has no effect on the operation of the mechanism. Most of what im trying to get out of this is making the function of each of the three leds/ping sensors respond to the interaction of a person differently. I'm not sure how to dictate within one code that each needs to react to the ping sensor data differently.

They are run with NEWping and im not sure if its possible to have led pin 9 to get brighter as someone approaches. led pin 5 to just light up, and led pin 6 to dim upon approach. I'd really like to accomplish this without having to buy 2 more arduino unos also the lights are pretty glitchy as in they are on then every once and a while they will blink. not sureif thats fixable either.

So the code you have is not the code you want ? Is that what you are saying ? So you modified the code from library link to run 3 sensors instead of 15 , but that's as far as you have gone and now you want to turn on specific led under specific circumstances , is that it ?

Yes that is a good way to say it. my biggest problem since im not a CS major is understanding how to get there from here. I know im way far behind in understanding the coding of an arduino. The current code treats all of the LEDs in an array under the same output. Which is me saying that in the current coding the LEDS each fade Bright to dim but only when a person is in front of the individual sensors. So Bright when a person first enters the ping range to dim as they get closer to the sensor. I guess i would like a sample coding or advice on somehow separating the response of each of the 3 LEDS. So one being bright to dim one turning on or off and one dim to bright.

So do you understand any of the code ? Do you know where the leds are being written ? Do you know how to use a SWITCH CASE statement to execute some code based on the value of the PWM signal written to the leds ? Do you know how to use an IF statement to do the same thing ? Do you know where to put these statements ?

yes i do understand why everything is where it is at the moment. However when it comes to the Switch case and possible if statement to control then pwm being written to the less i wouldn't know how to do that. I figure the location of said statements should be in the analog write section.

Attached is a program I wrote for a keypad that uses SWITCH CASE.
Here is the CASE part of the program:

volatile int Key=0;

 /* Output the test message to the LCD */
   switch(Key)
       
                {
                  case 0:
                    lcd.setCursor(0,0); 
                    lcd.print("CMD-0             ");
                    lcd.setCursor(0,1); 
                    lcd.print("Do CMD-0           ");
                  break;

                  case 1:
                    lcd.setCursor(0,0); 
                    lcd.print("CMD-1        ");
                    lcd.setCursor(0,1); 
                    lcd.print("Do CMD-1      ");
                  break; 
                  case 2:
                   lcd.setCursor(0,0); 
                    lcd.print("CMD-2      ");
                    lcd.setCursor(0,1); 
                    lcd.print("Do CMD-2          ");
                  break;

                  case 3:
                    lcd.setCursor(0,0); 
                    lcd.print("CMD-3          ");
                    lcd.setCursor(0,1); 
                    lcd.print("Do CMD-3         ");
                     break;  
                  case 4:
                    lcd.setCursor(0,0); 
                    lcd.print("CMD-4           ");
                    lcd.setCursor(0,1); 
                    lcd.print("Do CMD-4        ");
                      break; 
                  case 5:
                   lcd.setCursor(0,0); 
                    lcd.print("CMD-5          ");
                    lcd.setCursor(0,1);  
                    lcd.print("Do CMD-5       ");
                  break;		

                  case 6:
                    lcd.setCursor(0,0); 
                    lcd.print("CMD-6        ");
                    lcd.setCursor(0,1); 
                    lcd.print("Do CMD-6       ");
                  break;

                  case 7:
                    lcd.setCursor(0,0); 
                    lcd.print("CMD-7         ");
                    lcd.setCursor(0,1); 
                    lcd.print("Do CMD-7       ");
                  break;
                   case 8:
                    lcd.setCursor(0,0); 
                    lcd.print("CMD-8        ");
                    lcd.setCursor(0,1); 
                    lcd.print("Do CMD-8      ");
                  break;
                   case 9:
                    lcd.setCursor(0,0); 
                    lcd.print("CMD-9        ");
                    lcd.setCursor(0,1); 
                    lcd.print("Do CMD-9      ");
                  break;
                   case 10:
                    lcd.setCursor(0,0); 
                    lcd.print("CMD-10       ");
                    lcd.setCursor(0,1); 
                    lcd.print("Do CMD-10    ");
                  break;
                   case 11:
                    lcd.setCursor(0,0); 
                    lcd.print("CMD-11       ");
                    lcd.setCursor(0,1); 
                    lcd.print("Do CMD-11     ");
                  break;
                   case 12:
                    lcd.setCursor(0,0); 
                    lcd.print("CMD-12      ");
                    lcd.setCursor(0,1); 
                    lcd.print("Do CMD-12    ");
                  break;
                   case 13:
                    lcd.setCursor(0,0); 
                    lcd.print("CMD-13       ");
                    lcd.setCursor(0,1); 
                    lcd.print("Do CMD-13      ");
                  break;
                   case 14:
                    lcd.setCursor(0,0); 
                    lcd.print("CMD-14      ");
                    lcd.setCursor(0,1); 
                    lcd.print("Do CMD-14    ");
                  break;
                   case 15:
                    lcd.setCursor(0,0); 
                    lcd.print("CMD-15      ");
                    lcd.setCursor(0,1); 
                    lcd.print("Do CMD-15     ");
                  break;
                  
                 }

As you can see , there is a variable named Key. There 16 possibles cases (no surprise since it is a 4x4 keypad).
There is an Interrupt Service Routine (ISR) that monitors the Data Available output of the 74c922 keypad encoder ic. When you press the keypad , the chip detects it, reads it , encodes it into DCBA and raises DA line, which is connected to the interrupt HW pin which calls the service routine which reads the value and stores it in the variable “Key” . The CASE statement uses the statement "SWITCH(variable) which is acted upon by the CASE [integer value of variable]: so if for example , the value of the variable used = 2, then only the CASE[2]: code will be executed.
Check out the tutorial example here :

The syntax is straightforward so you shouldn’t have any problems. All you need to do is use a variable you have defined for the PWM , like fadevalue:

They are run with NEWping and im not sure if its possible to have led pin 9 to get brighter as someone approaches.

int  fadeValue, fadeValue2, fadeValue3;
switch(fadevalue)
        {
             case 30:
                  Serial.print("Help Mr. Bill ! someone is coming !");
                  // other code here
                  analogWrite(ledPins[i], cm[i]);
              break;
          case 80:
                  Serial.print("Help Mr. Bill ! they're getting closer !");
                    // other code here
              break;
          case 100:
                  Serial.print("OMG ! They're almost on top of me !");
                    // other code here
                   int ledPins[SONAR_NUM] = {
                                                                          9, 5, 6 };              // an array of pin numbers to which pwm output is written
                                                                          int  fadeValue, fadeValue2, fadeValue3;
                                                                          const int ledPin1 = 9;
                                                                          const int ledPin2 = 5;
                                                                          const int ledPin3 = 6;
                                                                          Serial.println();
                                                                      }
              break;
          case 150:
                  Serial.print("Lucy ! I'm home !");
                    // other code here
              break;
       }

I copy and pasted some of your led code into the CASE statement. I don’t know if those are correct but you can change it if needed.
You just have to put the case code somewhere where the variable has the data you want to write (where “i” contains the value
you want to monitor)

Matrix_4x4_16_key_encoder_LCD_CASE_ok.ino (4.96 KB)