/*
Forum: https://forum.arduino.cc/t/att-mata-avstand-och-omvandla-det-till-toner/1342230
Wokwi: https://wokwi.com/projects/419982214678329345
This sketch "fakes" a TOF board VL53L0X by a HC SR-04 Ultrasonic Dstance Sensor
and converts the distance data into some audible signals
The range (0 ... 1000 mm) is mapped to the frequencies from "maxHz" to "minHz"
so the highest tone will be created at the lowest range/distance detected
2025/01/13
ec2021
*/
#include "VL53L0X.h"
const unsigned long interval {100}; // Interval in ms between two measurements
const byte tonePin {5}; // Pin for buzzer
const int maxRange {1000}; // maximum Range of TOF sensor
const int minHz {50}; // minimum frequency to be used
const int maxHz {3000}; // maximum frequency to be used
VL53L0X lox = VL53L0X();
unsigned long lastMeasurement = 0;
void setup() {
Serial.begin(115200);
Serial.println("Fake VL53L0X test.");
if (!lox.begin()) {
Serial.println(F("Failed to boot Fake VL53L0X"));
while(1);
}
Serial.println(F("Fake VL53L0X API Continuous Ranging example\n\n"));
// start continuous ranging
lox.startRangeContinuous();
}
void loop() {
if (millis()-lastMeasurement > interval){
lastMeasurement = millis();
doMeasurement();
}
}
void doMeasurement(){
if (lox.isRangeComplete()) {
noTone(tonePin);
Serial.print("Distance in mm: ");
float loxRange = lox.readRange();
Serial.println(loxRange);
int Tone = map(loxRange,0, maxRange,maxHz,minHz);
tone(tonePin,Tone,interval);
}
}