## arduino as a digital fuel meter

Need help with software or code, this is your place.
Hello i need your help again , PLEASE!!!!!!
i have an idea to build an adruino digital fuel meter, i found a similar project here i this site (http://www.design-concepts.com/insights ... ons-racing) this project have water warning light, oil warning light, and low level fuel light , but i don't need any of this , only i need is the fuel level .
So i take a arduino , a fuel sensor (it's variable resistor),and a lcd display and make the connections.
The fuel sensor connected to AO pin, and the lcd to digital pins, i run the code from the site above ( i put the code and in this post ).
And guess ; it work's but they give me wrong measurement,
THE problem is the sensor , the code is build for sensor with 70-1000 ohm .
my sensor is 0-190ohm (0 ohm is empty - 190 ohm is full )
here i need desperately your help, because i dont know to make calculations in arduino.

thanks
Code: Select all
`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 inputint index = 0; // the index of the current readingint total = 0; // the running totalint 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)int oilled = 8; // led write location for oil led (defined)int oilvin = A2; // analog input for oil sensor (defined)int oilvolt = 0; // oil sensor voltage from analog input (actual analog input, not converted to an actual voltage)int watervin = A1; // location for water sensor input (defined)int waterled = 9; // led write location for water temp led (defined)int watervolt=0; // water sensor voltage from analog input (actual analog input, not converted to an actual voltage)#include // inclusion of lcd libraryLiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins#include // inclusion of bargraph librarybyte lcdNumCols = 16; // -- number of columns in the LCDLcdBarGraph lbg(&lcd,16,0,1); // -- creating bargraph instancevoid 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}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 / 1023.0); // calculate voutfloat 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 downelse if (rtwoelse if (rtwo < 315) percentage = 76;else if (rtwo < 396) percentage = 68;else if (rtwo < 478) percentage = 60;else if (rtwo < 560) percentage = 52;else if (rtwo < 642) percentage = 44;else if (rtwo < 723) percentage = 36;else if (rtwo < 805) percentage = 28;else if (rtwo < 887) percentage = 20;else if (rtwo < 968) percentage = 12;else if (rtwo < 1050) percentage =4;lcd.setCursor(0, 0); // Fuel status display on LCDlcd.print("FUEL LEVEL");lcd.print(" ");lcd.setCursor(12,0);lcd.print(percentage);lcd.setCursor(15, 0);lcd.print("%");lcd.setCursor(0,1);lbg.drawValue(percentage,100);float oilvolt=analogRead(oilvin); // Read oil and water gauges analog inputfloat watervolt= analogRead(watervin);if (percentage < 13)digitalWrite(fuelled, HIGH); // turns on fuel warning led if fuel under 13%if (watervolt < 460) digitalWrite(waterled, HIGH); // turns on water temp led if temp above 210 F as determined in calibrationif (oilvolt > 410) digitalWrite(oilled, HIGH); // turns on oil pressure led if pressure below 12.5 psidelay(1000); // flashes led/sdigitalWrite(fuelled, LOW);digitalWrite(waterled, LOW);digitalWrite(oilled, LOW);delay(1000);}`
skipper

Posts: 12
Joined: Sat May 21, 2016 7:34 am

The resistance of the variable resistor actually doesn't matter. No matter what the Arduino will read it as an analog value of 0-1023.

So you just need to convert that to 0-100 to get your percent. Something like this would work

Code: Select all
`int percent = map(averagefuel, 0, 1024, 0, 100); //take a reading that is from 0-1024 and make it from 0-100OR - IM NOT SURE WHAT ONEint percent = map(averagefuel, 0, 1024, 100, 0); //take a reading that is from 0-1024 and make it from 100-0`

It might be one or the other depending on if the Arduino reads 0 when it is empty, or 1023 when it is empty.

skipper wrote:Hello i need your help again , PLEASE!!!!!!
i have an idea to build an adruino digital fuel meter, i found a similar project here i this site (http://www.design-concepts.com/insights ... ons-racing) this project have water warning light, oil warning light, and low level fuel light , but i don't need any of this , only i need is the fuel level .
So i take a arduino , a fuel sensor (it's variable resistor),and a lcd display and make the connections.
The fuel sensor connected to AO pin, and the lcd to digital pins, i run the code from the site above ( i put the code and in this post ).
And guess ; it work's but they give me wrong measurement,
THE problem is the sensor , the code is build for sensor with 70-1000 ohm .
my sensor is 0-190ohm (0 ohm is empty - 190 ohm is full )
here i need desperately your help, because i dont know to make calculations in arduino.

thanks
Code: Select all
`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 inputint index = 0; // the index of the current readingint total = 0; // the running totalint 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)int oilled = 8; // led write location for oil led (defined)int oilvin = A2; // analog input for oil sensor (defined)int oilvolt = 0; // oil sensor voltage from analog input (actual analog input, not converted to an actual voltage)int watervin = A1; // location for water sensor input (defined)int waterled = 9; // led write location for water temp led (defined)int watervolt=0; // water sensor voltage from analog input (actual analog input, not converted to an actual voltage)#include // inclusion of lcd libraryLiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins#include // inclusion of bargraph librarybyte lcdNumCols = 16; // -- number of columns in the LCDLcdBarGraph lbg(&lcd,16,0,1); // -- creating bargraph instancevoid 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}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 / 1023.0); // calculate voutfloat 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 downelse if (rtwoelse if (rtwo < 315) percentage = 76;else if (rtwo < 396) percentage = 68;else if (rtwo < 478) percentage = 60;else if (rtwo < 560) percentage = 52;else if (rtwo < 642) percentage = 44;else if (rtwo < 723) percentage = 36;else if (rtwo < 805) percentage = 28;else if (rtwo < 887) percentage = 20;else if (rtwo < 968) percentage = 12;else if (rtwo < 1050) percentage =4;lcd.setCursor(0, 0); // Fuel status display on LCDlcd.print("FUEL LEVEL");lcd.print(" ");lcd.setCursor(12,0);lcd.print(percentage);lcd.setCursor(15, 0);lcd.print("%");lcd.setCursor(0,1);lbg.drawValue(percentage,100);float oilvolt=analogRead(oilvin); // Read oil and water gauges analog inputfloat watervolt= analogRead(watervin);if (percentage < 13)digitalWrite(fuelled, HIGH); // turns on fuel warning led if fuel under 13%if (watervolt < 460) digitalWrite(waterled, HIGH); // turns on water temp led if temp above 210 F as determined in calibrationif (oilvolt > 410) digitalWrite(oilled, HIGH); // turns on oil pressure led if pressure below 12.5 psidelay(1000); // flashes led/sdigitalWrite(fuelled, LOW);digitalWrite(waterled, LOW);digitalWrite(oilled, LOW);delay(1000);}`
ameyer
Founder

Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

dear ameyer, thank for your reply i make the changes to the code but still nothing .

i am sure that, i make somewere wrong with the code can you please check the code;
thanks!!

Code: Select all
`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 inputint index = 0; // the index of the current readingint total = 0; // the running totalint 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)#include <LiquidCrystal.h> // inclusion of lcd library  LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins #include <LcdBarGraph.h> // inclusion of bargraph librarybyte lcdNumCols = 16;  // -- number of columns in the LCDLcdBarGraph lbg(&lcd,16,0,1); // -- creating bargraph instancevoid setup(){Serial.begin(9600);  // initialize serial communication with computer:int  percent = map(averagefuel, 0, 1024, 100, 0); //take a reading that is from 0-1024 and make it from 100-0lcd.begin(16, 2);  // set up the LCD's number of columns and rows:pinMode(A0, INPUT); // configure analog pins as inputs;}void loop() {lcd.setCursor(0, 0); // Fuel status display on LCDlcd.print("FUEL LEVEL");lcd.print(" ");lcd.setCursor(12,0);lcd.print(percentage);lcd.setCursor(15, 0);lcd.print("%");lcd.setCursor(0,1);lbg.drawValue(percentage,100);delay(1000);}`
skipper

Posts: 12
Joined: Sat May 21, 2016 7:34 am

There was pretty good reason the code wouldn't work. It actually only read the values once. You had almost everything in setup which runs once. You need to do things like that in the loop

I doubt this will work, but try this out.

Code: Select all
`const int numReadings = 10;  // Need to do some smoothing of the readings for fuel levelint readings[numReadings];  // the readings from the analog inputint index = 0; // the index of the current readingint inputPin = A0;  // analog input from fuel sendor (measured)#include <LcdBarGraph.h> // inclusion of bargraph library#include <LiquidCrystal.h> // inclusion of lcd library  LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pinsbyte lcdNumCols = 16;  // -- number of columns in the LCDLcdBarGraph lbg(&lcd, 16, 0, 1); // -- creating bargraph instancevoid setup() {  Serial.begin(9600);  // initialize serial communication with computer:  pinMode(A0, INPUT); // configure analog pins as inputs  lcd.begin(16, 2);  // set up the LCD's number of columns and rows:}void loop() {  int valueFromSensor = analogRead(inputPin);  int averagefuelReading = returnAverageSensorValue(valueFromSensor);    int  percent = map(averagefuelReading, 0, 1024, 100, 0); //take a reading that is from 0-1024 and make it from 100-0    printToLCD(percent);  lbg.drawValue(percent, 100);}int returnAverageSensorValue(int newestValue){  index ++; //increment the index  if(index >= numReadings) index = 0; //reset the index if it hits 10  readings[index] = newestValue;    int sumValue = 0;  for(int i = 0; i < numReadings; i++){    sumValue += readings[i];  }    return sumValue/numReadings;}void printToLCD(int percentage){  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);}`

skipper wrote:dear ameyer, thank for your reply i make the changes to the code but still nothing .

i am sure that, i make somewere wrong with the code can you please check the code;
thanks!!

Code: Select all
`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 inputint index = 0; // the index of the current readingint total = 0; // the running totalint 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)#include <LiquidCrystal.h> // inclusion of lcd library  LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins #include <LcdBarGraph.h> // inclusion of bargraph librarybyte lcdNumCols = 16;  // -- number of columns in the LCDLcdBarGraph lbg(&lcd,16,0,1); // -- creating bargraph instancevoid setup(){Serial.begin(9600);  // initialize serial communication with computer:int  percent = map(averagefuel, 0, 1024, 100, 0); //take a reading that is from 0-1024 and make it from 100-0lcd.begin(16, 2);  // set up the LCD's number of columns and rows:pinMode(A0, INPUT); // configure analog pins as inputs;}void loop() {lcd.setCursor(0, 0); // Fuel status display on LCDlcd.print("FUEL LEVEL");lcd.print(" ");lcd.setCursor(12,0);lcd.print(percentage);lcd.setCursor(15, 0);lcd.print("%");lcd.setCursor(0,1);lbg.drawValue(percentage,100);delay(1000);}`
ameyer
Founder

Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

dear ameyer, first of all sorry for my delay .
thank you so much for you help , and yes we have measurements. but its not accurate.
sensor position 0-95ohm ------ 95-190ohm
lcd shows 10,40,60,90 ------ 10,11,12,15,17 %
exactly that numbers
I need to show me something like that 0-190ohm ------- 0-100%

thanks again!
skipper

Posts: 12
Joined: Sat May 21, 2016 7:34 am

This is a very easy problem. We just need the complete raw readings from the sensor when the tank is empty, at 50% and full

We just need to know then it is empty, what does the arduino read. And again, what does it read when it is full. Then we will map that range to 0-100%

Try this code and write down the values you see and get back to me.

Code: Select all
`int inputPin = A0;  // analog input from fuel sendor (measured)void setup() {  Serial.begin(9600);  // initialize serial communication with computer:  pinMode(A0, INPUT); // configure analog pins as inputs}void loop() {  int valueFromSensor = analogRead(inputPin);  Serial.println(valueFromSensor);}`

skipper wrote:dear ameyer, first of all sorry for my delay .
thank you so much for you help , and yes we have measurements. but its not accurate.
sensor position 0-95ohm ------ 95-190ohm
lcd shows 10,40,60,90 ------ 10,11,12,15,17 %
exactly that numbers
I need to show me something like that 0-190ohm ------- 0-100%

thanks again!
ameyer
Founder

Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

This is a very easy problem. We just need the complete raw readings from the sensor when the tank is empty, at 50% and full

We just need to know then it is empty, what does the arduino read. And again, what does it read when it is full. Then we will map that range to 0-100%

Try this code and write down the values you see and get back to me.

Code: Select all
`int inputPin = A0;  // analog input from fuel sendor (measured)void setup() {  Serial.begin(9600);  // initialize serial communication with computer:  pinMode(A0, INPUT); // configure analog pins as inputs}void loop() {  int valueFromSensor = analogRead(inputPin);  Serial.println(valueFromSensor);}`

skipper wrote:dear ameyer, first of all sorry for my delay .
thank you so much for you help , and yes we have measurements. but its not accurate.
sensor position 0-95ohm ------ 95-190ohm
lcd shows 10,40,60,90 ------ 10,11,12,15,17 %
exactly that numbers
I need to show me something like that 0-190ohm ------- 0-100%

thanks again!
ameyer
Founder

Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

First of all, thank you so much beacause you spend your time to help me!

Here is the position of the sensor 0%_____25%_____50%_____75%_____100%
And here is the values..............1023_____990_____940_____915______857

thanks!
skipper

Posts: 12
Joined: Sat May 21, 2016 7:34 am

Awesome!

So the range is from 1023 to 857 for a change of 166. So you could say that every change of 1.66 is 1%.
So you could then do 100 - ((value - 857) / 1.66) to convert it to 0 -100

But there is this awesome function called map that will convert it all for us.

So we say
Code: Select all
`int  percent = map(fuelReading, 1023, 857, 0, 100)`

//take a reading that is from 1023(min) to 857(max) and convert it so it is from 0 to 100

So the final code is like this:

Code: Select all
`int valueAtZeroFuel = 1023;int valueAtFullFuel = 857;const int numReadings = 10;  // Need to do some smoothing of the readings for fuel levelint readings[numReadings];  // the readings from the analog inputint index = 0; // the index of the current readingint inputPin = A0;  // analog input from fuel sendor (measured)#include <LcdBarGraph.h> // inclusion of bargraph library#include <LiquidCrystal.h> // inclusion of lcd library  LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pinsbyte lcdNumCols = 16;  // -- number of columns in the LCDLcdBarGraph lbg(&lcd, 16, 0, 1); // -- creating bargraph instancevoid setup() {  Serial.begin(9600);  // initialize serial communication with computer:  pinMode(A0, INPUT); // configure analog pins as inputs  lcd.begin(16, 2);  // set up the LCD's number of columns and rows:}void loop() {  int valueFromSensor = analogRead(inputPin);  int averagefuelReading = returnAverageSensorValue(valueFromSensor);  int  percent = map(averagefuelReading, valueAtZeroFuel, valueAtFullFuel, 0, 100); //take a reading that is from valueAtZeroFuel to valueAtFullFuel and make it from 0 to 100    printToLCD(percent);  lbg.drawValue(percent, 100);}int returnAverageSensorValue(int newestValue){  index ++; //increment the index  if(index >= numReadings) index = 0; //reset the index if it hits 10  readings[index] = newestValue;    int sumValue = 0;  for(int i = 0; i < numReadings; i++){    sumValue += readings[i];  }    return sumValue/numReadings;}void printToLCD(int percentage){  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);}`

skipper wrote:First of all, thank you so much beacause you spend your time to help me!

Here is the position of the sensor 0%_____25%_____50%_____75%_____100%
And here is the values..............1023_____990_____940_____915______857

thanks!
ameyer
Founder

Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

thank you so much. you are the best .
works just perfect!!
thanks!!!!!
skipper

Posts: 12
Joined: Sat May 21, 2016 7:34 am

hello my friends !

I already use this code to take measurements from a sensor to lcd screen, but i want to have more info from this screen ,
so i connect 2 more sensors to A1,A2 and change sensor readings by pressing 3 different push buttons
example: pressing (one push ) button1 pin A5 show me info from sensor A0
pressing (one push ) button2 pin A4 show me info from sensor A1
pressing (one push ) button3 pin A3 show me info from sensor A2

I experiment with (case) and (if) command but i achieve nothing.

please i need help!!
thank

Code: Select all
`int valueAtZeroFuel = 1023;int valueAtFullFuel = 857;const int numReadings = 10;  // Need to do some smoothing of the readings for fuel levelint readings[numReadings];  // the readings from the analog inputint index = 0; // the index of the current readingint inputPin = A0;  // analog input from fuel sendor (measured)#include <LcdBarGraph.h> // inclusion of bargraph library#include <LiquidCrystal.h> // inclusion of lcd library  LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pinsbyte lcdNumCols = 16;  // -- number of columns in the LCDLcdBarGraph lbg(&lcd, 16, 0, 1); // -- creating bargraph instancevoid setup() {  Serial.begin(9600);  // initialize serial communication with computer:  pinMode(A0, INPUT); // configure analog pins as inputs  lcd.begin(16, 2);  // set up the LCD's number of columns and rows:}void loop() {  int valueFromSensor = analogRead(inputPin);  int averagefuelReading = returnAverageSensorValue(valueFromSensor);  int  percent = map(averagefuelReading, valueAtZeroFuel, valueAtFullFuel, 0, 100); //take a reading that is from valueAtZeroFuel to valueAtFullFuel and make it from 0 to 100    printToLCD(percent);  lbg.drawValue(percent, 100);}int returnAverageSensorValue(int newestValue){  index ++; //increment the index  if(index >= numReadings) index = 0; //reset the index if it hits 10  readings[index] = newestValue;    int sumValue = 0;  for(int i = 0; i < numReadings; i++){    sumValue += readings[i];  }    return sumValue/numReadings;}void printToLCD(int percentage){  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);}`
skipper

Posts: 12
Joined: Sat May 21, 2016 7:34 am

This code doesn't look complete. There is no mention of the buttons or the other sensors

skipper wrote:hello my friends !

I already use this code to take measurements from a sensor to lcd screen, but i want to have more info from this screen ,
so i connect 2 more sensors to A1,A2 and change sensor readings by pressing 3 different push buttons
example: pressing (one push ) button1 pin A5 show me info from sensor A0
pressing (one push ) button2 pin A4 show me info from sensor A1
pressing (one push ) button3 pin A3 show me info from sensor A2

I experiment with (case) and (if) command but i achieve nothing.

please i need help!!
thank

Code: Select all
`int valueAtZeroFuel = 1023;int valueAtFullFuel = 857;const int numReadings = 10;  // Need to do some smoothing of the readings for fuel levelint readings[numReadings];  // the readings from the analog inputint index = 0; // the index of the current readingint inputPin = A0;  // analog input from fuel sendor (measured)#include <LcdBarGraph.h> // inclusion of bargraph library#include <LiquidCrystal.h> // inclusion of lcd library  LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pinsbyte lcdNumCols = 16;  // -- number of columns in the LCDLcdBarGraph lbg(&lcd, 16, 0, 1); // -- creating bargraph instancevoid setup() {  Serial.begin(9600);  // initialize serial communication with computer:  pinMode(A0, INPUT); // configure analog pins as inputs  lcd.begin(16, 2);  // set up the LCD's number of columns and rows:}void loop() {  int valueFromSensor = analogRead(inputPin);  int averagefuelReading = returnAverageSensorValue(valueFromSensor);  int  percent = map(averagefuelReading, valueAtZeroFuel, valueAtFullFuel, 0, 100); //take a reading that is from valueAtZeroFuel to valueAtFullFuel and make it from 0 to 100    printToLCD(percent);  lbg.drawValue(percent, 100);}int returnAverageSensorValue(int newestValue){  index ++; //increment the index  if(index >= numReadings) index = 0; //reset the index if it hits 10  readings[index] = newestValue;    int sumValue = 0;  for(int i = 0; i < numReadings; i++){    sumValue += readings[i];  }    return sumValue/numReadings;}void printToLCD(int percentage){  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);}`
ameyer
Founder

Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

OOPS!!! sorry wrong code ,
this is the code but dot work

Code: Select all
`int valueAtZeroFuel = 0;int valueAtFullFuel = 1023;const int numReadings = 10;  // Need to do some smoothing of the readings for fuel levelint readings[numReadings];  // the readings from the analog inputint index = 0; // the index of the current readingint inputPin = A0;  // analog input from fuel sensor (measured)int inputpin = A5;  // push button for fuel sensorint inputpin = A1;  // analog input from water sensorint inputpin = A4;  // push button for water sensorint inputpin = A2;  // analog input from oil sensorint inputpin = A3;  // push button for oil sensor#include <LcdBarGraph.h> // inclusion of bargraph library#include <LiquidCrystal.h> // inclusion of lcd library  LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pinsbyte lcdNumCols = 16;  // -- number of columns in the LCDLcdBarGraph lbg(&lcd, 16, 0, 1); // -- creating bargraph instancevoid setup() {  Serial.begin(9600);  // initialize serial communication with computer:  pinMode(A5,A0,A4,A1,A3,A2 INPUT); // configure analog pins as inputs  lcd.begin(16, 2);  // set up the LCD's number of columns and rows:  lcd.setCursor(0, 0); //   lcd.print("DISPLAY ON  ");  lcd.setCursor(5,1);  lcd.print("READING");  delay (5000);  lcd.setCursor(5,1);  lcd.print("         ");    }void loop() {  int valueFromSensor = analogRead(inputPin);  int averagefuelReading = returnAverageSensorValue(valueFromSensor);  int  percent = map(averagefuelReading, valueAtZeroFuel, valueAtFullFuel, 0, 100); //take a reading that is from valueAtZeroFuel to valueAtFullFuel and make it from 0 to 100    printToLCD(percent);  lbg.drawValue(percent, 100);}int returnAverageSensorValue(int newestValue){  index ++; //increment the index  if(index >= numReadings) index = 0; //reset the index if it hits 10  readings[index] = newestValue;    int sumValue = 0;  for(int i = 0; i < numReadings; i++){    sumValue += readings[i];  }    return sumValue/numReadings;}void printToLCD(int percentage){  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);}void printToLCD(int percentage){  lcd.setCursor(0, 0); // Fuel status display on LCD  lcd.print("WATER LEVEL");  lcd.print(" ");  lcd.setCursor(12, 0);  lcd.print(percentage);  lcd.setCursor(15, 0);  lcd.print("%");  lcd.setCursor(0, 1);  delay (1000);}void printToLCD(int percentage){  lcd.setCursor(0, 0); // Fuel status display on LCD  lcd.print("OIL LEVEL");  lcd.print(" ");  lcd.setCursor(12, 0);  lcd.print(percentage);  lcd.setCursor(15, 0);  lcd.print("%");  lcd.setCursor(0, 1);  delay (1000);}`
skipper

Posts: 12
Joined: Sat May 21, 2016 7:34 am

There are so many problems with this code that I don't know where to start.
You have 6 variables named inputpin - Though one is named inputPin.
You have 3 functions named printToLCD.
And, you never check the status of any of the buttons.

This looks like you stopped 33% of the way.

Once you fill in all the rest of the code, you will still have an issue where your print function has a delay in in. Remember that "delay" stops and ignores everything until it is done. So if you press a button during that time it will be completely ignored.

Here is what the code should look like. I have no clue if this works and I have no way of testing it.

Code: Select all
`#define numReadings 10int fuelSensor = A0;  // analog input from fuel sensor (measured)int fuelButton = A5;  // push button for fuel sensorint fuelReadings[numReadings];int fuelIndex = 0; // the index of the current readingint fuelZeroLevel = 0;int fuelFullLevel = 1023;int waterSensor = A1;  // analog input from water sensorint waterButton = A4;  // push button for water sensorint waterReadings[numReadings];int waterIndex = 0; // the index of the current readingint waterZeroLevel = 0;int waterFullLevel = 1023;int oilSensor = A2;  // analog input from oil sensorint oilButton = A3;  // push button for oil sensorint oilReadings[numReadings];int oilIndex = 0; // the index of the current readingint oilZeroLevel = 0;int oilFullLevel = 1023;#include <LiquidCrystal.h> // inclusion of lcd library  LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pinsbyte lcdNumCols = 16;  // -- number of columns in the LCDvoid setup() {  Serial.begin(9600);  // initialize serial communication with computer:  //enable all of your pins  pinMode(fuelSensor, INPUT);  pinMode(fuelButton, INPUT);  pinMode(waterSensor, INPUT);  pinMode(waterButton, INPUT);  pinMode(oilSensor, INPUT);  pinMode(oilButton, INPUT);  //enable the pullup resistors on the button pins  digitalWrite(fuelButton, HIGH);   digitalWrite(waterButton, INPUT);  digitalWrite(oilButton, INPUT);  //set up the LCD's number of columns and rows:  lcd.begin(16, 2);    lcd.setCursor(0, 0);  lcd.print("DISPLAY ON  ");  lcd.setCursor(5,1);  lcd.print("READING");  delay (5000); //Why is this needed?  lcd.setCursor(5,1);  lcd.print("         ");}void loop() {  int valueFromFuelSensor = analogRead(fuelSensor);  int averageFuelReading = returnAverageSensorValue(valueFromFuelSensor, fuelReadings, fuelIndex); //this might not actually work  int fuelPercent = map(averageFuelReading, fuelZeroLevel, fuelFullLevel, 0, 100);    int valueFromWaterSensor = analogRead(waterSensor);  int averageWaterReading = returnAverageSensorValue(valueFromWaterSensor, waterReadings, waterIndex); //this might not actually work  int waterPercent = map(averageWaterReading, waterZeroLevel, waterFullLevel, 0, 100);    int valueFromOilSensor = analogRead(oilSensor);  int averageOilReading = returnAverageSensorValue(valueFromOilSensor, oilReadings, oilIndex); //this might not actually work  int oilPercent = map(averageOilReading, oilZeroLevel, oilFullLevel, 0, 100);    //check what button is pressed  if(digitalRead(fuelButton) == LOW){    printToLCD(fuelPercent, "FUEL LEVEL");  }else if(digitalRead(waterButton) == LOW){    printToLCD(fuelPercent, "WATER LEVEL");  }else if(digitalRead(oilButton) == LOW){    printToLCD(fuelPercent, "OIL LEVEL");  } }int returnAverageSensorValue(int newestValue, int readings[numReadings], int index){  index ++; //increment the index  if(index >= numReadings) index = 0; //reset the index if it hits 10  readings[index] = newestValue;    int sumValue = 0;  for(int i = 0; i < numReadings; i++){    sumValue += readings[i];  }    return sumValue/numReadings;}void printToLCD(int percentage, String title){  lcd.setCursor(0, 0); // Fuel status display on LCD  lcd.print(title);  lcd.print(" ");  lcd.setCursor(12, 0);  lcd.print(percentage);  lcd.setCursor(15, 0);  lcd.print("%");  lcd.setCursor(0, 1);}`

skipper wrote:OOPS!!! sorry wrong code ,
this is the code but dot work

Code: Select all
`int valueAtZeroFuel = 0;int valueAtFullFuel = 1023;const int numReadings = 10;  // Need to do some smoothing of the readings for fuel levelint readings[numReadings];  // the readings from the analog inputint index = 0; // the index of the current readingint inputPin = A0;  // analog input from fuel sensor (measured)int inputpin = A5;  // push button for fuel sensorint inputpin = A1;  // analog input from water sensorint inputpin = A4;  // push button for water sensorint inputpin = A2;  // analog input from oil sensorint inputpin = A3;  // push button for oil sensor#include <LcdBarGraph.h> // inclusion of bargraph library#include <LiquidCrystal.h> // inclusion of lcd library  LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pinsbyte lcdNumCols = 16;  // -- number of columns in the LCDLcdBarGraph lbg(&lcd, 16, 0, 1); // -- creating bargraph instancevoid setup() {  Serial.begin(9600);  // initialize serial communication with computer:  pinMode(A5,A0,A4,A1,A3,A2 INPUT); // configure analog pins as inputs  lcd.begin(16, 2);  // set up the LCD's number of columns and rows:  lcd.setCursor(0, 0); //   lcd.print("DISPLAY ON  ");  lcd.setCursor(5,1);  lcd.print("READING");  delay (5000);  lcd.setCursor(5,1);  lcd.print("         ");    }void loop() {  int valueFromSensor = analogRead(inputPin);  int averagefuelReading = returnAverageSensorValue(valueFromSensor);  int  percent = map(averagefuelReading, valueAtZeroFuel, valueAtFullFuel, 0, 100); //take a reading that is from valueAtZeroFuel to valueAtFullFuel and make it from 0 to 100    printToLCD(percent);  lbg.drawValue(percent, 100);}int returnAverageSensorValue(int newestValue){  index ++; //increment the index  if(index >= numReadings) index = 0; //reset the index if it hits 10  readings[index] = newestValue;    int sumValue = 0;  for(int i = 0; i < numReadings; i++){    sumValue += readings[i];  }    return sumValue/numReadings;}void printToLCD(int percentage){  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);}void printToLCD(int percentage){  lcd.setCursor(0, 0); // Fuel status display on LCD  lcd.print("WATER LEVEL");  lcd.print(" ");  lcd.setCursor(12, 0);  lcd.print(percentage);  lcd.setCursor(15, 0);  lcd.print("%");  lcd.setCursor(0, 1);  delay (1000);}void printToLCD(int percentage){  lcd.setCursor(0, 0); // Fuel status display on LCD  lcd.print("OIL LEVEL");  lcd.print(" ");  lcd.setCursor(12, 0);  lcd.print(percentage);  lcd.setCursor(15, 0);  lcd.print("%");  lcd.setCursor(0, 1);  delay (1000);}`
ameyer
Founder

Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

thank you so much for your help !!!
the buttons works just fine, but the sensors mesurements dont work ,
as works with this code
so i have to make cobination with this 2 codes

Code: Select all
`int valueAtZeroFuel = 0;int valueAtFullFuel = 1023;const int numReadings = 10;  // Need to do some smoothing of the readings for fuel levelint readings[numReadings];  // the readings from the analog inputint index = 0; // the index of the current readingint inputPin = A0;  // analog input from fuel sendor (measured)#include <LcdBarGraph.h> // inclusion of bargraph library#include <LiquidCrystal.h> // inclusion of lcd library  LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pinsbyte lcdNumCols = 16;  // -- number of columns in the LCDLcdBarGraph lbg(&lcd, 16, 0, 1); // -- creating bargraph instancevoid setup() {  Serial.begin(9600);  // initialize serial communication with computer:  pinMode(A0, INPUT); // configure analog pins as inputs  lcd.begin(16, 2);  // set up the LCD's number of columns and rows:}void loop() {  int valueFromSensor = analogRead(inputPin);  int averagefuelReading = returnAverageSensorValue(valueFromSensor);  int  percent = map(averagefuelReading, valueAtZeroFuel, valueAtFullFuel, 0, 100); //take a reading that is from valueAtZeroFuel to valueAtFullFuel and make it from 0 to 100    printToLCD(percent);  lbg.drawValue(percent, 100);}int returnAverageSensorValue(int newestValue){  index ++; //increment the index  if(index >= numReadings) index = 0; //reset the index if it hits 10  readings[index] = newestValue;    int sumValue = 0;  for(int i = 0; i < numReadings; i++){    sumValue += readings[i];  }    return sumValue/numReadings;}void printToLCD(int percentage){  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);}`
skipper

Posts: 12
Joined: Sat May 21, 2016 7:34 am

Did any of the sensors work?

Looks like there may have been a simple mistake in the code . Try this

Code: Select all
`#define numReadings 10int fuelSensor = A0;  // analog input from fuel sensor (measured)int fuelButton = A5;  // push button for fuel sensorint fuelReadings[numReadings];int fuelIndex = 0; // the index of the current readingint fuelZeroLevel = 0;int fuelFullLevel = 1023;int waterSensor = A1;  // analog input from water sensorint waterButton = A4;  // push button for water sensorint waterReadings[numReadings];int waterIndex = 0; // the index of the current readingint waterZeroLevel = 0;int waterFullLevel = 1023;int oilSensor = A2;  // analog input from oil sensorint oilButton = A3;  // push button for oil sensorint oilReadings[numReadings];int oilIndex = 0; // the index of the current readingint oilZeroLevel = 0;int oilFullLevel = 1023;#include <LiquidCrystal.h> // inclusion of lcd library  LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pinsbyte lcdNumCols = 16;  // -- number of columns in the LCDvoid setup() {  Serial.begin(9600);  // initialize serial communication with computer:  //enable all of your pins  pinMode(fuelSensor, INPUT);  pinMode(fuelButton, INPUT);  pinMode(waterSensor, INPUT);  pinMode(waterButton, INPUT);  pinMode(oilSensor, INPUT);  pinMode(oilButton, INPUT);  //enable the pullup resistors on the button pins  digitalWrite(fuelButton, HIGH);   digitalWrite(waterButton, INPUT);  digitalWrite(oilButton, INPUT);  //set up the LCD's number of columns and rows:  lcd.begin(16, 2);    lcd.setCursor(0, 0);  lcd.print("DISPLAY ON  ");  lcd.setCursor(5,1);  lcd.print("READING");  delay (5000); //Why is this needed?  lcd.setCursor(5,1);  lcd.print("         ");}void loop() {  int valueFromFuelSensor = analogRead(fuelSensor);  int averageFuelReading = returnAverageSensorValue(valueFromFuelSensor, fuelReadings, fuelIndex); //this might not actually work  int fuelPercent = map(averageFuelReading, fuelZeroLevel, fuelFullLevel, 0, 100);    int valueFromWaterSensor = analogRead(waterSensor);  int averageWaterReading = returnAverageSensorValue(valueFromWaterSensor, waterReadings, waterIndex); //this might not actually work  int waterPercent = map(averageWaterReading, waterZeroLevel, waterFullLevel, 0, 100);    int valueFromOilSensor = analogRead(oilSensor);  int averageOilReading = returnAverageSensorValue(valueFromOilSensor, oilReadings, oilIndex); //this might not actually work  int oilPercent = map(averageOilReading, oilZeroLevel, oilFullLevel, 0, 100);    //check what button is pressed  if(digitalRead(fuelButton) == LOW){    printToLCD(fuelPercent, "FUEL LEVEL");  }else if(digitalRead(waterButton) == LOW){    printToLCD(waterPercent, "WATER LEVEL");  }else if(digitalRead(oilButton) == LOW){    printToLCD(oilPercent, "OIL LEVEL");  } }int returnAverageSensorValue(int newestValue, int readings[numReadings], int index){  index ++; //increment the index  if(index >= numReadings) index = 0; //reset the index if it hits 10  readings[index] = newestValue;    int sumValue = 0;  for(int i = 0; i < numReadings; i++){    sumValue += readings[i];  }    return sumValue/numReadings;}void printToLCD(int percentage, String title){  lcd.setCursor(0, 0); // Fuel status display on LCD  lcd.print(title);  lcd.print(" ");  lcd.setCursor(12, 0);  lcd.print(percentage);  lcd.setCursor(15, 0);  lcd.print("%");  lcd.setCursor(0, 1);}`

skipper wrote:thank you so much for your help !!!
the buttons works just fine, but the sensors mesurements dont work ,
as works with this code
so i have to make cobination with this 2 codes

Code: Select all
`int valueAtZeroFuel = 0;int valueAtFullFuel = 1023;const int numReadings = 10;  // Need to do some smoothing of the readings for fuel levelint readings[numReadings];  // the readings from the analog inputint index = 0; // the index of the current readingint inputPin = A0;  // analog input from fuel sendor (measured)#include <LcdBarGraph.h> // inclusion of bargraph library#include <LiquidCrystal.h> // inclusion of lcd library  LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pinsbyte lcdNumCols = 16;  // -- number of columns in the LCDLcdBarGraph lbg(&lcd, 16, 0, 1); // -- creating bargraph instancevoid setup() {  Serial.begin(9600);  // initialize serial communication with computer:  pinMode(A0, INPUT); // configure analog pins as inputs  lcd.begin(16, 2);  // set up the LCD's number of columns and rows:}void loop() {  int valueFromSensor = analogRead(inputPin);  int averagefuelReading = returnAverageSensorValue(valueFromSensor);  int  percent = map(averagefuelReading, valueAtZeroFuel, valueAtFullFuel, 0, 100); //take a reading that is from valueAtZeroFuel to valueAtFullFuel and make it from 0 to 100    printToLCD(percent);  lbg.drawValue(percent, 100);}int returnAverageSensorValue(int newestValue){  index ++; //increment the index  if(index >= numReadings) index = 0; //reset the index if it hits 10  readings[index] = newestValue;    int sumValue = 0;  for(int i = 0; i < numReadings; i++){    sumValue += readings[i];  }    return sumValue/numReadings;}void printToLCD(int percentage){  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);}`
ameyer
Founder

Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

With your help !!! the code is almost ready, the sensors works fine, and i make some modifications (bar graph ) the only issue is that , the buttons give me the last measurement of each sensor .

how it works now:
if I push the fuel button on the (fuel, oil, water.) .The sensor my give the last measurement.
So if I want to give my measurements in real time, I have to keep pushing the follow button

What i want : When i push the button of each sensor, (fuel. water , oil) to give real time measurements of the sensor .

this is the code
Code: Select all
`#define numReadings 10int fuelSensor = A0;  // analog input from fuel sensor (measured)int fuelButton = A5;  // push button for fuel sensorint fuelReadings[numReadings];int fuelIndex = 0; // the index of the current readingint fuelZeroLevel = 0;int fuelFullLevel = 1023;int waterSensor = A1;  // analog input from water sensorint waterButton = A4;  // push button for water sensorint waterReadings[numReadings];int waterIndex = 0; // the index of the current readingint waterZeroLevel = 0;int waterFullLevel = 1023;int oilSensor = A2;  // analog input from oil sensorint oilButton = A3;  // push button for oil sensorint oilReadings[numReadings];int oilIndex = 0; // the index of the current readingint oilZeroLevel = 0;int oilFullLevel = 1023;#include <LcdBarGraph.h> // inclusion of bargraph library#include <LiquidCrystal.h> // inclusion of lcd library  LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pinsbyte lcdNumCols = 16;  // -- number of columns in the LCDLcdBarGraph lbg(&lcd, 16, 0, 1); // -- creating bargraph instancevoid setup() {    Serial.begin(9600);  // initialize serial communication with computer:  //enable all of your pins  pinMode(fuelSensor, INPUT);  pinMode(fuelButton, INPUT);  pinMode(waterSensor, INPUT);  pinMode(waterButton, INPUT);  pinMode(oilSensor, INPUT);  pinMode(oilButton, INPUT);  //enable the pullup resistors on the button pins  digitalWrite(fuelButton, HIGH);   digitalWrite(waterButton, INPUT);  digitalWrite(oilButton, INPUT);  //set up the LCD's number of columns and rows:  lcd.begin(16, 2);    lcd.setCursor(0, 0);  lcd.print("DISPLAY ON  ");  lcd.setCursor(5,1);  lcd.print("READING");  delay (5000);  lcd.clear(); }void loop() {  int valueFromFuelSensor = analogRead(fuelSensor);  int averageFuelReading = returnAverageSensorValue(valueFromFuelSensor, fuelReadings, fuelIndex); //this might not actually work  int fuelPercent = map(averageFuelReading, fuelZeroLevel, fuelFullLevel, 0, 100);    int valueFromWaterSensor = analogRead(waterSensor);  int averageWaterReading = returnAverageSensorValue(valueFromWaterSensor, waterReadings, waterIndex); //this might not actually work  int waterPercent = map(averageWaterReading, waterZeroLevel, waterFullLevel, 0, 100);    int valueFromOilSensor = analogRead(oilSensor);  int averageOilReading = returnAverageSensorValue(valueFromOilSensor, oilReadings, oilIndex); //this might not actually work  int oilPercent = map(averageOilReading, oilZeroLevel, oilFullLevel, 0, 100);    //check what button is pressed  if(digitalRead(fuelButton) == LOW){       printToLCD(fuelPercent, "FUEL LEVEL");     lbg.drawValue(fuelPercent, 100);       }else if(digitalRead(waterButton) == LOW){       printToLCD(waterPercent, "H2O  LEVEL");     lbg.drawValue(waterPercent, 100);       }else if(digitalRead(oilButton) == LOW){       printToLCD(oilPercent, "OIL LEVEL");     lbg.drawValue(oilPercent, 100);  } }int returnAverageSensorValue(int newestValue, int readings[numReadings], int index){  index ++; //increment the index  if(index >= numReadings) index = 0; //reset the index if it hits 10  readings[index] = newestValue;    int sumValue = 0;  for(int i = 0; i < numReadings; i++){    sumValue += readings[i];  }    return sumValue/numReadings;}void printToLCD(int percentage, String title){  lcd.setCursor(0, 0); // Fuel status display on LCD  lcd.print(title);  lcd.print(" ");  lcd.setCursor(12, 0);  lcd.print(percentage);  lcd.setCursor(15, 0);  lcd.print("%");  lcd.setCursor(0, 1);}`

Again thank you for your help!!!!!!
skipper

Posts: 12
Joined: Sat May 21, 2016 7:34 am

Try this - I just made a simple change so we keep track of the last pressed button and then we show readings from that until a different button is pressed.

Code: Select all
`#define numReadings 10int whatToShow = 0;// 0 == fuel, 1 == water, 2 == oilint fuelSensor = A0;  // analog input from fuel sensor (measured)int fuelButton = A5;  // push button for fuel sensorint fuelReadings[numReadings];int fuelIndex = 0; // the index of the current readingint fuelZeroLevel = 0;int fuelFullLevel = 1023;int waterSensor = A1;  // analog input from water sensorint waterButton = A4;  // push button for water sensorint waterReadings[numReadings];int waterIndex = 0; // the index of the current readingint waterZeroLevel = 0;int waterFullLevel = 1023;int oilSensor = A2;  // analog input from oil sensorint oilButton = A3;  // push button for oil sensorint oilReadings[numReadings];int oilIndex = 0; // the index of the current readingint oilZeroLevel = 0;int oilFullLevel = 1023;#include <LcdBarGraph.h> // inclusion of bargraph library#include <LiquidCrystal.h> // inclusion of lcd library  LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pinsbyte lcdNumCols = 16;  // -- number of columns in the LCDLcdBarGraph lbg(&lcd, 16, 0, 1); // -- creating bargraph instancevoid setup() {  Serial.begin(9600);  // initialize serial communication with computer:  //enable all of your pins  pinMode(fuelSensor, INPUT);  pinMode(fuelButton, INPUT);  pinMode(waterSensor, INPUT);  pinMode(waterButton, INPUT);  pinMode(oilSensor, INPUT);  pinMode(oilButton, INPUT);  //enable the pullup resistors on the button pins  digitalWrite(fuelButton, HIGH);  digitalWrite(waterButton, INPUT);  digitalWrite(oilButton, INPUT);  //set up the LCD's number of columns and rows:  lcd.begin(16, 2);  lcd.setCursor(0, 0);  lcd.print("DISPLAY ON  ");  lcd.setCursor(5, 1);  lcd.print("READING");  delay (5000);  lcd.clear();}void loop() {  int valueFromFuelSensor = analogRead(fuelSensor);  int averageFuelReading = returnAverageSensorValue(valueFromFuelSensor, fuelReadings, fuelIndex); //this might not actually work  int fuelPercent = map(averageFuelReading, fuelZeroLevel, fuelFullLevel, 0, 100);  int valueFromWaterSensor = analogRead(waterSensor);  int averageWaterReading = returnAverageSensorValue(valueFromWaterSensor, waterReadings, waterIndex); //this might not actually work  int waterPercent = map(averageWaterReading, waterZeroLevel, waterFullLevel, 0, 100);  int valueFromOilSensor = analogRead(oilSensor);  int averageOilReading = returnAverageSensorValue(valueFromOilSensor, oilReadings, oilIndex); //this might not actually work  int oilPercent = map(averageOilReading, oilZeroLevel, oilFullLevel, 0, 100);  //check what button is pressed  if (digitalRead(fuelButton) == LOW) {    whatToShow = 0;  } else if (digitalRead(waterButton) == LOW) {    whatToShow = 1;  } else if (digitalRead(oilButton) == LOW) {    whatToShow = 2;  }  if (whatToShow == 0) {    printToLCD(fuelPercent, "FUEL LEVEL");    lbg.drawValue(fuelPercent, 100);  } else if (whatToShow == 1) {    printToLCD(waterPercent, "H2O  LEVEL");    lbg.drawValue(waterPercent, 100);  } else if (whatToShow == 2) {    printToLCD(oilPercent, "OIL LEVEL");    lbg.drawValue(oilPercent, 100);  }}int returnAverageSensorValue(int newestValue, int readings[numReadings], int index) {  index ++; //increment the index  if (index >= numReadings) index = 0; //reset the index if it hits 10  readings[index] = newestValue;  int sumValue = 0;  for (int i = 0; i < numReadings; i++) {    sumValue += readings[i];  }  return sumValue / numReadings;}void printToLCD(int percentage, String title) {  lcd.setCursor(0, 0); // Fuel status display on LCD  lcd.print(title);  lcd.print(" ");  lcd.setCursor(12, 0);  lcd.print(percentage);  lcd.setCursor(15, 0);  lcd.print("%");  lcd.setCursor(0, 1);}`

skipper wrote:With your help !!! the code is almost ready, the sensors works fine, and i make some modifications (bar graph ) the only issue is that , the buttons give me the last measurement of each sensor .

how it works now:
if I push the fuel button on the (fuel, oil, water.) .The sensor my give the last measurement.
So if I want to give my measurements in real time, I have to keep pushing the follow button

What i want : When i push the button of each sensor, (fuel. water , oil) to give real time measurements of the sensor .

this is the code
Code: Select all
`#define numReadings 10int fuelSensor = A0;  // analog input from fuel sensor (measured)int fuelButton = A5;  // push button for fuel sensorint fuelReadings[numReadings];int fuelIndex = 0; // the index of the current readingint fuelZeroLevel = 0;int fuelFullLevel = 1023;int waterSensor = A1;  // analog input from water sensorint waterButton = A4;  // push button for water sensorint waterReadings[numReadings];int waterIndex = 0; // the index of the current readingint waterZeroLevel = 0;int waterFullLevel = 1023;int oilSensor = A2;  // analog input from oil sensorint oilButton = A3;  // push button for oil sensorint oilReadings[numReadings];int oilIndex = 0; // the index of the current readingint oilZeroLevel = 0;int oilFullLevel = 1023;#include <LcdBarGraph.h> // inclusion of bargraph library#include <LiquidCrystal.h> // inclusion of lcd library  LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pinsbyte lcdNumCols = 16;  // -- number of columns in the LCDLcdBarGraph lbg(&lcd, 16, 0, 1); // -- creating bargraph instancevoid setup() {    Serial.begin(9600);  // initialize serial communication with computer:  //enable all of your pins  pinMode(fuelSensor, INPUT);  pinMode(fuelButton, INPUT);  pinMode(waterSensor, INPUT);  pinMode(waterButton, INPUT);  pinMode(oilSensor, INPUT);  pinMode(oilButton, INPUT);  //enable the pullup resistors on the button pins  digitalWrite(fuelButton, HIGH);   digitalWrite(waterButton, INPUT);  digitalWrite(oilButton, INPUT);  //set up the LCD's number of columns and rows:  lcd.begin(16, 2);    lcd.setCursor(0, 0);  lcd.print("DISPLAY ON  ");  lcd.setCursor(5,1);  lcd.print("READING");  delay (5000);  lcd.clear(); }void loop() {  int valueFromFuelSensor = analogRead(fuelSensor);  int averageFuelReading = returnAverageSensorValue(valueFromFuelSensor, fuelReadings, fuelIndex); //this might not actually work  int fuelPercent = map(averageFuelReading, fuelZeroLevel, fuelFullLevel, 0, 100);    int valueFromWaterSensor = analogRead(waterSensor);  int averageWaterReading = returnAverageSensorValue(valueFromWaterSensor, waterReadings, waterIndex); //this might not actually work  int waterPercent = map(averageWaterReading, waterZeroLevel, waterFullLevel, 0, 100);    int valueFromOilSensor = analogRead(oilSensor);  int averageOilReading = returnAverageSensorValue(valueFromOilSensor, oilReadings, oilIndex); //this might not actually work  int oilPercent = map(averageOilReading, oilZeroLevel, oilFullLevel, 0, 100);    //check what button is pressed  if(digitalRead(fuelButton) == LOW){       printToLCD(fuelPercent, "FUEL LEVEL");     lbg.drawValue(fuelPercent, 100);       }else if(digitalRead(waterButton) == LOW){       printToLCD(waterPercent, "H2O  LEVEL");     lbg.drawValue(waterPercent, 100);       }else if(digitalRead(oilButton) == LOW){       printToLCD(oilPercent, "OIL LEVEL");     lbg.drawValue(oilPercent, 100);  } }int returnAverageSensorValue(int newestValue, int readings[numReadings], int index){  index ++; //increment the index  if(index >= numReadings) index = 0; //reset the index if it hits 10  readings[index] = newestValue;    int sumValue = 0;  for(int i = 0; i < numReadings; i++){    sumValue += readings[i];  }    return sumValue/numReadings;}void printToLCD(int percentage, String title){  lcd.setCursor(0, 0); // Fuel status display on LCD  lcd.print(title);  lcd.print(" ");  lcd.setCursor(12, 0);  lcd.print(percentage);  lcd.setCursor(15, 0);  lcd.print("%");  lcd.setCursor(0, 1);}`

Again thank you for your help!!!!!!
ameyer
Founder

Posts: 3323
Joined: Thu Jan 21, 2010 11:59 pm
Location: The Bay Area

Everything works perfect !!!!!
Thank you so much, for your help in my project !!!!!
But mostly thank you because, i've learned more things about programing !!!

fantastic forum
skipper

Posts: 12
Joined: Sat May 21, 2016 7:34 am

Return to Software Help

### Who is online

Users browsing this forum: No registered users and 1 guest