/* FUN WITH IR, SMOOTHING, MAPPING AND COLOR!!!! smoothing and mapping used to blend between R,G, and B using a tri-color LED (or three different LEDs). - joe saavedra, cecilia elguero, kirsten halterman */ #define NUMREADINGS 30 int readings[NUMREADINGS]; // the readings from the analog input int index = 0; // the index of the current reading int total = 0; // the running total int average = 0; // the average int inputPin = 0; // SHARP IR SENSOR int ledPinR = 11; // VARIOUS LEGS FOR TRI COLOR LED int ledPinG = 10; // COULD ALSO BE 3 DIFFERENT LEDs int ledPinB = 9; void setup() { Serial.begin(9600); // initialize serial communication with computer for (int i = 0; i < NUMREADINGS; i++) readings[i] = 0; // initialize all the readings to 0 } void loop() { total -= readings[index]; // subtract the last reading readings[index] = analogRead(inputPin); // read from the sensor total += readings[index]; // add the reading to the total index = (index + 1); // advance to the next index if (index >= NUMREADINGS){ // if we're at the end of the array... index = 0; // ...wrap around to the beginning } average = total / NUMREADINGS; // calculate the average if (average < 6){ // anything lower than 6, is now ZERO so LED goes OFF average = 0; } Serial.println(average); // send it to the computer (as ASCII digits) // Divide the sensor's range into >>perceptually<< correct proportions among each LED pin // mapping is used to scale each range (which is different) to 0 - 255 if (var < 50){ analogWrite(ledPinR, map(average,0,50,0,255)); // off --> red analogWrite(ledPinG, 0); analogWrite(ledPinB, 0);} if (var >= 50 && var <= 150){ // red --> green analogWrite(ledPinR, map(average,50,150,255,0)); analogWrite(ledPinG, map(average,50,150,0,255)); analogWrite(ledPinB, 0);} if (var > 150){ // green --> blue analogWrite(ledPinR, 0); analogWrite(ledPinG, map(average,150,600,255,0)); analogWrite(ledPinB, map(average,150,600,0,255));} }