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();
}
}
