#include <LiquidCrystal.h>
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() {
// Initialize LCD
lcd.begin(16 , 2);
}
void loop() {
// load battery icon on position 15,0
batterylevel(15,0);
}
//draw battery level in position x,y
void batterylevel(int xpos,int ypos)
{
//read the voltage and convert it to volt
double curvolt = double( readVcc() ) / 1000;
// check if voltge is bigger than 4.2 volt so this is a power source
if(curvolt > 4.2)
{
byte batlevel[8] = {
B01110,
B11111,
B10101,
B10001,
B11011,
B11011,
B11111,
B11111,
};
lcd.createChar(0 , batlevel);
lcd.setCursor(xpos,ypos);
lcd.write(byte(0));
}
if(curvolt <= 4.2 && curvolt > 4.0)
{
byte batlevel[8] = {
B01110,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
};
lcd.createChar(0 , batlevel);
lcd.setCursor(xpos,ypos);
lcd.write(byte(0));
}
if(curvolt <= 4.0 && curvolt > 3.8)
{
byte batlevel[8] = {
B01110,
B10001,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
};
lcd.createChar(0 , batlevel);
lcd.setCursor(xpos,ypos);
lcd.write(byte(0));
}
if(curvolt <= 3.8 && curvolt > 3.6)
{
byte batlevel[8] = {
B01110,
B10001,
B10001,
B11111,
B11111,
B11111,
B11111,
B11111,
};
lcd.createChar(0 , batlevel);
lcd.setCursor(xpos,ypos);
lcd.write(byte(0));
}
if(curvolt <= 3.6 && curvolt > 3.4)
{
byte batlevel[8] = {
B01110,
B10001,
B10001,
B10001,
B11111,
B11111,
B11111,
B11111,
};
lcd.createChar(0 , batlevel);
lcd.setCursor(xpos,ypos);
lcd.write(byte(0));
}
if(curvolt <= 3.4 && curvolt > 3.2)
{
byte batlevel[8] = {
B01110,
B10001,
B10001,
B10001,
B10001,
B11111,
B11111,
B11111,
};
lcd.createChar(0 , batlevel);
lcd.setCursor(xpos,ypos);
lcd.write(byte(0));
}
if(curvolt <= 3.2 && curvolt > 3.0)
{
byte batlevel[8] = {
B01110,
B10001,
B10001,
B10001,
B10001,
B10001,
B11111,
B11111,
};
lcd.createChar(0 , batlevel);
lcd.setCursor(xpos,ypos);
lcd.write(byte(0));
}
if(curvolt < 3.0)
{
byte batlevel[8] = {
B01110,
B10001,
B10001,
B10001,
B10001,
B10001,
B10001,
B11111,
};
lcd.createChar(0 , batlevel);
lcd.setCursor(xpos,ypos);
lcd.write(byte(0));
}
}
//read internal voltage
long readVcc() {
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA, ADSC));
result = ADCL;
result |= ADCH << 8;
result = 1126400L / result; // Back-calculate AVcc in mV
return result;
}
Comments