Go Down

Topic: Control RGB LED Strip with sharp distance sensor (Read 6 times) previous topic - next topic

Mollison92

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.
"I live life a Quarter Mile at a Time" - Dom Torreto

Grumpy_Mike

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.

Mollison92

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??
"I live life a Quarter Mile at a Time" - Dom Torreto

Grumpy_Mike

Do the second.

Quote
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.

Mollison92

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 :/
"I live life a Quarter Mile at a Time" - Dom Torreto

Henrik67

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

Mollison92

that still doesnt work, will change from white to one colour, but no others
"I live life a Quarter Mile at a Time" - Dom Torreto

AWOL

Code: [Select]
if (distance >= 80) {
    analogWrite(REDPIN, 255);
    analogWrite(GREENPIN, 255);
    analogWrite(BLUEPIN, 255);
  } else {
    analogWrite(GREENPIN, 255);
    analogWrite(REDPIN, 0);
    analogWrite(BLUEPIN, 0);

}
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Mollison92

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
"I live life a Quarter Mile at a Time" - Dom Torreto

AWOL

Quote
in doing that it makes the colour red :S

Have you checked your wiring?

Quote
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Mollison92

"I live life a Quarter Mile at a Time" - Dom Torreto

Mollison92

yep wires were wrong aha

Quote
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??
"I live life a Quarter Mile at a Time" - Dom Torreto

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Mollison92

Code: [Select]
{
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??
"I live life a Quarter Mile at a Time" - Dom Torreto

AWOL

#14
Jul 28, 2011, 02:59 pm Last Edit: Jul 28, 2011, 03:00 pm by AWOL Reason: 1
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up