While changing and tweaking the code, I finally used this one. There are some moments where the green or red LEDs flicker a little bit when the orange one is turned on, maybe it has to do with the values I assign to each of them, but I tried different combination, and in some of them, the orange LED didn't even turned on.
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
/* IR sensor with three colored LEDs
* Jennifer Dopazo - October 2008
*/
// Input settings ----------------------------------------------
int IRPin = 3; // IR sensor connected to analog pin 3
int val = 0; // variable to store the read value
// Digital pin settings ----------------------------------------------
int greenLed = 9; // green LED connected to digital pin 9
int orangeLed = 10; // yellow LED connected to digital pin 10
int redLed = 11; // red LED connected to digital pin 11
// Variables to store the input from the sensor ----------------------------------------------
int greenVal = 0;
int orangeVal = 0;
int redVal = 0;
int DEBUG = 1; // Set to 1 to turn on debugging output
int average[100]; // Averaging Code setting
byte counter = 0;
void setup()
{
pinMode(greenLed, OUTPUT); // sets the pin as output
pinMode(orangeLed, OUTPUT); // sets the pin as output
pinMode(redLed, OUTPUT); // sets the pin as output
}
// Main program ----------------------------------------------
void loop(){
val = analogRead(IRPin);
// Analog Average - Rolling Buffer by Jeff Gray - 2008
average[counter] = val;
byte c;
int total = 0;
for(c = 0;c<100;c++){
total += average[c];
}
int averaged = total / 100;
Serial.println(averaged);
counter = (counter + 1) % 100;
if(val > 180){ // max distance
greenVal = 255; // green LED on
orangeVal = 0; // yellow LED off
redVal = 0; // red LED off
}
else if(val > 75){ // medium distance
greenVal = 0; // green LED off
orangeVal = 255; // yellow LED on
redVal = 0; // red LED off
}
else if(val > 0){ // min distance
greenVal = 0; // green LED off
orangeVal = 0; // yellow LED off
redVal = 255; // red LED on
}
else{
greenVal = 0;
orangeVal = 0;
redVal = 0;
}
}
Comments
You can follow this conversation by subscribing to the comment feed for this post.