Multicolor LED + IR sensor from Jessica Floeh on Vimeo.
Click below for the code:
// Analog pin settings
int sharpIR = 2; // connected to the analog pin 2, analog gnd, analog 5v
// Digital pin settings
int redLED = 9; // full-color LED connected to digital pins 9, 10 and 11
int blueLED = 10; // (Connect cathode to analog 3v3)
int greenLED = 11;
// Values
int sharpVal = 0; // the variable to store the input from the sharp 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 IR adjustment and output
int sens = 3; // Sensitivity threshold, to prevent small changes in
// pot 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
sharpVal = analogRead(sharpIR); // read input pins, convert to 0-255 scale
if (sharpVal< 341)
{
sharpVal= (sharpVal* 3) / 4; //normalize to 0-255
redVal= 255-sharpVal;
greenVal= sharpVal;
blueVal= 1; //blue off
} //end of if
else if (sharpVal< 682)
{
sharpVal= ((sharpVal-341)*3) / 4; //normalize to 0-255
redVal= 1; //red off
greenVal= 255-sharpVal;
blueVal= sharpVal;
} //end of else if
else
{
sharpVal= ((sharpVal-682)*3)/ 4; //normalize to 0-255
redVal= sharpVal;
greenVal= 1; //green off
blueVal= 255-sharpVal;
} //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 (sharpVal > sens ) // If old and new values differ
// above sensitivity threshold
{
if (PRINT) // ...and if the PRINT flag is set...
{
Serial.print("The value of Infrared Sensor is: "); // ...then print the values.
Serial.print(sharpVal);
PRINT = 0;
}
}
else
{
PRINT = 1; // Re-set the flag
}
}
}
Comments
You can follow this conversation by subscribing to the comment feed for this post.