ok added some comments to my code -
I think my issue is hardware and not software - but I'm often wrong
const int pin = 4;
unsigned long millisLastMin = 0;
unsigned long millisLast = 0;
const int pinOn = LOW;
unsigned long onSeconds = 0;
unsigned long onSecondsLast = 0;
int thisSecondOn = 0;
int pin60[60];
int pin60Pos = 0;
void setup(){
pinMode(pin, INPUT);
digitalWrite(pin,HIGH);
Serial.begin(115200);
Serial.println("Start");
}
void loop()
{
int r = digitalRead(pin);
//if hasn't been on in this second check if on now
if (thisSecondOn == 0) {
if (r == pinOn) { //do we have power
Serial.print("+"); //debug comment only
thisSecondOn = 1; //got power this second dont need to check again until next second
onSeconds++; //add to power seconds on
}
}
//new second so reset so that we check during this second time period
if (millis() - millisLast > 1000) {
millisLast = millis();
Serial.print(thisSecondOn); //debug comment only
thisSecondOn = 0;
}
//after a minute save our data
//byte for pin60[] would be better then int as should never be more then 60 for seconds in a minute - changed to int to make sure //not some overflow issue
if (millis() - millisLastMin > 60000){
millisLastMin = millis();
Serial.println("x"); //debug comment
//pin60Pos is our minutes in the hour
pin60[pin60Pos++] = (onSeconds - onSecondsLast); //save our seconds for this minute into an array
// sum our hour
int pin60Sum = 0;
for (int x=0;x<60;x++)
{
pin60Sum +=pin60[x];
}
if (pin60Pos > 59) pin60Pos = 0;
onSecondsLast = onSeconds;
//output seconds on in the hour [Hh]
//output total seconds on [Ht]
Serial.print("[Hh]");
Serial.print(pin60Sum);
Serial.print("[Ht]");
Serial.println(onSeconds);
}
}