Control RGB LED Strip with sharp distance sensor

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.

Grumpy_Mike if I'm correct you an change accuracy like in this tutorial (http://luckylarry.co.uk/arduino-projects/arduino-using-a-sharp-ir-sensor-for-distance-calculation/)

And the data sheet for my specific sensor is (http://sharp-world.com/products/device/lineup/data/pdf/datasheet/gp2y0a21yk_e.pdf)

I modified the code in the tutorial from 65 to 27 and is what I'm using, it says something about how to decide the distance to use buy I don't understand it...

Maybe you do??

This code here creates a colour swirl on a loop:

void loop() {
  int r, g, b;
 
  // fade from blue to violet
  for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  // fade from violet to red
  for (b = 255; b > 0; b--) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  // fade from red to yellow
  for (g = 0; g < 256; g++) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 
  // fade from yellow to green
  for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  // fade from green to teal
  for (b = 0; b < 256; b++) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  // fade from teal to blue
  for (g = 255; g > 0; g--) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 
}

is there a way that i can incorporate the changing colour into my code so as to have a colour changing blend rather than just from say 'blue to green' to more 'blue- light blue - aqua - green) depending on the height?? and also that at that height it wouldn't keep loping just change to and stop??

That web page is just telling you how to get a distance reading from the sensor's input. You don't want that as you are just dealing with the raw numbers so that will give you the maximum amount of distance discrimination it is possible to have. It doesn't alter the accuracy of the reading at all, all it does is put the reading into a number of centimeters. This reading is then (by necessary) much less discriminatory than the raw numbers.

is there a way that i can incorporate the changing colour into my code so as to have a colour changing blend

Yes just fade all three colours at the same time.

Each colour can be represented by a triad, that is three numbers (red, green & blue), so you have an initial number (start colour) and a target number (end colour).

Subtract start from finish (for all three numbers), this gives you the distance or delta for each colour. Then divide this by the number of steps you want to fade with, use a floating point number here because it will almost certainly be less than one, and use that to increment the current value to write out the to each LED for each step of the fade.

Yes just fade all three colours at the same time.

Each colour can be represented by a triad, that is three numbers (red, green & blue), so you have an initial number (start colour) and a target number (end colour).

Subtract start from finish (for all three numbers), this gives you the distance or delta for each colour. Then divide this by the number of steps you want to fade with, use a floating point number here because it will almost certainly be less than one, and use that to increment the current value to write out the to each LED for each step of the fade.

Dont quite understand what you mean, because if i fade all three at the same time wont that just create a constant colour?? for example to make yellow is red 255 and green 255, but to make orange is like red 255 green 127, so if i faded them at the same time i couldn't make orange?

All three LEDs are in the fading loop but each time round the loop you add the delta value to the LED intensity. So if the red's delta value is 0.2 it only actually changes every five times round the loop. Where as is the green's delta value is 2 then it changes by two each time round the loop.

still don't quite understand, a bit of code may help, and also is this going to allow that say from 80 - 20 cm, the colour will start of white and finish at red with a new colour at 5cm increments in between??

This is a function you can add to your code to fade from the current colour to the one passed to it.
It is not tested so may contain errors but it illustrates how to fade colours.

// set up these global variables before the setup()
int currentRed = 0, currentGreen = 0, currentBlue = 0;

void fadeTo(int R, int G, int B, int fadeSteps) {  // fade from the current colour to the one passed to the function
  float deltaR, deltaG, deltaB;
  float tempR, tempG, tempB;
// set up the temp values to the current LED state
  tempR = currentRed;
  tempG = currentGreen;
  tempB = currentBlue;

// calculate the distance between each colour as it is now, and the target colour
  deltaR = ( float(R - currentRed)) / fadeSteps;
  deltaG = ( float(G - currentGreen)) / fadeSteps;
  deltaB = ( float(B - currentBlue)) / fadeSteps;

  // fade to new colour
  for (int i = 0; i < fadeSteps; i++) { 
 // calculate new intermediate colour
   tempR += deltaR;
   tempG += deltaG;
   tempB += deltaB;

  // set these LEDs to the new intermediate colour
    analogWrite(REDPIN, int(tempR));
    analogWrite(GREENPIN, int(tempG));
    analogWrite(BLUEPIN, int(tempB));

    delay(FADESPEED);
  } // end of for loop

// keep the global colour up to date
  currentRed = tempR;
  currentGreen = tempG;
  currentBlue = tempB;

}

Okay, I'll give it a try, so this should all still work with my distances??

so this should all still work with my distances??

No it's a function to add to your sketch to allow you to fade from one colour to another. How you incorporate that into distance is up to you.

If you just use that code without trying to understand what it is doing you will not learn anything, which is the object of the exercise.

hmmmm, okay, i see where you coming from, its for a school project with a dead line hence asking for codes etc as i don't have time to fully write them. although as we go i am starting to work out and understand a bit more.

do you know of a way i can incorporate the colour change into my distances, or am i better of with just going to a colour wheel and finding the number values for each colour i wan't and go that way??

do you know of a way i can incorporate the colour change into my distances

Yes call the function I gave you when you want to fade between two colours!!!!

its for a school project

I hope I get good marks.