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