Ok I think I got it this time, seem to work perfectly according to serial outputs...
When placing the sensor on top of a bottle of water, I can shake the bottle to simulate gas moving inside the tank and serial outputs show a very linear readouts, taking a second or two to update the outputs ... "RunningMedian distance = RunningMedian(50);"
Here is the sketch that I will try on the tractor:
/* This example shows how to use continuous mode to take
range measurements with the 6 pin VL53L0X module. It is based on
vl53l0x_ContinuousRanging_Example.c from the VL53L0X API.
The range readings are in units of mm.
Original source: https://github.com/adafruit/Adafruit_VL53L0X
Modified by Ahmad Shamshiri for RoboJax.com
Date modified: May 31, 2018 at 19:25 at Ajax, Ontario, Canada
Watch the instruciton video for this code https://youtu.be/S2jaAQEv3Yo
Pin connection
VL53L0X Pin Arduino Pin
VCC 5V
GND GND
SDA A4 or SDA if available
SCL A5 or SCL if available
GPIO1 leave it unconnected
XSHUT D12 (digital 12 or pin 12)
*/
#include <Wire.h>
#include <VL53L0X.h>
#include <Adafruit_NeoPixel.h>
#include <RunningMedian.h>
VL53L0X sensor;
RunningMedian distance = RunningMedian(50);
#define PIN A0 // // WS2812 chip - Data In
#define Pixel_Number 16
Adafruit_NeoPixel strip = Adafruit_NeoPixel(Pixel_Number, PIN, NEO_GRB + NEO_KHZ800);
void updateStrip(int ledsToLight, uint32_t colorOn, uint32_t colorOff){
for(int x = 0; x < Pixel_Number; x++){
if(x < ledsToLight) {
strip.setPixelColor(x, colorOn);
} else {
strip.setPixelColor(x, colorOff);
}
}
strip.show();
}
struct Level {
int ledsToLight;
uint32_t color;
};
#define max_colors 3
Level colors[max_colors] = {
{3, strip.Color(255,0,0)},
{5, strip.Color(255,128,0)},
{16, strip.Color(0,255,0)},
};
uint32_t getColor(int ledsToLight){
uint32_t ret = strip.Color(0,0,255);
for(int x = 0; x < max_colors; x++){
if (ledsToLight <= colors[x].ledsToLight){
ret = colors[x].color;
break;
}
}
return ret;
}
void setup()
{
pinMode(12,INPUT_PULLUP);
digitalWrite(12,HIGH);
strip.begin();
Serial.begin(9600);
Wire.begin();
sensor.init();
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();
}
void loop()
{
// This is to Filter Noise and jumping readouts from the sensor to provide linear outputs
int range = sensor.readRangeContinuousMillimeters();
distance.add(range);
range = distance.getMedian();
range = range -55; // This is to calibrate the sensor so it read 0 when set on flat surface
Serial.print("Distance: ");
Serial.print(range);
Serial.print("mm");
if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
Serial.println();
delay(100);
int ledsToLight = 0;
int distancetotop = ( range );
if(distancetotop < 60 ) {ledsToLight = 16;}
else if(distancetotop > 60 && distancetotop < 72 ) {ledsToLight = 15;}
else if(distancetotop > 72 && distancetotop < 84 ) {ledsToLight = 14;}
else if(distancetotop > 84 && distancetotop < 96 ) {ledsToLight = 13;}
else if(distancetotop > 96 && distancetotop < 108 ) {ledsToLight = 12;}
else if(distancetotop > 108 && distancetotop < 120 ) {ledsToLight = 11;}
else if(distancetotop > 120 && distancetotop < 132 ) {ledsToLight = 10;}
else if(distancetotop > 132 && distancetotop < 144 ) {ledsToLight = 9;}
else if(distancetotop > 144 && distancetotop < 156 ) {ledsToLight = 8;}
else if(distancetotop > 156 && distancetotop < 168 ) {ledsToLight = 7;}
else if(distancetotop > 168 && distancetotop < 180 ) {ledsToLight = 6;}
else if(distancetotop > 180 && distancetotop < 192 ) {ledsToLight = 5;}
else if(distancetotop > 192 && distancetotop < 204 ) {ledsToLight = 4;}
else if(distancetotop > 204 && distancetotop < 216 ) {ledsToLight = 3;}
else if(distancetotop > 216 && distancetotop < 228 ) {ledsToLight = 2;}
else if(distancetotop > 228 && distancetotop < 240 ) {ledsToLight = 1;}
else if(distancetotop > 240 ) {ledsToLight = 0;}
if (ledsToLight == 0) {
doFlash();
} else {
auto colorOff = strip.Color(0,0,0);
auto colorOn = getColor(ledsToLight);
updateStrip(ledsToLight, colorOn, colorOff);
}
}
void doFlash(){
auto color = strip.Color(0,0,0);
if((millis() / 540) & 1 != 0){
color = strip.Color(255,0,0);
}
for (int x = 0; x < Pixel_Number; x++){
strip.setPixelColor(x, color);
}
strip.show();
}
I will confirm it all of this is working in real life...
Cheers