int sensorPin = A0;
int onboardLED = 13;
int sensorValue = 0;
unsigned int batteryCapacity = 50000;
unsigned int batteryLevel = 0;
unsigned int ticks = 0; // represents time
unsigned int wait = 100; // represents wait time
double PercentFull;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(onboardLED, OUTPUT);
}
void loop () {
// put your main code here, to run repeatedly:
sensorValue = analogRead(sensorPin);
batteryLevel += sensorValue;
ticks += wait;
if(batteryLevel >= batteryCapacity) {
Serial.print(ticks);
Serial.print(" ms. ");
Serial.println("FULLY CHARGED");
batteryLevel = batteryCapacity; // to prevent integer from continuing to increase.
ticks = 0;
delay(20000); // wait for 20 seconds.
}
else {
PrintBatteryPercentage();
}
delay(wait);
}
void PrintBatteryPercentage() {
Serial.print(ticks);
Serial.print(" ms. charge at ");
// convert the integers to decimal numbers, divide them and print to Serial Monitor
PercentFull=100*((double)batteryLevel / (double)batteryCapacity);
Serial.print(PercentFull);
Serial.println("%"); // prints percentage sign and starts a new line.
}