Hello everyone I'm working with a a couple of VL53L1X sensors and I have successfully managed to get them to both work and report in with accurate enough measurements for what I intend to use them for which is a great start.
The next hurdle I'm trying to figure out is how to get a tone out based upon the measurement being seen, the ideal scenario is that I have the tone speed consistent (beeps 4 times a second for example) but the closer you get the tone changes and the left side ands the right side will have different tone ranges (left side has tone range of 110-150 and the right side would have a tone range of 1568-1976).
I know what I want to do but I'm struggling to have the brainwave needed to achieve this so I'm reaching out to see if I could grab some help.
Below is my code so far and below that I will show you the serial output.
/*
Code for the left/right device.
The Tone pin that will be used is Digital pin 3
RGB LEDs are also controlled once the sensor has been triggered - TODO
*/
#include <Wire.h>
#include <VL53L1X.h>
// Left side tones
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
// Right side tones
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
// 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()
{
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++)
{
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);
}
sensors[i].setAddress(0x2A + i);
sensors[i].startContinuous(50);
}
}
void loop()
{
//for (uint8_t i = 0; i < sensorCount; i++)
//for (uint8_t i = 0; i < 1; i++)
{
// Serial.print(sensors[i].read());
Serial.print((String)"Left side sensor "+sensors[0].read());
Serial.println();
Serial.print((String)"Right side sensor "+sensors[1].read());
Serial.println();
// if (sensors[i].timeoutOccurred()) { Serial.print(" TIMEOUT"); }
// Serial.print('\t');
}
// Serial.println();
}
Output of Serial
21:06:34.040 -> Left side sensor 2777
21:06:34.040 -> Right side sensor 2788
21:06:34.133 -> Left side sensor 2766
21:06:34.133 -> Right side sensor 2784
21:06:34.226 -> Left side sensor 2770
21:06:34.226 -> Right side sensor 2786
Please ignore the commented out sections that due to me doing some testing