Control RGB LED Strip with sharp distance sensor

So, I have managed to control the changing of colours on my strip from this tutorial (http://www.ladyada.net/products/rgbledstrip/#usage)

As well as the use of a Sharp 10-80cm analog distance sensor using this tutorial (http://luckylarry.co.uk/arduino-projects/arduino-using-a-sharp-ir-sensor-for-distance-calculation/)

Now am looking for a way to combine the two together so as that by varying the distance from the sensor changes the colour on the strip eg. No object, white light.... Object halfway, yellow light... Object on surface, colour red... (mere example)

Help, and even code would be greatly appreciated :) Thanks.

It is hard to know how to answer this because basically on one hand it is so very simple and on the other it can be complected.

Basically you have numbers that control the LEDs, so instead of taking those numbers from the sketch as fixed values you take them from your input sensor.
Or you take your input sensor value, and then using if statements decide what happens to the LEDs when the input value is with a certain range sort of like this:-
read sensor into variable val
if(val > some value && val < some other value) { do one thing }
if(val > some other value && val < some different other value) { do another thing }
The && is read as AND
Note this is not exactly code but is close enough.

Okay I see, am rather new to arduino, which would be the easier, and how to I put the sensor reading into the led value??

Do the second.

how to I put the sensor reading into the led value

Did you understand the second method? You don't directly, you do a bit of code depending on what the value is. That's what all the if() statements are about. You send the LED values exactly the same way you are doing it in the first link.

i sort of understand the second bit, so far am able to make one colour with the sensor but that is as far as i can get.

Code i have written:

#define REDPIN 5
#define GREENPIN 6
#define BLUEPIN 3
int IRpin = 1; // analog pin for reading the IR sensor
void setup() {

  • Serial.begin(9600); // start the serial port*

  • pinMode(REDPIN, OUTPUT);*

  • pinMode(GREENPIN, OUTPUT);*

  • pinMode(BLUEPIN, OUTPUT);*
    }
    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 >= 80) {*

  • analogWrite(REDPIN, 255);*

  • analogWrite(GREENPIN, 255);*

  • analogWrite(BLUEPIN, 255);*

  • }*

  • if (distance < 80) analogWrite(GREENPIN, 255);*
    }

And the resultant is white light stays on and wont change to green :confused:

You need to set the blue and red pins to zero.

that still doesnt work, will change from white to one colour, but no others

if (distance >= 80) {
    analogWrite(REDPIN, 255);
    analogWrite(GREENPIN, 255);
    analogWrite(BLUEPIN, 255);
  } else {
    analogWrite(GREENPIN, 255);
    analogWrite(REDPIN, 0);
    analogWrite(BLUEPIN, 0);

}

in doing that it makes the colour red :S

and so far a can get it to change below 80, but how can i make it so the bellow 70 is a different colour again

in doing that it makes the colour red :S

Have you checked your wiring?

but how can i make it so the bellow 70 is a different colour again

You got the colour to change with one comparison; maybe you could put another comparison in, one that tests for a range of 70.

i think so ill double check

yep wires were wrong aha

You got the colour to change with one comparison; maybe you could put another comparison in, one that tests for a range of 70.

how do i go about that??

See reply #7 and extrapolate.

{
 if (distance >= 80) {
    analogWrite(REDPIN, 255);
    analogWrite(GREENPIN, 255);
    analogWrite(BLUEPIN, 255);
  } else {
    analogWrite(GREENPIN, 255);
    analogWrite(REDPIN, 0);
    analogWrite(BLUEPIN, 0);
 }
 if (distance >= 70) {
    analogWrite(REDPIN, 255);
    analogWrite(GREENPIN, 255);
    analogWrite(BLUEPIN, 255);
  } else {
    analogWrite(GREENPIN, 0);
    analogWrite(REDPIN, 255);
    analogWrite(BLUEPIN, 0);

if that is what you meant, all i get now is red??

Is that a real red, or a red LED connected to the green pin?

You need to be careful with cascaded "if"s like you have written - you may cover some ranges more than once.

AWOL: Is that a real red, or a red LED connected to the green pin?

You need to be careful with cascaded "if"s like you have written - you may cover some ranges more than once.

Im using an RGB light strip each one to its own pin

Don’t use any ‘else’ with the if statement and use two comparisons with an and like I said in the first reply.

 if (distance < 80 ) {
    analogWrite(REDPIN, 255);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 0);
  }
 if (distance >= 80 && distance < 150) {
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 255);
    analogWrite(BLUEPIN, 0);
  }
 if (distance >= 150 && distance <230 ) {
    analogWrite(REDPIN, 0);
    analogWrite(GREENPIN, 0);
    analogWrite(BLUEPIN, 255);
  }

and so on, have you got the tune?
so make up the words

and so on, have you got the tune? so make up the words

don't quite get that bit but the code i understand, so i have a 10-80cm sensor, so if i make all the values less than 80 where you have greater it should work the same??

Thank you so much Grumpy_Mike,

i have that working and now i can just play with distance and colour,

do you know of a way to make the sensor more accurate??

do you know of a way to make the sensor more accurate?

Sorry no, it is a miracle it is as accurate as it is.

I think you get better accuracy from an ultrasonic sensor.