I need help to complete for coding in rgb led strip

my project is to create a distance sensor device that can change 6 colours from different distance
and make it stay for 5 seconds on the least distance
without using the delay command
greatly thanks for any help as i am confuse with millis() and rgb and setting the distance code
distance <60 red colour
distance < 50 pink colour
distance <40 yellow colour
distance < 30 purple colour
distance <20 orange
distance < 10 green colour

i am very new to arduino
i am using arduino uno
with dfr0274 rgb driver
a rgb 12v strip
a Adafruit_VL53L0X laser sensor

#include "Adafruit_VL53L0X.h"

Adafruit_VL53L0X lox = Adafruit_VL53L0X();

int RedPin = 10;      //Arduino driving pin for Red
int GreenPin = 11;    //Arduino driving pin for Green
int BluePin = 9;      //Arduino driving pin for Blue


void setColor(int red, int green, int blue)
{
  analogWrite(RedPin, red);
  analogWrite(GreenPin, green);
  analogWrite(BluePin, blue);  

}



void setup() {

  pinMode(RedPin, OUTPUT);    //Init Arduino driving pins
  pinMode(GreenPin, OUTPUT);
  pinMode(BluePin, OUTPUT);  
  Serial.begin(115200);

  // wait until serial port opens for native USB devices
  while (! Serial) {
    delay(1);
  }
  
  Serial.println("Adafruit VL53L0X test");
  if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while(1);
  }
  // power 
  Serial.println(F("VL53L0X API Simple Ranging example\n\n")); 
}


void loop() {
  VL53L0X_RangingMeasurementData_t measure;
    
  Serial.print("Reading a measurement... ");
  lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!

  if (measure.RangeStatus != 4) {  // phase failures have incorrect data
    Serial.print("Distance (mm): "); Serial.println(measure.RangeMilliMeter);
  } else {
    Serial.println(" out of range ");
  }
    
  delay(100);
}
  1. measure the distance and print it out to the Serial Monitor
    when that works

  2. hard code distance and make sure you get color you want

  3. combine 1 and 2

.

You could use an array to hold your distance thresholds and colours. This will stop your code from becoming longer than it needs to be:

const byte colourDistance[6][4] = {
  {60, 255, 0, 0}, //red colour
  {50, 255, 224, 224}, //pink colour 
  {40, 255, 255, 0}, // yellow colour
  {30, 255, 0, 255}, // purple colour
  {20, 255, 128, 0}, // orange
  {10, 0, 255, 0} // green colour
};

thanks i manage to get make this but i not sure how to apply your code

and i got 2 problem now my led is not bright enough as i do not know how to control
and the second problem is i need to let the light with the closest distance to stay

#include "Adafruit_VL53L0X.h"
int BluePin = 9;
int RedPin = 10;
int GreenPin = 11;
boolean ledState; // on or off
unsigned long ledTime; // when leds were turned on
unsigned long interval = 5000; // burn time
int red;
int green;
int blue;

Adafruit_VL53L0X lox = Adafruit_VL53L0X();



  
void setup() {
  Serial.begin(115200);
  pinMode(BluePin, OUTPUT);
  pinMode(RedPin, OUTPUT);
  pinMode(GreenPin, OUTPUT);

  // wait until serial port opens for native USB devices
  while (! Serial) {
    delay(1);
  }
  
  Serial.println("Adafruit VL53L0X test");
  if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while(1);
  }
  // power 
  Serial.println(F("VL53L0X API Simple Ranging example\n\n")); 
}



void loop() {
  VL53L0X_RangingMeasurementData_t measure;
    
  Serial.print("Reading a measurement... ");
  lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!

  if (measure.RangeStatus != 4) {  // phase failures have incorrect data
    Serial.print("Distance (mm): "); 
    Serial.println(measure.RangeMilliMeter);
     
  
  
  
   
                        
red  = (measure.RangeMilliMeter<100);
blue = (measure.RangeMilliMeter<200) ;
green = (measure.RangeMilliMeter<300); 
 analogWrite(RedPin, red);
  analogWrite(GreenPin, green);
  analogWrite(BluePin, blue);  

 
 ledState = true; // remember led(s) are on
    ledTime = millis(); // note the time
    }
    if (ledState && millis() - ledTime > interval) { // if a led is on and time is up
    analogWrite(GreenPin, LOW);
    analogWrite(RedPin, LOW);
    analogWrite(GreenPin, LOW);
    ledState = false; // leds are off
  
    }
  
  else {
    Serial.println(" out of range ");
  }

    
  delay(1);
}

Your brightness problem is because of this:

measure.RangeMilliMeter < 100

That is a boolean expression. It is either true or false. In C language, false is represented by 0 and true is represented by 1.

So in this code:

    red  = (measure.RangeMilliMeter < 100);
    ...
    analogWrite(RedPin, red);

you are setting the analogWrite value to 0 or 1. But the full range is 0 to 255. So 1 is going to be very dim!

Try is instead:

 red  = (measure.RangeMilliMeter < 100 ? 0 : 255);

Your second problem, I think, is that you have not really though about how the sensor works and how to deal with that.

The sensor does not measure the closest approach of an object and output the distance at the closest point. It measures the instantaneous distance to an object. If an object is moving towards the sensor, the sensor will measure the distance many times with a decreasing value. If the object moves away again, many readings will be made and those readings will increase.

Your sketch will need to detect the closest distance. It will also need to keep the time at which that measurement was made.

If the next reading is closer still, then a new closest distance is recorded and the time updates.

But if the next reading is further away, then it can be ignored

When 5 seconds have passed since the closest measurement, then the closest measurement can reset to some high value.

thanks paul i have don it with the ultrasonic but i replace it with laser and rgb
thats why i am not sure with the coding

const byte trigPin = 7;
const byte echoPin = 8;
const byte led1 = 3;
const byte led2 = 4;
const byte led3 = 5;
const byte led4 = 6;
unsigned long duration, distance;
boolean printed; // for 'out of range'
boolean ledState; // on or off
unsigned long ledTime; // when leds were turned on
unsigned long interval = 5000; // burn time

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration / 58;
  if (distance > 30 || distance <= 0) {
    if (!printed) { // if not printed
      Serial.println(F("Out of range"));
      printed = true; // print once
    }
  }
  else { // if in range
    Serial.print(F("Distance is "));
    Serial.print(distance);
    Serial.println(F(" cm"));
    if (distance < 25) digitalWrite(led4, HIGH);
    if (distance < 20) digitalWrite(led3, HIGH);
    if (distance < 15) digitalWrite(led2, HIGH);
    if (distance < 10) digitalWrite(led1, HIGH);
    ledState = true; // remember led(s) are on
    ledTime = millis(); // note the time
    printed = false; // enable printing 'out of range'
  }
  if (ledState && millis() - ledTime > interval) { // if a led is on and time is up
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
    ledState = false; // leds are off
  }

}

i manage to get it done for the brightness and colour but i still cannot make the correct light to stop and stay for the nearest distance

#include "Adafruit_VL53L0X.h"
int BluePin = 9;
int RedPin = 10;
int GreenPin = 11;
boolean ledState; // on or off
unsigned long ledTime; // when leds were turned on
unsigned long interval = 5000; // burn time
int red  = 0;
int green  =0;
int blue =0;

Adafruit_VL53L0X lox = Adafruit_VL53L0X();


void setColor(int red, int green, int blue)
{
  analogWrite(RedPin, red);
  analogWrite(GreenPin, green);
  analogWrite(BluePin, blue);

}
void setup() {
  Serial.begin(115200);
  pinMode(BluePin, OUTPUT);
  pinMode(RedPin, OUTPUT);
  pinMode(GreenPin, OUTPUT);

   

  // wait until serial port opens for native USB devices
  while (! Serial) {
    delay(1);
  }
  
  Serial.println("Adafruit VL53L0X test");
  if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while(1);
  }
  // power 
  Serial.println(F("VL53L0X API Simple Ranging example\n\n")); 
}



void loop() {
  VL53L0X_RangingMeasurementData_t measure;
    
  Serial.print("Reading a measurement... ");
  lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!

  if (measure.RangeStatus != 4) {  // phase failures have incorrect data
    Serial.print("Distance (mm): "); 
    Serial.println(measure.RangeMilliMeter);
     
  
  
  
   
                        
if (measure.RangeMilliMeter <300)

    { setColor(255, 0, 0);};
    if (measure.RangeMilliMeter < 250)

  {setColor(80, 80, 0);};
    
if(measure.RangeMilliMeter < 200 )

  {setColor(0, 0, 255);};
  if (measure.RangeMilliMeter < 150)

  {setColor(0, 80, 80);};
  
if (measure.RangeMilliMeter < 100)

  {setColor(0, 255, 0);};

  if (measure.RangeMilliMeter < 50)

  {setColor(120, 0, 120);};

 ledState = true; // remember led(s) are on
    ledTime = millis(); // note the time
    }
    if (ledState && millis() - ledTime > interval) { // if a led is on and time is up
    {
      setColor(0, 0, 0);
    
    };
    ledState = false; // leds are off
  
    }
  
  else {
    Serial.println(" out of range ");
  }}

i want the sensor to detect non stop
but when some thing pass by it will light up different colour according to the nearest distance it has reach
after out of range for 5 seconds it will turn off the led only

Thanks for using code tags, but could you post the code again, having done an Auto-Format (Tools menu)?

what is a auto format?

stevelee:
what is a auto format?

Why not try it and see?

PaulRB:
Thanks for using code tags

My bet is he just used "copy for Forum" which is now fixed to automatically add code tags to the code.

ok i have it auto format already

#include "Adafruit_VL53L0X.h"
int BluePin = 9;
int RedPin = 10;
int GreenPin = 11;
boolean ledState; // on or off
unsigned long ledTime; // when leds were turned on
unsigned long interval = 5000; // burn time
int red  = 0;
int green  =0;
int blue =0;

Adafruit_VL53L0X lox = Adafruit_VL53L0X();


void setColor(int red, int green, int blue)
{
  analogWrite(RedPin, red);
  analogWrite(GreenPin, green);
  analogWrite(BluePin, blue);

}
void setup() {
  Serial.begin(115200);
  pinMode(BluePin, OUTPUT);
  pinMode(RedPin, OUTPUT);
  pinMode(GreenPin, OUTPUT);

   

  // wait until serial port opens for native USB devices
  while (! Serial) {
    delay(1);
  }
  
  Serial.println("Adafruit VL53L0X test");
  if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while(1);
  }
  // power 
  Serial.println(F("VL53L0X API Simple Ranging example\n\n")); 
}



void loop() {
  VL53L0X_RangingMeasurementData_t measure;
    
  Serial.print("Reading a measurement... ");
  lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!

  if (measure.RangeStatus != 4) {  // phase failures have incorrect data
    Serial.print("Distance (mm): "); 
    Serial.println(measure.RangeMilliMeter);
if (measure.RangeMilliMeter <300 )

    { setColor(255, 0, 0);};//red
    if (measure.RangeMilliMeter < 250)

  {setColor(255, 255, 0);};//yellow
    
if(measure.RangeMilliMeter < 200 )

  {setColor(80, 0, 80);};//purple
  if (measure.RangeMilliMeter < 150)

  {setColor(80,80,80);};
  
if (measure.RangeMilliMeter < 100)

  {setColor(0,0,255);};//blue

  if (measure.RangeMilliMeter < 50)

  {setColor(0,255,0);};//green

 ledState = true; // remember led(s) are on
    ledTime = millis(); // note the time
    }
    if (ledState && millis() - ledTime > interval) { // if a led is on and time is up
    {
      setColor(0, 0, 0);
    
    };
    ledState = false; // leds are off
  
    }
  
  else {
    Serial.println(" out of range ");
  }}

No, you have not done Auto Format. Try again.

This looks like a mistake to me:

    if (ledState && millis() - ledTime > interval) { // if a led is on and time is up
    {
      setColor(0, 0, 0);
    
    };
    ledState = false; // leds are off

Should it be

    if (ledState && millis() - ledTime > interval) { // if a led is on and time is up
    {
      setColor(0, 0, 0);
      ledState = false; // leds are off
    };

i just did auto format again but it say no changes necessary for auto format
the code you show me as wrong i need to manually insert to make it right

#include "Adafruit_VL53L0X.h"
int BluePin = 9;
int RedPin = 10;
int GreenPin = 11;
boolean ledState; // on or off
unsigned long ledTime; // when leds were turned on
unsigned long interval = 5000; // burn time
int red  = 0;
int green  = 0;
int blue = 0;

Adafruit_VL53L0X lox = Adafruit_VL53L0X();


void setColor(int red, int green, int blue)
{
  analogWrite(RedPin, red);
  analogWrite(GreenPin, green);
  analogWrite(BluePin, blue);

}
void setup() {
  Serial.begin(115200);
  pinMode(BluePin, OUTPUT);
  pinMode(RedPin, OUTPUT);
  pinMode(GreenPin, OUTPUT);



  // wait until serial port opens for native USB devices
  while (! Serial) {
    delay(1);
  }

  Serial.println("Adafruit VL53L0X test");
  if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while (1);
  }
  // power
  Serial.println(F("VL53L0X API Simple Ranging example\n\n"));
}



void loop() {
  VL53L0X_RangingMeasurementData_t measure;

  Serial.print("Reading a measurement... ");
  lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!

  if (measure.RangeStatus != 4) {  // phase failures have incorrect data
    Serial.print("Distance (mm): ");
    Serial.println(measure.RangeMilliMeter);
    if (measure.RangeMilliMeter < 300 )

    {
      setColor(255, 0, 0);
    };//red
    if (measure.RangeMilliMeter < 250)

    {
      setColor(255, 255, 0);
    };//yellow

    if (measure.RangeMilliMeter < 200 )

    {
      setColor(80, 0, 80);
    };//purple
    if (measure.RangeMilliMeter < 150)

    {
      setColor(80, 80, 80);
    };

    if (measure.RangeMilliMeter < 100)

    {
      setColor(0, 0, 255);
    };//blue

    if (measure.RangeMilliMeter < 50)

    {
      setColor(0, 255, 0);
    };//green

    ledState = true; // remember led(s) are on
    ledTime = millis(); // note the time
  }
  if (ledState && millis() - ledTime > interval) { // if a led is on and time is up
    {
      setColor(0, 0, 0);
      ledState = false; // leds are off

    };

  }

  else {
    Serial.println(" out of range ");
  }
}

i need something like
if

  [code]  if (measure.RangeMilliMeter < 50)

    {
      setColor(0, 255, 0);
    };//green

happens ignore

 if (measure.RangeMilliMeter < 300 )

    {
      setColor(255, 0, 0);
    };//red
    if (measure.RangeMilliMeter < 250)

    {
      setColor(255, 255, 0);
    };//yellow

    if (measure.RangeMilliMeter < 200 )

    {
      setColor(80, 0, 80);
    };//purple
    if (measure.RangeMilliMeter < 150)

    {
      setColor(80, 80, 80);
    };

    if (measure.RangeMilliMeter < 100)

    {
      setColor(0, 0, 255);
    };//blue

if

 if (measure.RangeMilliMeter < 100)

    {
      setColor(0, 0, 255);
    };//blue

ignore

(measure.RangeMilliMeter < 300 )

    {
      setColor(255, 0, 0);
    };//red
    if (measure.RangeMilliMeter < 250)

    {
      setColor(255, 255, 0);
    };//yellow

    if (measure.RangeMilliMeter < 200 )

    {
      setColor(80, 0, 80);
    };//purple
    if (measure.RangeMilliMeter < 150)

    {
      setColor(80, 80, 80);
    };

ignore for 5 seconds until led are off
but i got no idea how to make it work

i just did auto format again but it say no changes necessary for auto format
the code you show me as wrong i need to manually insert to make it right

Auto formatting does not change the code, it just changes the layout so it is not confusing.

Yes any change to the code has to be done by you.

The code structure you want is the if .... else if...... else if .....

what i need is to let the led with the nearest distance to hold light if within any other nearer distance occur it will light up and hold
any one can help me how to ignore

 if (measure.RangeMilliMeter < 100 && measure.RangeMilliMeter > 51)

    {
      setColor(0, 0, 255);
    };//blue
    
    if (measure.RangeMilliMeter < 150 && measure.RangeMilliMeter > 101 )

    {
      setColor(80, 80, 80);
    };

  

    if (measure.RangeMilliMeter < 200 && measure.RangeMilliMeter > 151 )

    {
      setColor(80, 0, 80);
    };//purple
  
     if (measure.RangeMilliMeter < 250 && measure.RangeMilliMeter > 201)

    {
      setColor(255, 255, 0);
    };//yellow

   if (measure.RangeMilliMeter < 300 && measure.RangeMilliMeter > 251)

    {
      setColor(255, 0, 0);
    };//red

if

if (measure.RangeMilliMeter < 50 && measure.RangeMilliMeter > 20)

    {
      setColor(0, 255, 0);
    };//green

happens

#include "Adafruit_VL53L0X.h"
int BluePin = 9;
int RedPin = 10;
int GreenPin = 11;
boolean ledState; // on or off
unsigned long ledTime; // when leds were turned on
unsigned long interval = 5000; // burn time
int red  = 0;
int green  = 0;
int blue = 0;

Adafruit_VL53L0X lox = Adafruit_VL53L0X();


void setColor(int red, int green, int blue)
{
  analogWrite(RedPin, red);
  analogWrite(GreenPin, green);
  analogWrite(BluePin, blue);

}
void setup() {
  Serial.begin(115200);
  pinMode(BluePin, OUTPUT);
  pinMode(RedPin, OUTPUT);
  pinMode(GreenPin, OUTPUT);



  // wait until serial port opens for native USB devices
  while (! Serial) {
    delay(1);
  }

  Serial.println("Adafruit VL53L0X test");
  if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while (1);
  }
  // power
  Serial.println(F("VL53L0X API Simple Ranging example\n\n"));
}



void loop() {
  VL53L0X_RangingMeasurementData_t measure;

  Serial.print("Reading a measurement... ");
  lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!

  if (measure.RangeStatus != 4) {  // phase failures have incorrect data
    Serial.print("Distance (mm): ");
    Serial.println(measure.RangeMilliMeter);
    if (measure.RangeMilliMeter < 50 && measure.RangeMilliMeter > 20)

    {
      setColor(0, 255, 0);
    };//green

   if (measure.RangeMilliMeter < 100 && measure.RangeMilliMeter > 51)

    {
      setColor(0, 0, 255);
    };//blue
    
    if (measure.RangeMilliMeter < 150 && measure.RangeMilliMeter > 101 )

    {
      setColor(80, 80, 80);
    };

  

    if (measure.RangeMilliMeter < 200 && measure.RangeMilliMeter > 151 )

    {
      setColor(80, 0, 80);
    };//purple
  
     if (measure.RangeMilliMeter < 250 && measure.RangeMilliMeter > 201)

    {
      setColor(255, 255, 0);
    };//yellow

   if (measure.RangeMilliMeter < 300 && measure.RangeMilliMeter > 251)

    {
      setColor(255, 0, 0);
    };//red

    ledState = true; // remember led(s) are on
    ledTime = millis(); // note the time
  }
  if (ledState && millis() - ledTime > interval) { // if a led is on and time is up
    {
      setColor(0, 0, 0);
      ledState = false; // leds are off
    };


  }

  else {
    Serial.println(" out of range ");
  }
}