Here is my code (from an Arduino Uno):
int photo = A4;
int val = 0;
int secs = 0;
int mins = 0;
int hours = 0;
int days = 0;
void setup() {
pinMode(photo, INPUT); //pin A4 is a photoresistor, which detects light
Serial.begin(9600);
}
void printTime(int secs, int mins, int hours, int days) {
Serial.print(days);
Serial.write(" days, ");
Serial.print(hours);
Serial.write(" hours, ");
Serial.print(mins);
Serial.write(" minutes, ");
Serial.print(secs);
Serial.print(" seconds");
Serial.write("\n");
}
void loop() {
val = analogRead(photo);
delay(1000);
if (val > 512) {
secs = secs + 1;
if (secs == 60) {
mins = mins + 1;
secs = 0;
if(mins == 60) {
hours = hours + 1;
mins = 0;
if(hours == 24) {
days = days + 1;
hours = 0;
}
}
}
}
//TODO: reverse order of units of time (days first, etc)
Serial.print("12");
if (val > 512) {
Serial.println(" ");
printTime(secs, mins, hours, days);
Serial.println(" ");
Serial.println("========================");
} else {
Serial.println(" ");
Serial.println("STOPPED AT");
printTime(secs, mins, hours, days);
Serial.println(" ");
Serial.println("========================");
delay(1000);
}
}
I am doing a science project for school testing generic vs brand-name battery life.