Você está na página 1de 2

#include <Wire.

h>
#include <LiquidCrystal_I2C.h> //we need the library for the lcd
display

LiquidCrystal_I2C lcd(0x27, 16, 2); //we ned to tell the arduino which
pins to use with the lcd

int batC = 0; //a variable with no decimal point to


hold battery percentage
int graph = 0; //a variable with no decimal point to
hold the value for the graph
int offset = 20;
int relayPin = 8;

void setup() { //only runs when the Arduino is


powered on reset

Serial.begin(9600);
lcd.begin(); //start lcd with 16 columns & 2 rows
lcd.clear(); //clear screen
pinMode(A0,INPUT); //we will use analog pin 0 to read
voltage from 18650
pinMode(relayPin, OUTPUT);

void loop() { //runs over and over

int volt = analogRead(A0);// read the input


double voltage = map(volt,0,1023, 0, 2500) + offset;// map 0-1023 to 0-2500 and
add correction offset

voltage /=100;// divide by 100 to get the decimal values

Serial.print("Voltage: ");
Serial.print(voltage);//print the voltge
Serial.println("V");
digitalWrite(relayPin, LOW);

//some logic to set values

if (voltage <5.0){ //battery @ 3.5V or less


batC = 0; //% = 0
graph = 0; //# of graph segments to light
}

if(voltage > 5.1 && voltage < 5.9){


batC = 2;
graph = 1;
}

if (voltage > 6.0 && voltage < 7.9){


batC = 12;
graph = 4;
}
if (voltage > 8.0 && voltage <9.9){
batC = 42;
graph = 7;
}

if (voltage > 10.0 && voltage < 10.9){


batC = 62;
graph = 10;
}

if (voltage > 11.0 && voltage < 11.9){


batC = 79;
graph = 13;
}

if (voltage > 12.0 && voltage < 13.4){


batC = 81;
graph = 15;
}

if (voltage > 13.5){


batC = 100;
graph = 16;
}

if (voltage > 12.0){


digitalWrite(relayPin, HIGH);
}

//send all the values to the lcd

lcd.setCursor(0,0);
lcd.print("V:");
lcd.setCursor(3,0);
lcd.print(voltage);
lcd.setCursor(10,0);
lcd.print("% :");
lcd.setCursor(13,0);
lcd.print(batC);
lcd.setCursor(0,1);
for(int m = 0; m < graph; m++){ //draw the graph (light up m number
of segments)
lcd.write(255);
}

delay(500); //wait one second to stabilize

lcd.clear(); //clear previous data from lcd

Você também pode gostar