So im doing a battery status monitor for 12V battery.
I've wonderend on the internet and found that a lead acid battery usage is between 11.60V - 12.73V.
And i want an lcd to show that range in percentage.
My codes goes as follows:
/* VeneDuino v0.1 - 5.5.2014
todo:
molemmat akut?
rele katkoo autom
kytkimellä päälle. sähköt rosvolla?
kuitu releeltä sisälle?
lämpötila/ldr?
--------------------------------------------------- */
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7); // -- creating LCD instance
// custom chars
byte yksi[8] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B11111,
};
byte kaksi[8] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B11111,
B11111,
};
byte kolme[8] = {
B00000,
B00000,
B00000,
B00000,
B11111,
B11111,
B11111,
};
byte nelja[8] = {
B00000,
B00000,
B00000,
B11111,
B11111,
B11111,
B11111,
};
byte viisi[8] = {
B00000,
B00000,
B11111,
B11111,
B11111,
B11111,
B11111,
};
byte kuusi[8] = {
B00000,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
};
byte seiska[8] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
};
// end of custom chars
// variables
int sensorPin = A2; // input pin
//float lukema; // map input to
// print value as percent
int lcdPin = 6; // lcd taustavalo pin
// voltage meter variables
const int vSensPin = A0; // input for battery voltage
float vIn;
float voltageIn;
float vDisp;
float R1 = 33050;
float R2 = 15010;
byte prossa;
// start or setup -------------------------------------
void setup(){
lcd.begin(2, 16);
lcd.clear();
Serial.begin(9600);
pinMode(sensorPin, INPUT);
// settings for
pinMode(lcdPin, OUTPUT); // set lcd backgroundPin to output
analogWrite(lcdPin, 150); // set background brightness
lcd.setCursor(0, 1); // create static text
lcd.print("Tila: ");
createChars(); // create custom chars
}
void loop()
{
// voltage
vIn = analogRead(vSensPin);
voltageIn = (vIn * 4.8)/1024.0; //voltage coming out of the voltage divider
vDisp = voltageIn / (R2/(R1+R2)); //voltage to display
prossa = mapfloat(vDisp, 11.60, 12.73, 0, 100); // Change 11.60V - 12.73V to 0-100%
prossa = constrain(prossa, 0, 100); // stop overflow
// debug
Serial.print("vIn: ");
Serial.println(vIn);
Serial.print("voltageIn: ");
Serial.println(voltageIn);
Serial.print("vDisp: ");
Serial.println(vDisp);
Serial.print("prossa: ");
Serial.println(prossa);
//
// print voltage to display
lcd.setCursor(0, 0);
lcd.print("1: ");
lcd.setCursor(3, 0);
lcd.print(vDisp);
lcd.setCursor(6, 1);
lcd.print(" "); // empty bar blocks for new ones
if (prossa > 9) { // clear bar ghosts
lcd.setCursor(5, 1);
lcd.print(" ");
}
// print value bar
if (prossa <= 8) { // charge!
lcd.setCursor(5, 1);
lcd.print(" Lataa! ");
}
else if (prossa <= 12.5) { // 11.81
lcd.setCursor(5, 1);
lcd.write(byte(1));
}
else if (prossa <= 25) { // 11.96
lcd.setCursor(5, 1);
lcd.write(byte(1));
lcd.write(byte(2));
}
else if (prossa <= 37.5) { // 12.10
lcd.setCursor(5, 1);
lcd.write(byte(1));
lcd.write(byte(2));
lcd.write(byte(3));
}
else if (prossa <= 50) { // 12.24
lcd.setCursor(5, 1);
lcd.write(byte(1));
lcd.write(byte(2));
lcd.write(byte(3));
lcd.write(byte(4));
}
else if (prossa <= 62.5) { // 12.37
lcd.setCursor(5, 1);
lcd.write(byte(1));
lcd.write(byte(2));
lcd.write(byte(3));
lcd.write(byte(4));
lcd.write(byte(5));
}
else if (prossa <= 75) { // 12.62
lcd.setCursor(5, 1);
lcd.write(byte(1));
lcd.write(byte(2));
lcd.write(byte(3));
lcd.write(byte(4));
lcd.write(byte(5));
lcd.write(byte(6));
}
else if (prossa <= 100) { // 12.73
lcd.setCursor(5, 1);
lcd.write(byte(1));
lcd.write(byte(2));
lcd.write(byte(3));
lcd.write(byte(4));
lcd.write(byte(5));
lcd.write(byte(6));
lcd.write(byte(7));
}
// show % for prossa
// if (prossa > 9) {
lcd.setCursor(12, 1);
lcd.print(prossa);
lcd.setCursor(15, 1);
lcd.print("%");
// }
// Serial.println(prossa); // debug
// lcd.setCursor(10, 0);
// lcd.print(prossa);
delay(500);
}
// End of loop --------------------------------
void createChars() // function to create custom chars
{
lcd.createChar(1, yksi); // lcd.write(byte(1));
lcd.createChar(2, kaksi); // lcd.write(byte(2));
lcd.createChar(3, kolme); // lcd.write(byte(3));
lcd.createChar(4, nelja); // lcd.write(byte(4));
lcd.createChar(5, viisi); // lcd.write(byte(5));
lcd.createChar(6, kuusi); // lcd.write(byte(6));
lcd.createChar(7, seiska); // lcd.write(byte(7));
}
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
Everything else seem to be working BUT the percentage display. This seems to be because of map() funcion not supporting floats?
How could i make it work so the voltage change would be displayed as percentage?