Good evening,
Juist need a little pointer to help with this personal project I'm working on.
I have a 2 TOF sensors, 2 RGB LED's and a small speaker. I have it currently setup in a way that the speaker gives off a tone and an LED Flashes (either red or green) when the sensor reading is below a certain value.
When the sensor is not being set off the LED's stay a blue colour. I would like the LED that was flashing to stay on for 5 seconds with either a green or red colour and then after that return to a solid blue colour.
Below is my code so far:
/*
Code for the yes/no device.
The Tone pin that will be used is Digital pin 3
RGB LEDs are also controlled once the sensor has been triggered
RGB Pins on LED1:
G=D11
R=D12
B=D13
Red colour is RGB(255, 0, 0) or #ff0000
Red colour is RGB(0, 255, 0) or #00ff00
Static orange colour for when sensor has not been set off RGB(255, 128, 0) or #ff8000
*/
#include <Wire.h>
#include <VL53L1X.h>
int redPinNo = 8;
int greenPinNo = 9;
int bluePinNo = 10;
int redPinYes = 12;
int greenPinYes = 11;
int bluePinYes = 13;
// The number of sensors in your system.
const uint8_t sensorCount = 2;
// The Arduino pin connected to the XSHUT pin of each sensor.
const uint8_t xshutPins[sensorCount] = { 6, 7 };
VL53L1X sensors[sensorCount];
void setup()
{
pinMode(redPinNo, OUTPUT);
pinMode(greenPinNo, OUTPUT);
pinMode(bluePinNo, OUTPUT);
pinMode(redPinYes, OUTPUT);
pinMode(greenPinYes, OUTPUT);
pinMode(bluePinYes, OUTPUT);
while (!Serial) {}
Serial.begin(115200);
Wire.begin();
Wire.setClock(400000); // use 400 kHz I2C
// Disable/reset all sensors by driving their XSHUT pins low.
for (uint8_t i = 0; i < sensorCount; i++)
{
pinMode(xshutPins[i], OUTPUT);
digitalWrite(xshutPins[i], LOW);
}
// Enable, initialize, and start each sensor, one by one.
for (uint8_t i = 0; i < sensorCount; i++)
{
// Stop driving this sensor's XSHUT low. This should allow the carrier
// board to pull it high. (We do NOT want to drive XSHUT high since it is
// not level shifted.) Then wait a bit for the sensor to start up.
pinMode(xshutPins[i], INPUT);
delay(10);
sensors[i].setTimeout(500);
if (!sensors[i].init())
{
Serial.print("Failed to detect and initialize sensor ");
Serial.println(i);
while (1);
}
// Each sensor must have its address changed to a unique value other than
// the default of 0x29 (except for the last one, which could be left at
// the default). To make it simple, we'll just count up from 0x2A.
sensors[i].setAddress(0x2A + i);
sensors[i].startContinuous(50);
}
}
void loop()
{
int MaxDistanceValue = 1000;
int DistanceNo = sensors[0].read();
int DistanceYes = sensors[1].read();
//for (uint8_t i = 0; i < sensorCount; i++)
//for (uint8_t i = 0; i < 1; i++)
//{
// Serial.print(sensors[i].read());
//Serial.print((String)"NO side sensor "+sensors[0].read());
//Serial.println();
//Serial.print((String)"YES side sensor "+sensors[1].read());
//Serial.println();
Serial.print((String)"NO side sensor "+DistanceNo);
Serial.println();
Serial.print((String)"YES side sensor "+DistanceYes);
Serial.println();
if (DistanceNo<MaxDistanceValue*0.1) {
tone(3, 147, 50 );
}
else if (DistanceNo<MaxDistanceValue*0.2) {
tone(3, 139, 50 );
}
else if (DistanceNo<MaxDistanceValue*0.4) {
tone(3, 131, 50 );
}
else if (DistanceNo<MaxDistanceValue*0.6) {
tone(3, 123, 50 );
}
else if (DistanceNo<MaxDistanceValue*0.8) {
tone(3, 117, 50 );
}
else if (DistanceNo<MaxDistanceValue*1) {
tone(3, 110, 50 );
}
//else {
// noTone(3);
//}
if (DistanceNo<MaxDistanceValue*1) {
analogWrite(redPinNo, 255);
analogWrite(greenPinNo, 0);
analogWrite(bluePinNo, 0);
delay(50);
analogWrite(redPinNo, 0);
analogWrite(greenPinNo, 0);
analogWrite(bluePinNo, 0);
}
else {
analogWrite(redPinNo, 0);
analogWrite(greenPinNo, 0);
analogWrite(bluePinNo, 255);
}
//delay(50);
if (DistanceYes<MaxDistanceValue*0.1) {
tone(3, 1976, 50 );
}
else if (DistanceYes<MaxDistanceValue*0.2) {
tone(3, 1865, 50 );
}
else if (DistanceYes<MaxDistanceValue*0.4) {
tone(3, 1760, 50 );
}
else if (DistanceYes<MaxDistanceValue*0.6) {
tone(3, 1661, 50 );
}
else if (DistanceYes<MaxDistanceValue*0.8) {
tone(3, 1568, 50 );
}
else if (DistanceYes<MaxDistanceValue*1) {
tone(3, 1480, 50 );
}
//else {
// noTone(3);
//}
if (DistanceYes<MaxDistanceValue*1) {
analogWrite(redPinYes, 0);
analogWrite(greenPinYes, 255);
analogWrite(bluePinYes, 0);
delay(50);
analogWrite(redPinYes, 0);
analogWrite(greenPinYes, 0);
analogWrite(bluePinYes, 0);
}
else {
analogWrite(redPinYes, 0);
analogWrite(greenPinYes, 0);
analogWrite(bluePinYes, 255);
}
// if (sensors[i].timeoutOccurred()) { Serial.print(" TIMEOUT"); }
// Serial.print('\t');
//}
// Serial.println();
}
To help visualize what the code does I have just uploaded a 20 second clip to youtube so you can see the behaviour.
Yes/No toy video for reference
If any one is able to help it would be much appreciated. Thank you