so im making a project that can measure the volume of the fuel remaining on the fuel tank by using float as sensor … the vout when fuel is empty = 430 and vout when fuel is full = 30, is there any suggestion on making my project even better?
here’s my coding so far:
const int numReadings = 10; // Need to do some smoothing of the readings for fuel level
// Define the number of samples to keep track of. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input. Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int averagefuel = 0; // the average of fuel reading as an analog input (calculated)
int percentage = 0; // percentage of tank capacity remaining (calculated)
int inputPin = A0; // analog input from fuel sendor (measured)
int fuelled = 10; // led write location for fuel led warning light (defined)
int vout = 0; // fuel sensor voltage from analog input (calculated)
int rtwo = 0; // resistance across fuel sending unit (calculated)
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup()
{
Serial.begin(9600); // initialize serial communication with computer:
for (int thisReading = 0; thisReading < numReadings; thisReading++) // initialize all the readings to 0:
readings[thisReading] = 0;
lcd.begin(16, 2); // set up the LCD’s number of columns and rows:
pinMode(0, INPUT); // configure analog pins as inputs
pinMode(1, INPUT);
pinMode(2, INPUT);
}
void loop() {
total= total - readings[index]; // subtract the last reading:
readings[index] = analogRead(inputPin); // read from the sensor:
total= total + readings[index]; // add the reading to the total:
index = index + 1; // advance to the next position in the array:
if (index >= numReadings) // if we’re at the end of the array…
index = 0; // …wrap around to the beginning:
averagefuel = total / numReadings; // calculate the average:
float vout = averagefuel * (5.0 / 430.0); // calculate vout
float rtwo = 1000 / ((5/vout)-1); // calculate rtwo (R1 on board is 1000 ohms)
if(rtwo < 75) percentage = 100; // takes ohm range of tank (1050 to 70) and divides it up by 12 gallons or 8%
else if (rtwo < 151) percentage = 92; // starts checking to see if its full and works its way down
else if (rtwo <233 ) percentage = 84;
else if (rtwo < 315) percentage = 76;
else if (rtwo < 396) percentage = 68;
lcd.setCursor(0, 0); // Fuel status display on LCD
lcd.print(“FUEL LEVEL”);
lcd.print(" ");
lcd.setCursor(12,0);
lcd.print(percentage);
lcd.setCursor(15, 0);
lcd.print("%");
lcd.setCursor(0,1);
delay(1000);
}