Sprinkler Timer code (UNO with Relay Shield)

Hi,
My most recent project is a sprinkler timer. The idea is to cycle through the three zones that I have in my lawn by opening and closing one solenoid valve and the sprinkler pump.

Some hardware comments: My project uses an Arduino Uno and a 4-channel relay shield rated up to 250VAC at 10A (bought at Seeedstudio.com). The valve needs 24VAC to open and the pump needs 240VAC to start (but the pump runs off of another relay that is activated by the relay shield).

I first coded to turn on and off the system at predetermined days of the week and times. No problems there. I decided to add a few more features such as push buttons to change the scheduled sprinkling days and times (instead of re-programming), and being able to turn it on and off with a push button during off-cycle times. Here’s where I’m running into trouble. I want it to:

  1. Run the pump and valve for ten minutes when the button is pushed, unless…
  2. The “Off” button is pushed before the ten minutes are up.

The on and off function works perfectly, but the ten minute timer does not. The timer seems to work fine if it is set to 30 seconds, but anything above that and it will not turn off. Additionally at 30 seconds or less seconds, my LCD displays the correct countdown. Above that and it just spits out a bunch of random numbers.

Am I missing something? Any suggestions?

Thanks,
Andrew

Following is my code. It is very long but scroll down to or search for:
if (switchStateRead == HIGH){
interval = 60000;
previousMillis = currentMillis;
digitalWrite(pumpPin, HIGH);
digitalWrite(valvePin,HIGH);}

#include <Wire.h>
#include "LiquidCrystal.h"
#include "RTClib.h"
LiquidCrystal lcd(12, 11, 13);
RTC_DS1307 rtc;
int pumpPin = 7;    // Arduino Pin to control the motor
int valvePin = 6;
int ledPin = 10;
int readPin = 2;
int addPin = 3;
int offPin = 9;
int  weekday = 1;
int switchStateRead = 0;
boolean switchStateAdd = LOW;
boolean switchStateOff = LOW;
int interval = 10000;
long previousMillis = 0;
void setup()  { 
  // declare pin 5 to be an output:
  pinMode(pumpPin, OUTPUT);
  pinMode(valvePin, OUTPUT);
  pinMode (ledPin, OUTPUT);
  pinMode(readPin,INPUT);
  pinMode(addPin,INPUT);
  pinMode(offPin,INPUT);
  #ifdef AVR
  Wire.begin();
  #else
  Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
  #endif
  rtc.begin();
  lcd.begin(16, 2);
 Serial.begin(57600);} 
// the loop routine runs over and over again forever:
void loop()  { 
  DateTime now = rtc.now();
  digitalWrite(ledPin,HIGH);
  unsigned long currentMillis = millis();
  switchStateRead = digitalRead (readPin);
  switchStateOff = digitalRead (offPin);
  switchStateAdd = digitalRead (addPin);
int mod = now.unixtime() % 604800;
  int leftNumber;
  DateTime startSec;
  DateTime endSec;
//Thursday is 1, Friday is 2, etc. This is because unixtime's base date was Thursday January 1st, 1970:

  int  startHour = 21;
  int  startMinute = 50;
  int  startSecond = 30;
  if(weekday == 1){
     startSec = 0;
     endSec = 86400;}
  if(weekday == 2){
     startSec = 86400;
     endSec = 172800;}
  if(weekday == 3){
     startSec = 172800;
     endSec = 259200;}
  if(weekday == 4){
     startSec = 259200;
     endSec = 345600;}
  if(weekday == 5){
     startSec = 345600;
     endSec = 432000;}
  if(weekday == 6){
     startSec = 432000;
     endSec = 518400;}
  if(weekday == 7){
     startSec = 518400;
     endSec = 604800;}
  if(mod >= startSec.unixtime() && mod <= endSec.unixtime() && now.hour() == startHour && now.minute() == startMinute && now.second() == startSecond){
     digitalWrite(pumpPin,HIGH);// NO3 and COM3 Connected;
     digitalWrite(valvePin,HIGH);
     delay(5000);
     digitalWrite(pumpPin,LOW);// NO3 and COM3 Disconnected;
     digitalWrite(valvePin,LOW);
     delay(1000);
     digitalWrite(pumpPin,HIGH);// NO3 and COM3 Connected;
     digitalWrite(valvePin,HIGH);
     delay(5000);
     digitalWrite(pumpPin,LOW);// NO3 and COM3 Disconnected;
     digitalWrite(valvePin,LOW);
     delay(1000);
     digitalWrite(pumpPin,HIGH);// NO3 and COM3 Connected;
     digitalWrite(valvePin,HIGH);
     delay(5000);
     digitalWrite(pumpPin,LOW);// NO3 and COM3 Disconnected;
     digitalWrite(valvePin,LOW);}
  DateTime startSec2;
  DateTime endSec2;
  int  weekday2 = 2;
  int  startHour2 = 21;
  int  startMinute2 = 18;
  int  startSecond2 = 00;
  if(weekday2 == 1){
     startSec2 = 0;
     endSec2 = 86400;}
  if(weekday2 == 2){
     startSec2 = 86400;
     endSec2 = 172800;}
  if(weekday2 == 3){
     startSec2 = 172800;
     endSec2 = 259200;}
  if(weekday2 == 4){
     startSec2 = 259200;
     endSec2 = 345600;}
  if(weekday2 == 5){
     startSec2 = 345600;
     endSec2 = 432000;}
  if(weekday2 == 6){
     startSec2 = 432000;
     endSec2 = 518400;}
  if(weekday2 == 7){
     startSec2 = 518400;
     endSec2 = 604800;}
  if(mod >= startSec2.unixtime() && mod <= endSec2.unixtime() && now.hour() == startHour2 && now.minute() == startMinute2 && now.second() == startSecond2){
     digitalWrite(pumpPin,HIGH);// NO3 and COM3 Connected;
     digitalWrite(valvePin,HIGH);
     delay(2400000);
     digitalWrite(pumpPin,LOW);// NO3 and COM3 Disconnected;
     digitalWrite(valvePin,LOW);
     delay(60000);
     digitalWrite(pumpPin,HIGH);// NO3 and COM3 Connected;
     digitalWrite(valvePin,HIGH);
     delay(2400000);
     digitalWrite(pumpPin,LOW);// NO3 and COM3 Disconnected;
     digitalWrite(valvePin,LOW);
     delay(60000);
     digitalWrite(pumpPin,HIGH);// NO3 and COM3 Connected;
     digitalWrite(valvePin,HIGH);
     delay(2400000);
     digitalWrite(pumpPin,LOW);// NO3 and COM3 Disconnected;
     digitalWrite(valvePin,LOW);}
  if (switchStateRead == HIGH){
     interval = 60000;
     previousMillis = currentMillis;
     digitalWrite(pumpPin, HIGH);
     digitalWrite(valvePin,HIGH);}
  if(currentMillis - previousMillis > interval){
     digitalWrite(pumpPin, LOW);
     digitalWrite(valvePin,LOW);
   lcd.clear();}
   if(digitalRead(pumpPin) == HIGH && currentMillis - previousMillis < interval){
     lcd.setCursor(0,0);
     lcd.print("RUNNING"); 
     lcd.setCursor(0,1);
     lcd.print((interval/1000)-((currentMillis - previousMillis)/1000));}
      if(digitalRead(addPin) == HIGH && switchStateAdd == LOW) {
     switchStateAdd = digitalRead(addPin);
     lcd.clear();
     if(weekday == 7){
     weekday = 1;}
     else{weekday = weekday + 1;}
     if(weekday == 1){
       lcd.print("Thursday");}
        if(weekday == 2){
       lcd.print("Friday");}
           if(weekday == 3){
       lcd.print("Saturday");}
           if(weekday == 4){
       lcd.print("Sunday");}
           if(weekday == 5){
       lcd.print("Monday");}
           if(weekday == 6){
       lcd.print("Tuesday");}
           if(weekday == 7){
       lcd.print("Wednesday");}
   }
 if(digitalRead(offPin) == HIGH && switchStateOff == LOW) {
     digitalWrite(pumpPin, LOW);
     digitalWrite(valvePin, LOW);
     lcd.clear();
   switchStateOff = digitalRead(offPin);}}

Please read the sticky post, "How to use this forum - please read." You'll note that there's a suggestion that you post the smallest sketch you can come up with that illustrates the problem you've encountered. It's important to post a complete sketch, but it's also important to isolate the issue to a small, easily readable sketch. If the code is too long and complex, a reader may again skip to the next post. Your code is pretty long, and it references several libraries, at least one of which isn't included with the IDE, so it can be a chore to understand. You'll get better results if you post something shorter.

You'll also find a suggestion to post a link to any libraries you use that aren't included of the IDE distribution. That keeps us from having to hunt for them, and maybe finding a different library with the same name, or a different version of the one you're using.

Having said that, here's what I see: there's a fundamental flaw with these two statements. I'll leave it to you to figure out what it is.

int interval = 10000;
     interval = 60000;

what can an int store

Duly noted on shortening the code. Sorry about that. You're right. I forgot the int only stores values 16-bit values (that explains the 30 second limit, which actually is 32.768 second limit). I changed the variable to long (I don't think I'll need to run my sprinkler for more than 2.1 million seconds). I also got rid of the "=10000" as there's no need to declare what the variable is equal to right away.

Thanks, Andrew

So, did that fix it?