Skip to content

Arduino Code for Water Pumps on Timer

/* 
This is a sketch for controlling driving Peristaltic Liquid Pumps with the Adafruit Motor Shield
Much of this code is adopted from Adafruit's code
This sketch will drive two pumps (or motors) for approximately 30 minutes at 5 and 6 am
Learn more on our blog at http://chicagodist.com/blogs/news
*/
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
#include "RTClib.h"
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 
// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *myMotor1 = AFMS.getMotor(1);
// You can also make another motor on port M2
Adafruit_DCMotor *myMotor2 = AFMS.getMotor(2);
RTC_DS1307 rtc;
void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Adafruit Motorshield v2 - DC Motor test!");
#ifdef AVR
  Wire.begin();
#else
  Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  
  // Set the speed to start, from 0 (off) to 255 (max speed)
  myMotor1->setSpeed(255);
  myMotor1->run(FORWARD);
  // turn on motor
  myMotor1->run(RELEASE);
  myMotor2->setSpeed(255);
  myMotor2->run(FORWARD);
  // turn on motor
  myMotor2->run(RELEASE);
  
  rtc.begin();
}
void loop() {
  DateTime now = rtc.now();
  uint8_t i;
  if (now.hour() == 5){
//    DateTime now = rtc.now();
    myMotor1->run(FORWARD);
    myMotor1->setSpeed(255);
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
//delay is measured in milliseconds, so the motor will run for 35 minutes
    delay(2100000);
    myMotor1->run(RELEASE);
//put a 25.5 minute delay in so the motor does not run again during 6:00 hour
    delay(1550000);
  }
//  myMotor1->run(RELEASE);
  if  (now.hour() == 6){
//    DateTime now = rtc.now();
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    myMotor2->run(FORWARD);
    myMotor2->setSpeed(255);
//delay is measured in milliseconds, so the motor will run for 35 minutes
    delay(2100000);
    myMotor2->run(RELEASE);
//put a 25.5 minute delay in so the motor does not run again during 6:00 hour
    delay(1550000);
}
//put the following in so we can check time via the serial monitor and make sure everything is working    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
}

Compare products

{"one"=>"Select 2 or 3 items to compare", "other"=>"{{ count }} of 3 items selected"}

Select first item to compare

Select second item to compare

Select third item to compare

Compare