int redPin = 2; int bluePin = 3; int greenPin = 4; int redIn = 2; int greenIn = 3; int blueIn = 4; int redVal; int greenVal; int blueVal; int IRpin = 5; // analog pin for reading the IR sensor void setup() { Serial.begin(9600); // start the serial port redVal = 255; greenVal = 255; blueVal = 255; update(); } // This function updates the LED outputs. void update() { analogWrite(redPin, redVal); analogWrite(greenPin, greenVal); analogWrite(bluePin, blueVal); } // This function updates one of the color variables // either getting brighter or getting dimmer. // It also updates the outputs and delays for 10 milliseconds. void color_morph(int* value, int get_brighter) { for (int i = 0; i < 255; i++) { if (get_brighter) (*value)--; else (*value)++; update(); delay(1); } } void loop() { int r, g, b; float volts = analogRead(IRpin)*0.0048828125; // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3 float distance = 27*pow(volts, -1.10); // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk Serial.println(distance); // print the distance delay(100); // arbitary wait ti0me. if(distance <=30 ){ // start out at black (all off) color_morph(&redVal, 1); // transition to red color_morph(&greenVal, 0); // transition to yellow color_morph(&redVal, 1); // transition to green color_morph(&blueVal, 1); // transition to aqua color_morph(&redVal, 1); // transition to white color_morph(&greenVal, 1); // transition to violet color_morph(&redVal, 1); // transition to blue color_morph(&blueVal, 1); // transition to black (all off) } else if (distance >=20 && distance <=35){ color_morph(&redVal, 0); // transition to red color_morph(&greenVal, 0); // transition to yellow color_morph(&redVal, 1); // transition to green color_morph(&blueVal, 0); // transition to aqua color_morph(&redVal, 0); // transition to white color_morph(&greenVal, 0); // transition to violet color_morph(&redVal, 0); // transition to blue color_morph(&blueVal, 1); // transition to black (all off) } else{ digitalWrite(redPin,HIGH); digitalWrite(greenPin,LOW); } }