Running a pump using Arduino

Hi everyone,

Noob here looking for help in achieving a simple task :D

I'm looking to have my arduino trigger one of the relays on my 8 channel board for 15 continuous minutes at every hour of a day.

My first attempt was pairing the Arduino board to one of the relays while running the blink LED script. I was unaware that the Uno couldn't delay for 15 mins. So I picked up a DS3231 RTC with hopes of having it take on the delay functions.

I was hoping to get a little input on assembling a simple script while utilizing the DS3231, Uno and the relay. Essentially, I'm looking to run a 120VAC water pump for 15 mins at every 45 mins.

Any bites? please let me know if further info is needed from my part. Thanks in advance!

I assume the timing is not critical and the 15 mins and 45 mins can be a little bit more or less. If so, you can do with out the RTC.

If ALL you are doing is turning the pump on and off, then you can use delay. Note the ALL, the use of delay stops all other processing so you will not be able to look at inputs etc during the delay.

Turn pump on

For count from 0 to 15. //loop for 15 min
Delay 60000

Turn pump off

For count from 0 to 45. //loop for 45 min
Delay 60000

Loop back to start

The use of delay is frowned upon because it stops processing. But as you say you only want to turn it on and off, this will do.

If you want to do something else then let us know the whole story.

Weedpharma

If you want to do other stuff as well then look at the blink without delay example. It should point you in the right direction.

meboosta: My first attempt was pairing the Arduino board to one of the relays while running the blink LED script. I was unaware that the Uno couldn't delay for 15 mins.

millis() and delay() use milliseconds stored in an unsigned long. That means a maximum value of 0xFFFFFFFF milliseconds which equals 49 days. You should not have trouble creating a delay of 15 minutes.

Below code should do what you want; timing not 100% accurate but close. Currently testing but that takes long ;). Note that this will only work properly if switching the relay on and off is the only thing that you want to do (else you must start using millis() and calculate if durations (15 minutes and 45 minutes) have passed).

// define relay pin
int relaypin = 13;

void setup()
{
  // initial output low
  digitalWrite(relaypin, LOW);
  // set pin mode to output
  pinMode(relaypin, OUTPUT);

  // initialize serial communication
  Serial.begin(9600);
}

void loop()
{
  // some debug
  printTime("relay on at: ");
  // set pin high
  digitalWrite(relaypin, HIGH);
  // delay 15 minutes
  delay(900000);

  // some debug
  printTime("relay off at: ");
  // set pin low
  digitalWrite(relaypin, LOW);
  // delay 45 minutes
  delay(2700000);
}

// function to get hours, minutes and seconds from millis()
// Input
// h: pointer to variable to store hours
// m: pointer to variable to store minutes
// s: pointer to variable to store seconds
// Returns:
// milliseconds since program started
unsigned long getDuration(int *h, int *m, int *s)
{
  // get milliseconds since program started
  unsigned long ms = millis();
  // calculate seconds, minutes and hours
  *s = ms / 1000;
  *m = ms / 1000 / 60;
  *h = ms / 1000 / 60 / 60;

  // return
  return ms;
}

// function to send formatted time (duration) over the serial line
// Input
// initial character string
void printTime(char *ptr)
{
  // variables for time information
  unsigned long ms;
  int hours;
  int minutes;
  int seconds;
  // buffer for serial output text (max 63 characters plus '\0')
  char buffer[64];

  // get duration that program has been running
  ms = getDuration(&hours, & minutes, & seconds);
  // place in text buffer for serial output
  sprintf(buffer, "%d:%02d:%02d (total %lu ms)", hours, minutes, seconds, ms);
  // feedback to user
  Serial.print(ptr);
  Serial.println(buffer);

}

The code does a little more than simply switching on and off; this is for debugging purposes. You can remove it from the loop function when no longer needed.

There are two additional unctions; getDuration gets the hours, minutes and seconds since the program has been running and printTime sends formatted information to the serial monitor.

// Edit: There is a bug in getDuration; after an hour, it gives 1 for the hour, 60 for the minutes (should be 0) and 3600 for the seconds (should be 0). No time to fix (need to go shopping with the lady); probably should have used modulo (%) instead of division (/).

Note that this bug does not effect the switching of the relay.

The fixed getDuration

unsigned long getDuration(int *h, int *m, int *s)
{
  // get milliseconds since program started
  unsigned long ms = millis();
  // calculate seconds, minutes and hours
  *s = ms / 1000;
  *s %= 60;
  *m = ms / 1000 / 60;
  *m %= 60;
  *h = ms / 1000 / 60 / 60;

  // return
  return ms;
}

And the output result

relay on at: 0:00:00 (total 0 ms)
relay off at: 0:14:59 (total 899999 ms)
relay on at: 1:00:00 (total 3600001 ms)
relay off at: 1:15:00 (total 4500001 ms)
relay on at: 2:00:00 (total 7200003 ms)
relay off at: 2:15:00 (total 8100003 ms)