Pages: [1] 2 3   Go Down
Author Topic: Control RGB LED Strip with sharp distance sensor  (Read 5786 times)
0 Members and 1 Guest are viewing this topic.
Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Thanks.
Logged

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

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33374
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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??
Logged

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

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33374
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 :/
Logged

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

Malmö, Sweden
Offline Offline
Newbie
*
Karma: 1
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25802
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25802
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i think so ill double check
Logged

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

Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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??
Logged

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25802
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

See reply #7 and extrapolate.
Logged

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

Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
{
 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??
Logged

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25802
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: July 28, 2011, 08:00:57 am by AWOL » Logged

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

Pages: [1] 2 3   Go Up
Jump to: