#include <LiquidCrystal.h>
#include "MultiMap.h"
#define TRIGGER_PIN 10
#define ECHO_PIN 9
#define USONIC_DIV 58.0
#define MEASURE_SAMPLE_DELAY 5
#define MEASURE_SAMPLES 25
#define MEASURE_DELAY 250
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
long mili_liter;
float liter,Average;
void setup()
{
lcd.begin(16, 2);
lcd.setCursor(0, 0);lcd.print("Digital Fuel ");
lcd.setCursor(0, 1);lcd.print("Level Indicator");delay(3000);
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(500);
lcd.clear();
}
void loop()
{
delay(MEASURE_DELAY);
long distance = measure();
mili_liter=Distance_to_ml(distance);
liter = mili_liter/1000.00;
Average=liter*40.00;
lcd.clear();
lcd.setCursor(0, 0);lcd.print("AV.R=");lcd.print(Average);lcd.print(" Km");
lcd.setCursor(0, 1);lcd.print("Fuel amt=");lcd.print(liter,3);lcd.print(" L");
}
long measure()
{
long measureSum = 0;
for (int i = 0; i < MEASURE_SAMPLES; i++)
{
delay(MEASURE_SAMPLE_DELAY);
measureSum += singleMeasurement();
}
return measureSum / MEASURE_SAMPLES;
}
long singleMeasurement()
{
long duration = 0;
// Measure: Put up Trigger...
digitalWrite(TRIGGER_PIN, HIGH);
// ... wait for 11 µs ...
delayMicroseconds(11);
// ... put the trigger down ...
digitalWrite(TRIGGER_PIN, LOW);
// ... and wait for the echo ...
duration = pulseIn(ECHO_PIN, HIGH);
return (long) (((float) duration / USONIC_DIV) * 10.0);
}
long Distance_to_ml(long val){
long out[] = { 5000, 4750, 4500, 4250, 4000, 3750, 3500, 3250, 3000, 2750, 2500, 2250, 2000, 1750, 1500, 1250, 1000, 750, 500, 0};///ml
long in[] = { 49, 55, 59, 69, 74, 80, 85, 87, 96, 99, 109, 115, 124, 132, 139, 146, 155, 164, 171, 190};////////mm
long ml = multiMap<long>(val, in, out, 20);
return ml;
}