3 LEDs + IR sensor from Jessica Floeh on Vimeo.
Code in following link:
// Input settings
int IRPin = 2; // potentiometer connected to analog pin 2
int IRval = 0; // variable to store the read value
// Digital pin settings
int aLed = 12; // LEDs connected to digital pins 12
int bLed = 10; //
int cLed = 6;
// Variables
int aVal = 0; // Variables to store the input from the potentiometers
int bVal = 0;
int cVal = 0;
int DEBUG = 1; // Set to 1 to turn on debugging output
int average[100]; // Averaging Code setting
byte counter = 0;
void setup()
{
pinMode(aLed, OUTPUT); // sets the pin as output
pinMode(bLed, OUTPUT); // sets the pin as output
pinMode(cLed, OUTPUT); // sets the pin as output
if (DEBUG) {
Serial.begin(9600); // Open serial communication for reporting
}
}
//Main program
void loop(){
IRval = analogRead(IRPin);
//val = (analogRead(analogPin)-30)*3;
// read the input pin
// the analog output from ir sensor is from about 0 to 3
// the analog read range is about 30-330, when distance below 10 cm, val becomes negative
// convert the range to 0-1024
//Averaging Code start
average[counter] = IRval;
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; //Averaging Code. modified from Dave Millis example
if(IRval > 204){ // first range
digitalWrite (aLed, HIGH);
delay(1000);
digitalWrite (bLed, LOW);
delay(1000);
digitalWrite (cLed, LOW);
delay(1000);
}
else if(IRval > 102){ //second range
digitalWrite (aLed, LOW);
delay(1000);
digitalWrite (bLed, HIGH);
delay(1000);
digitalWrite (cLed, LOW);
delay(1000);
}
else if(IRval > 0){ //third range
digitalWrite (aLed, LOW);
delay(1000);
digitalWrite (bLed, LOW);
delay(1000);
digitalWrite (cLed, HIGH);
delay(1000);
}
// else{ //
// digitalWrite (aLed, LOW);
// delay(1000);
// digitalWrite (bLed, LOW);
// delay(1000);
digitalWrite (cLed, LOW);
delay(10
}
if (DEBUG) { // if we want to read the output
DEBUG+=1;
if(DEBUG>100){ //print every hunderd loops
DEBUG = 1; // reset the counter
Serial.print("IRval:");
Serial.print(IRval);
}
}
analogWrite(aLed, aVal);
// analogWrite(bLed, bVal);
// analogWrite(cLed, cVal); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
}
Comments
You can follow this conversation by subscribing to the comment feed for this post.