IR sensor + 3 color LEDs from maze on Vimeo.
I didn't use the average code, actually I didn't know there's an average code until Yury mentioned about it.
Somehow my LEDs wouldn't flicker a lot.
So here is my code. It is quite simple.
Basically I just change the on and off status of each LED in different ranges.
------------------------------------------------------------------------------
/*
*New Arduino hooked-up to 3 different Super-Bright LEDs.
*Program the 3 LEDs to turn on/off individually depending on distance of hand.
*Tzumei M. Hsiao
*/
// Analog pin settings
int irIn = 3; // the IR sensor connected to the analog pin 3
// Digital pin settings
int redLED = 9; // LEDs connected to digital pins 9, 10 and 11
int blueLED = 10; // (Connect cathodes to digital ground)
int greenLED = 11;
// Values
int irVal = 0; // the variable to store the input from the IR sensor
int redVal = 0; // the variable to store the value of red LED
int greenVal = 0; // the variable to store the value of green LED
int blueVal = 0; // the variable to store the value of blue LED
// Variables for comparing values between loops
int i = 0; // Loop counter
int wait = (1000); // Delay between most recent pot adjustment and output
int sens = 3; // Sensitivity threshold, to prevent small changes in
// IR sensor values from triggering false reporting
// FLAGS
int PRINT = 1; // Set to 1 to output values
void setup()
{
pinMode(redLED, OUTPUT); // sets the digital pins as output
pinMode(blueLED, OUTPUT);
pinMode(greenLED, OUTPUT);
Serial.begin(9600); // Open serial communication for reporting
}
void loop()
{
i += 1; // Count loop
irVal = analogRead(irIn); // read input pins, convert to 0-255 scale
if (irVal < 300)//if (irVal<341)
{
redVal= 255;
greenVal= 1;
blueVal= 1;
}//end of if
else if (irVal< 600)//else if (irVal< 682)
{
redVal= 1;
greenVal= 1;
blueVal= 255;
}//end of else if
else
{
redVal= 1;
greenVal= 255;
blueVal= 1;
}//end of else
analogWrite(redLED, redVal); // Send the new value to LEDs
analogWrite(blueLED, blueVal);
analogWrite(greenLED, greenVal);
if (i % wait == 0) // If enough time has passed...
{
if ( irVal > sens ) // If old and new values differ
// above sensitivity threshold
{
if (PRINT) // ...and if the PRINT flag is set...
{
Serial.print("The value of IR sensor: "); // ...then print the values.
Serial.print(irVal);
PRINT = 0;
}
}
else
{
PRINT = 1; // Re-set the flag
}
}
}
Comments