Include the Library Code

Include the Library Code

LISTING PROGRAM

// include the library code:

#include <LiquidCrystal.h

#define charging A0

#define discharge A1

const int numReadings = 20;

int readings[numReadings]; // the readings from the analog input

int readIndex = 0; // the index of the current reading

int total = 0; // the running total

int average = 0; // the average

// initialize the library with the numbers of the interface pins

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup()

{

// set up the LCD's number of columns and rows:

lcd.begin(16, 2);

Serial.begin(9600);

for (int thisReading = 0; thisReading < numReadings; thisReading++)

readings[thisReading] = 0;

}

void loop()

{

int a = analogRead(charging);

// subtract the last reading:

total = total - readings[readIndex];

// read from the sensor:

readings[readIndex] = analogRead(discharge);

// add the reading to the total:

total = total + readings[readIndex];

// advance to the next position in the array:

readIndex = readIndex + 1;

// if we're at the end of the array...

if (readIndex >= numReadings)

{ // ...wrap around to the beginning:

readIndex = 0;

}

// calculate the average:

average = total / numReadings;

// send it to the computer as ASCII digits

Serial.println(average);

delay(1); // delay in between reads for stability

int tegangan = average * 4.88;

Serial.print("Tegangan Baterai terbaca = ");

Serial.print(tegangan);

Serial.println("mV ");

Serial.println();

Serial.println();

Serial.println();

// 1 cacahan = 0.0048

// 3 volt = 625 cacahan

// 4.2 volt = 875 cacahan

// rumus (875 - 625) * 0.4 = 100%

// Overcharging & surge protection 4.1 volt = 854 cacahan

// rumus (854-625) * 0.43 = 100%

int keluar = (average - 625)*0.43;

if (a >= 204)

{

lcd.setCursor(3, 0);

lcd.print("POWERBANK");

lcd.setCursor(0, 1);

lcd.print("Charging= ");

lcd.print(keluar);

lcd.print("% ");

delay(2000);

lcd.setCursor(0, 1);

lcd.print("Teg Batt= ");

lcd.print(tegangan);

lcd.print("mV");

delay(2000);

}

else

{

lcd.setCursor(3, 0);

lcd.print("POWERBANK");

lcd.setCursor(0, 1);

lcd.print("Discharge= ");

lcd.print(keluar);

lcd.print("% ");

delay(2000);

lcd.setCursor(0, 1);

lcd.print("Teg Batt= ");

lcd.print(tegangan);

lcd.print("mV");

delay(2000);

}

delay(500);

lcd.clear();

}

47