Hello!
Hoping to get some help with a little project where I'm stuck.
Whole project is >700 lines long but I'll try to paste the connected parts.
Before setup and loop I establish:
const int s8 = 9; //arduino pin
byte data = 0; //relayboard "off"
unsigned long Minute = 60;
unsigned long spr8long = 30*Minute;
unsigned long spr8longcount = 0;
unsigned long spr8shortcount = 0;
unsigned long spr8short = 6*Minute;
In setup:
pinMode(s8, INPUT_PULLUP);
In loop:
This section is supposed to check the "clock", switch and if the intended runtime(spr8short) is larger than actual runtime(spr8shortcount).
Let's say it just turn 9am, the switch is low and spr8short > spr8shortcount, then this will activate.
Once in this while loop it will write data1 to the relayboard and start increasing spr8shortcount until it reaches spr8short and then "jump back" to the mainloop and write data0 to turn off the relay.
while (hour() >= 9 && s8state == LOW && spr8short > spr8shortcount){
if(data == 0){
data = 1; // to activate a specific relay on relayboard
digitalWrite(latchPin, LOW); // Latch is low while we load new data to register
shiftOut(dataPin, clockPin, MSBFIRST, data); // Load the data to register using ShiftOut function
digitalWrite(latchPin, HIGH); // Toggle latch to present the new data on register outputs
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Section 8 started.");
}
if (spr8short > spr8shortcount) { // if runtime is > timer
unsigned long currentSec2 = millis()/1000; // second variable from millis
spr8shortcount = (currentSec2 - spr8shortcount) + spr8shortcount; // timer to add seconds
lcd.setCursor(0,1);
lcd.print("Time remaining:");
lcd.setCursor(0,2);
lcd.print(spr8short - spr8shortcount);
lcd.setCursor(5,2);
lcd.print(" seconds.");
}
s8state = digitalRead(s8); //Check if switch is still low
}
if(data == 1){
data = 0; //turn off the relayboard
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, data);
digitalWrite(latchPin, HIGH);
lcd.clear();
}
That part seems to function as intended..
At the end of the loop, at a specific time I try to reset the spr8shortcount, and this is the part that I can't get to work.
// Timer resets!
if(hour() == 12 && minute() == 0 && second() == 1){
lcd.clear();
lcd.print("Reset short timer");
spr8shortcount = 0; //reset the timer
lcd.setCursor(0,1);
lcd.print(spr8shortcount); //print to make sure it's 0, this is usually OK
delay(2000);
lcd.clear();
}
the lcd.print(spr8shortcount); returns 0 as it should but as soon as the delay is over and I do the same command in the start of the loop(ie print the value), it suddenly has a completely different value, often higher than it has ever been.
So for example:
- Program starts and waits for 9 am
- Timer part starts, showing a new "menu screen" and happily counts up spr8shortcount
- Timer reaches spr8short and exits to the normal screen and turns off the relay
- Back at "main screen" spr8shortcount is now == spr8short (doing a lcd.print to check)
- After 'Timer reset' function starts at 12:00:01 and sets spr8shortcount to 0, lcdprint also shows 0.
- After delay(2000); and lcd.clear(); we're back at main screen again and spr8shortcount suddenly shows as a much larger value instead of 0, which in turn means the reset did not work and the timer function can't start again.
I.e .. If the spr8shortcount was 10 at the end of the timer, in the reset it shows as 0 but back in the "main loop" it's suddenly 18.
I've tried so many things that I don't even know what to do anymore, would be very happy if someone could look at this and see if they find any obvious faults..