Code part two:
void loop()
{
char result[17]=" "; //The empty string that all printing is fed to
timecounternow=millis(); //Read clock
if ((timecounternow-timecounterthen)>=(9918+(timingflag*6))) //Has 10 seconds passed since last clock reading? (9915 is too little and 9916 is too much...)
{
updateclock(); //Update clock
lastsecondsticks=sensortickcounter-lastsecondstickscounter; //Update last ten seconds pulse counter
lastsecondstickscounter=sensortickcounter;
secondsvalue=secondsvalue-seconds[clocksec]+lastsecondsticks; //Last minutes powerusage is updated
seconds[clocksec]=lastsecondsticks; //The array with last minutes powerusage is updated
secondspowerrating=secondspowerrating-secondsrating[clocksec]+powerrating/6; //Last minutes powerrating is updated
secondsrating[clocksec]=(powerrating/6); //The array with last minutes powerrating is updated
minutesvalue=minutesvalue-minutes[clockmin]+secondsvalue; //Last hours powerusage is updated
minutes[clockmin]=secondsvalue; //The array with last hours powerusage is updated
hoursvalue=hoursvalue-hours[clockhour]+minutesvalue; //Last days powerusage is updated
hours[clockhour]=minutesvalue; //The array with last days powerusage is updated
daysvalue=daysvalue-days[clockday]+hoursvalue; //Last weeks powerusage is updated
days[clockday]=hoursvalue; //The array with last weeks powerusage is updated
weeksvalue=weeksvalue-weeks[clockweek]+daysvalue; //Last four weeks powerusage is updated
weeks[clockweek]=daysvalue; //The array with last four weeks powerusage is updated
}
adc_key_in=analogRead(0); //Read value to see if a button is pressed
key=get_key(adc_key_in); //Convert reading to a button number
if (key == -1) //No button is pressed, display total usage and current rating
{
sprintf(value, "%u", sensortickcounter/1000); //Convert powerusage value to string
b=strlen(value); //Get length of string containing powerusage
for (int i=0; i<b; i++) // Write powerusage to the output string
result[i]=value[i];
if(sensortickcounter<1000000)
{
result[b]=',';
result[b+1]=((sensortickcounter%1000)/100)+48; //Get first decimal
}
if(sensortickcounter<100000)
result[b+2]=((sensortickcounter%100)/10)+48; //Get second decimal
if(sensortickcounter<10000)
result[b+3]=(sensortickcounter%10)+48; //Get third decimal
result[5]='k'; //Add kWh to the output string
result[6]='W';
result[7]='h';
// result[b+8]=48+clocksec; //Print what "10 second" we are at right now. Only for testing
sprintf(powervalue, "%d", powerrating); //Convert long powerrating to string
a=strlen(powervalue); //Get length of string containing powerrating
for (int i=1; i<=a; i++) // Write powerrating to the output string
result[15-i]=powervalue[a-i];
result[15]='W'; //Add W to the output string
lcd.cursorTo(1, 0); //Go to start of line one
lcd.printIn("Totalt Just nu");
lcd.cursorTo(2, 0); //Set cursor at start of line two
lcd.printIn(result); //Print powerusage and powerrating on LCD
}
if (key == 0) //Button corresponding to minutes is pressed, display last minutes usage and rating
{
if(minutehaspassed)
sprintf(value, "%d", secondsvalue); //Convert powerusage to string
else
sprintf(value, "%d", sensortickcounter); //Convert powerusage to string
b=strlen(value); //Get length of string containing powerusage
for (int i=0; i<b; i++) // Write powerusage to the output string
result[i]=value[i]; //Add Wh to the output string
result[b]='W';
result[b+1]='h';
result[b+3]=48+(clockmin/10); //Print what minute we are at right now. Only for testing
result[b+4]=48+(clockmin%10);
if(minutehaspassed)
sprintf(powervalue, "%u", secondspowerrating); //Convert long powerusage to string
else
sprintf(powervalue, "%u", ((secondsvalue*360)/(clocksec))); //Less than on hour has passed so minutesvalue is too small
a=strlen(powervalue); //Get length of string containing powerusage
for (int i=1; i<=a; i++) // Write powerusage to the output string
result[15-i]=powervalue[a-i]; //Add W to the output string
result[15]='W';
lcd.cursorTo(1, 0); //Go to start of line one
lcd.printIn(msgs[0]);
lcd.cursorTo(2, 0); //Set cursor at start of line two
lcd.printIn(result); //Print powerusage and powerrating on LCD
}
if (key == 1) //Button corresponding to hours is pressed, display last hours usage and rating. Works the same as for minutes
{
if(hourhaspassed)
{
sprintf(value, "%d", minutesvalue/1000);
b=strlen(value);
for(int i=0; i<b; i++)
result[i]=value[i];
if(minutesvalue<1000000)
{
result[b]=',';
result[b+1]=((minutesvalue%1000)/100)+48; //Get first decimal
}
if(minutesvalue<100000)
result[b+2]=((minutesvalue%100)/10)+48; //Get second decimal
if(minutesvalue<10000)
result[b+3]=(minutesvalue%10)+48; //Get third decimal
}
else
{
sprintf(value, "%d", sensortickcounter/1000); //Convert powerusage to string
b=strlen(value);
for(int i=0; i<b; i++)
result[i]=value[i];
if(sensortickcounter<1000000)
{
result[b]=',';
result[b+1]=((sensortickcounter%1000)/100)+48; //Get first decimal
}
if(sensortickcounter<100000)
result[b+2]=((sensortickcounter%100)/10)+48; //Get second decimal
if(sensortickcounter<10000)
result[b+3]=(sensortickcounter%10)+48; //Get third decimal
}
result[5]='k';
result[6]='W';
result[7]='h';
// result[b+8]=clockhour+65; //Only for testing
if(hourhaspassed)
sprintf(powervalue, "%u", minutesvalue); //More than one hour since the start has passed so minutesvalue is ok
else
{
sprintf(powervalue, "%u", ((minutesvalue*60)/(clockmin+1))); //Less than on hour has passed so minutesvalue is too small
result[14-strlen(powervalue)]='c';
}
a=strlen(powervalue);
for(int i=1; i<=a; i++)
result[15-i]=powervalue[a-i];
result[15]='W';
lcd.cursorTo(1, 0);
lcd.printIn(msgs[1]);
lcd.cursorTo(2, 0);
lcd.printIn(result);
}
if (key == 2) //Button corresponding to days is pressed, display last days usage and rating. Works the same as for minutes
{
if(dayhaspassed)
{
sprintf(value, "%d", hoursvalue/1000);
b=strlen(value);
for (int i=0; i<b; i++)
result[i]=value[i];
if(hoursvalue<1000000)
{
result[b]=',';
result[b+1]=((hoursvalue%1000)/100)+48; //Get first decimal
}
if(hoursvalue<100000)
result[b+2]=((hoursvalue%100)/10)+48; //Get second decimal
if(hoursvalue<10000)
result[b+3]=(hoursvalue%10)+48; //Get third decimal
}
else
{
sprintf(value, "%d", sensortickcounter/1000); //Convert powerusage to string
b=strlen(value);
for(int i=0; i<b; i++)
result[i]=value[i];
if(sensortickcounter<1000000)
{
result[b]=',';
result[b+1]=((sensortickcounter%1000)/100)+48; //Get first decimal
}
if(sensortickcounter<100000)
result[b+2]=((sensortickcounter%100)/10)+48; //Get second decimal
if(sensortickcounter<10000)
result[b+3]=(sensortickcounter%10)+48; //Get third decimal
}
result[5]='k';
result[6]='W';
result[7]='h';
// result[b+8]=48+clockday; //Only for testing
if(dayhaspassed)
sprintf(powervalue, "%u", (hoursvalue/24)); //More than one day since the start has passed so hoursvalue is ok
else
{
sprintf(powervalue, "%u", ((hoursvalue*60)/(clockhour*60+clockmin+1))); //Less than on day has passed so hoursvalue is too small
result[14-strlen(powervalue)]='c';
}
a=strlen(powervalue);
for (int i=1; i<=a; i++)
result[15-i]=powervalue[a-i];
result[15]='W';
lcd.cursorTo(1, 0);
lcd.printIn(msgs[2]);
lcd.cursorTo(2, 0);
lcd.printIn(result);
}