Hello all
I am trying to make a 16.4v battery monitor bargraph on 2 lines: one with a full length scale corresponding to an AnalogInput of 0 to 4.4 Volts; the other line is to display the full value of the Battery, diminishing as it is used.
First with the usual R divider I bring back the In value from 16.4 to 5 v. The important part is to monitor precisely the last 4.4V.
I tried to use the map function, but get nowhere.
What is your advice to get it work ?
Thanks !
OK thanks for your help, here is the code to which I added my reasoning:
Robojax is a guy who seems to have m
// I have set up a divider so the analog input would not exceed 5V.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <LcdBarGraphRobojax.h>
byte lcdNumCols = 16; // -- number of columns in the LCD
byte lcdLine = 2; // -- number of line in the LCD
byte sensorPin = 0; // -- value for this example
float inpuValue;//all sorts of variables I tried to use to decompose what was going on
float voltage;
float affichage;
float bat;
LiquidCrystal_I2C lcd(0x27, lcdNumCols, lcdLine); // -- creating LCD instance
LcdBarGraphRobojax lbg(&lcd, 16, 0, 0); // -- creating 16 character long bargraph starting at char 0 of line 0 (see video)
void setup() {
// -- initializing the LCD
lcd.backlight(); // Make sure backlight is on
lcd.init();
// -- do some delay: some time I've got broken visualization, says the guy from whom I took the LCD part
delay(500);
}
void loop()
{
inpuValue = analogRead(sensorPin);
affichage = inpuValue * (5.0 / 1023.0);
float voltage = map(analogRead(sensorPin), 0, 1023, 0, 900); //normally, this should map an analog input which will span :
// seen from the battery values : 16.4V to 12V, that is a 4.4 V span.
// so, 0 to 4.4V on the A0 should be mapped as 0 to 1023 in the first variable I called 'affichage'.
// I want the bargraph to show full at 16.4 and zero at 12V.
lbg.clearLine(1);// clear line 1 to display fresh voltage value
// -- draw bar graph from the analog value read
lbg.drawValue(voltage, 1023);
// here on the 2nd line I want to find or compute back the value of the total batt from the value at A0. Since it is 4.4, I need to add 12V constant.
lcd.setCursor (0, 1);
lcd.print("Bat");
bat = affichage + 12; //
lcd.setCursor (3, 1);
lcd.print(bat);
// then I want the actuel value at A0 to be in Volts (from 4.4V to 0V as the battery decreases) and displayed.
lcd.setCursor (9 , 1); //
lcd.print("Vr:");
lcd.print(affichage); //
delay(200);
}
What is the range of input of lbg.drawValue()? For now, I will guess 0 to 16.
float BatteryVoltage = inpuValue * (16.4 / 1023.0);
// Constrain to range of interest
if (BatteryVoltage < 12.0) // Less than 12.0V
BatteryVoltage = 12.0;
if (BatteryVoltage > 16.4) // More than 16.4V
BatteryVoltage = 16.4;
// Convert range from 12.0-16.4 to 0-16
int DisplayValue = ((BatteryVoltage - 12.0) / 4.4) * 16;
// -- draw bar graph from the analog value read
lbg.drawValue(DisplayValue, 1023);
Sorry !
I meant that the lbg.drawValue was some command from the custom lib written by this guy Robojax. I suppose it means the line of the bargraph will have 16 blocks of display.
I will try your sketch.
What I already have learned is ths use of 'constrain', which does answer to a pat of my concern.
I'll keep you posted !
Thanks again
/*
I have set up a divider so the analog input would not exceed 5V.
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <LcdBarGraphRobojax.h>
byte lcdNumCols = 16; // -- number of columns in the LCD
byte lcdLine = 2; // -- number of line in the LCD
byte sensorPin = 0; // -- value for this example
float inpuValue;//all sorts of variables I tried to use to decompose what was going on
float voltage;
float affichage;
float bat;
LiquidCrystal_I2C lcd(0x27, lcdNumCols, lcdLine); // -- creating LCD instance
LcdBarGraphRobojax lbg(&lcd, 16, 0, 0); // -- creating 16 character long bargraph starting at char 0 of line 0 (see video)
void setup() {
// -- initializing the LCD
lcd.backlight(); // Make sure backlight is on
lcd.init();
// -- do some delay: some time I've got broken visualization, says the guy from whom I took the LCD part
delay(500);
}
void loop()
{
inpuValue = analogRead(sensorPin);
affichage = inpuValue * (5.0 / 1023.0);
float voltage = map(analogRead(sensorPin), 0, 900, 0, 1023); //normally, this should map an analog input which will span :
// (seen from the battery values : 16.4V to 12V, that is a 4.4 V span).
// so, 0 to 4.4V on the A0 is equiv to 0 to 900, should be mapped as 0 to 1023 in the variable I called 'voltage'.
// I want the bargraph to show full at 16.4 and zero at 12V.
lbg.clearLine(1);// clear line 1 to display fresh voltage value
// -- draw bar graph from the analog value read
lbg.drawValue(voltage, 1023);
// here on the 2nd line I want to find or compute back the value of the total batt from the value at A0. Since it is 4.4, I need to add 12V constant.
lcd.setCursor (0, 1);
lcd.print("Bat");
bat = affichage + 12; //
lcd.setCursor (3, 1);
lcd.print(bat);
// then I want the actuel value at A0 to be in Volts (from 4.4V to 0V as the battery decreases) and displayed.
lcd.setCursor (9 , 1); //
lcd.print("Vr:");
lcd.print(affichage); //
delay(200);
}
This works as I wanted.
Thanks for helping !
Have a good evening