RGB LED INDICATOR TO DISTANCE BY VL53L0X

This project is to demonstrate the control of RGB LEDs with a VL53L0X distance sensor.

The code can be downloaded at:

https://github.com/triantara/ARDUINO-TRIANTARA/blob/main/VL530X%20RGB.zip

53VL0X RGB LED

const int ledPinRed = 3; // Red LED connected to analogue out pin
const int ledPinGrn = 5; // Green LED connected to analogue out pin
const int ledPinBlu = 6; // Blue LED connected to analogue out pin
// Constants to define the ranges.

const int hueRedLow = 0;
const int hueRedHigh = 255;
const int hueBlue = 170;
const int angleMin = 0;
const int angleSector = 1;
const int angleMax = 360;

const int brightMin = 0;
const int brightMax = 255;

int potValueHue;

int hue, brightness;

const int saturation = 255;
unsigned int r, g, b;
#include <Wire.h>
#include <VL53L0X.h>

VL53L0X sensor;
void setup() {
// Still need to set a baud rate, even for USB.
Serial.begin(9600);
Wire.begin();

sensor.init();
sensor.setTimeout(100);

// Set LED pins to output.
pinMode(ledPinRed, OUTPUT);
pinMode(ledPinGrn, OUTPUT);
pinMode(ledPinBlu, OUTPUT);

sensor.startContinuous();
}

void loop() {
potValueHue = map(sensor.readRangeContinuousMillimeters(), 1000, 0, 0, 360);

hue = constrain(map(potValueHue, angleSector, angleMax – angleSector, hueRedLow, hueBlue), hueRedLow, hueBlue);

brightness = constrain(map(potValueHue, angleMin, angleSector, brightMin, brightMax), brightMin, brightMax);

brightness = brightness – constrain(map(potValueHue, angleMax – angleSector, angleMax, brightMin, brightMax), brightMin, brightMax);

// Do the conversion.
HSBToRGB(hue, saturation, brightness, &r, &g, &b);

analogWrite(ledPinRed, r);
analogWrite(ledPinGrn, g);
analogWrite(ledPinBlu, b);

Serial.print(” bright=”);
Serial.print(brightness);
Serial.print(” hue=”);
Serial.print(hue);
Serial.print(” red=”);
Serial.print(r);
Serial.print(” green=”);
Serial.print(g);
Serial.print(” blue=”);
Serial.print(b);
Serial.println(“”);
delay(50);
}

void HSBToRGB(
unsigned int inHue, unsigned int inSaturation, unsigned int inBrightness,
unsigned int *oR, unsigned int *oG, unsigned int *oB )
{
if (inSaturation == 0)
{
// achromatic (grey)
*oR = *oG = *oB = inBrightness;
}
else
{
unsigned int scaledHue = (inHue * 6);
unsigned int sector = scaledHue >> 8; // sector 0 to 5 around the color wheel
unsigned int offsetInSector = scaledHue – (sector << 8); // position within the sector
unsigned int p = (inBrightness * ( 255 – inSaturation )) >> 8;
unsigned int q = (inBrightness * ( 255 – ((inSaturation * offsetInSector) >> 8) )) >> 8;
unsigned int t = (inBrightness * ( 255 – ((inSaturation * ( 255 – offsetInSector )) >> 8) )) >> 8;

switch( sector ) {
case 0:
*oR = inBrightness;
*oG = t;
*oB = p;
break;
case 1:
*oR = q;
*oG = inBrightness;
*oB = p;
break;
case 2:
*oR = p;
*oG = inBrightness;
*oB = t;
break;
case 3:
*oR = p;
*oG = q;
*oB = inBrightness;
break;
case 4:
*oR = t;
*oG = p;
*oB = inBrightness;
break;
default: // case 5:
*oR = inBrightness;
*oG = p;
*oB = q;
break;
}
}
}