Programmcoding barrier, TOF sensor, LCD

Hi there,

i had a question how to realize a with a tof sensor some kinda barrier which has a constant distance of , for example 200mm and how you can achieve a rising bargraph on the LCD (1602) when you reduce the distance of the barrier range of 200mm.

The idea behind it, is to develop a running measurement on an overhead bin displayed on the LCD, where the LCD shows at specific distance FULL or FREE with a loop code. I tried it several times and didnt found anything helpful.

So basically its 2 problem:

-> bargraph to rise while u reduce the distance between wall and ToF sensor
-> distance showing as a serial print on lCD when for example maxdistance > 200mm ----> print FREE
otherwise maxdistance< 200mm -----> FULL

any ideas how to realise the process? That was just an idea and if u have another ideas of realizing the task let me know

code:

#include <Wire.h>
#include <VL53L0X.h>
VL53L0X sensor;
#define maxDistance 200

//int VCC2= 13;// 2nd VCC for laser sensor

#include <LiquidCrystal_I2C.h>
byte lcdNumCols = 16; // -- number of columns in the LCD
byte lcdLine = 2; // -- number of line in the LCD
// Set the LCD address to 0x26/0x3F for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, lcdNumCols, lcdLine);// use I2C Scanner to find the address: 0x27 or something like that

#include <LcdBarGraphRobojax.h>
LcdBarGraphRobojax rjx(&lcd, 16, 0, 0); // -- creating 16 character long bargraph starting at char 0 of line 0 (see video)

void setup()
{

Serial.begin(9600);
Wire.begin();

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

// Start continuous back-to-back mode (take readings as
// fast as possible). To use continuous timed mode
// instead, provide a desired inter-measurement period in
// ms (e.g. sensor.startContinuous(100)).
sensor.startContinuous(100);

// initialize the LCD,
lcd.init();
lcd.clear();
lcd.print("Robojax");
lcd.setCursor (0,1);
// lcd.print("Bargraph VL53L0X");
lcd.backlight(); // Turn on the blacklight and print a message.
// -- do some delay: some time I've got broken visualization

delay(2000);
lcd.clear();// clear the screen from previous value

}

void loop()
{
int distance=sensor.readRangeContinuousMillimeters();
if (maxDistance>=distance){
lcd.setCursor (0,2);
lcd.print("FREE");
int FREE=distance;
}
else{
lcd.setCursor (0,1);
lcd.print("FULL");
int FULL=distance;
}
// int distance =sensor.startContinuous(100);
rjx.clearLine(1);// clear line 1 to display fresh voltage value

// -- draw bar graph from the analog value read
rjx.drawValue(maxDistance, distance);
// -- do some delay: frequent draw may cause broken visualization
//delay(100);

lcd.setCursor (0,1); // go to start of 2nd line
//lcd.print("FULL");
lcd.setCursor (7,1); // go to start of 2nd line
lcd.print(distance);
lcd.print("mm");

if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }

Serial.println();
delay(500);
}

p.s.: the code is "quick and dirty" and i tried to optimize it . Its from another project

Thanks !:slight_smile: