Go Down

Topic: RTC Timer for 2 Pumps (Read 2646 times) previous topic - next topic

Vinterwoo


I am setting up a system that has two peristaltic pumps, one that runs for 6 hours during the day (8am to 2 pm), and one that runs for 6 hours during the night (10pm to 4 am). My hope is to have a dependable timer unit that can be deployed un-suprivised, and run on battery for multiple days. So my two main concerns are dependability and battery life.

My hardware consists of an Arduino Uno, a Seeed Studio Motor Shield (V2), a DS3231 RTC (though I plan to switch over to a DS1307 as my current one is giving me issues when running on battery), a 6 volt battery, and 2 DC 5 volt pumps.

I am basing my code off of a light timer by Iain Foulds here:

https://github.com/fouldsy/Arduino-RTC-Timer/blob/master/RTC_Light_Timer.pde




I have two questions concerning the best practices for this kind of RTC Timer code. As this is my first bit of code I plan to use in the real world, I'd appreciate advice in this matter



1- In my below code I have a start time and a stop time. In terms of robust code and battery usage, is it best to set a start time and a stop time? Or is it better to set a start time, and then a run time? AKA:

Start at 8 AM
Stop at 2 PM

vs

Start at 8 AM
Run for 6 hours



2-  As I have written my code below, the Arduino loops through every second to check the status of the timer. Is this "every second" looping battery intensive? Is it better to have a longer delay, or put the unit to sleep for stretches of time? Or is there a better practice for setting up a timer?

Or is the voltage used by the Arduino minimal enough that I should not be concerned (especially considering it is the pumps that will be using most of the juice).


Have a look at my code below, and thanks in advance to whomever responds!










Code: [Select]
#include "Wire.h" //Wire library is used for 12C bus communication
#include "RTClib.h"

RTC_DS1307 RTC;

#define DS3231_I2C_ADDRESS 0x68 //Define Bus address for RTC as 0x68



//MOTOR Setup
//First need to designate Pins
// Seed Shiled needs 2 objects to drive a motor- the Speed (0-255) and Direction (Forward or Reverse)
// Both of the above are declared with a Pin value using Pins 8-13 (if using Arduino for other things as well-may not want to use these digital pins????)

// For Seeed Shield:

//Pin 8 equal Out1 and Out 2 Forward (Out1 is + and Out 2 is -)
//Pin 11 equal Out1 and Out 2 Reverse (Out1 is + and Out 2 is -)
//Pin 12 equal Out3 and Out 4 Forward (Out3 is + and Out 4 is -)
//Pin 13 equal Out3 and Out 4 Reverse (Out3 is + and Out 4 is -)

//Pin 9 is speed for Out 1 and Out 2
//pin 10 is speed for Out 2 and Out 3




int pump1=8; //Define Pin for PUMP 1 (Day Time
int speed1=9; // Speed for PUMP 1 (Day Time)
int pump2=12; // Define Pin for PUMP 2 (Night Time)
int speed2=10; // Speed for PUMP 2 (Night Time)


//Timer for Pump 1
byte startHour1 = 8;    // Set our start and end times
byte startMinute1 = 0;  // Don't add leading zero to hour or minute - 7, not 07
byte endHour1 = 14;      // Use 24h format for the hour, without leading zero
byte endMinute1 = 0;

byte validStart1 = 0;    // Declare and set to 0 our start flag
byte poweredOn1 = 0;     // Declare and set to 0 our current power flag
byte validEnd1 = 0;      // Declare and set to 0 our end flag


//Timer for Pump 2
byte startHour2 = 22;    // Set our start and end times
byte startMinute2 = 0;  // Don't add leading zero to hour or minute - 7, not 07
byte endHour2 = 4;      // Use 24h format for the hour, without leading zero
byte endMinute2 = 0;

byte validStart2 = 0;    // Declare and set to 0 our start flag
byte poweredOn2 = 0;     // Declare and set to 0 our current power flag
byte validEnd2 = 0;      // Declare and set to 0 our end flag





void setup()
{
  Wire.begin();
  Serial.begin(9600);


    if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    //RTC.adjust(DateTime(__DATE__, __TIME__));
   
}


//MOTOR PINS
 pinMode(pump1, OUTPUT); // Set all pump controll pins as output
  pinMode(pump2, OUTPUT);
  pinMode(speed1, OUTPUT);
  pinMode(speed2, OUTPUT);

}


//MOTOR Functions

void pump1GO() {

  analogWrite(speed1,200); //255 is highest speed. Can set lower to run at lower RPMs. O is off.
  digitalWrite(pump1,HIGH);
  digitalWrite(pump2,LOW);

}



void pump2GO() {

  analogWrite(speed2,200); //255 is highest speed. Can set lower to run at lower RPMs. O is off.
  digitalWrite(pump2,HIGH);
  digitalWrite(pump1,LOW);

}



void pump1STOP() {

  analogWrite(speed1,200); //255 is highest speed. Can set lower to run at lower RPMs. O is off.
  digitalWrite(pump1,LOW);
  digitalWrite(pump2,LOW);

}



void pump2STOP() {

  analogWrite(speed2,200); //255 is highest speed. Can set lower to run at lower RPMs. O is off.
  digitalWrite(pump2,LOW);
  digitalWrite(pump1,LOW);

}








void loop()
{



 
//PUMP 1 Check
DateTime now = RTC.now(); // Read in what our current datestamp is from RTC
 
  if (now.second() == 0) { // Only process if we have ticked over to new minute
    if (poweredOn1 == 0) {  // Check if lights currently powered on
      checkStartTime1();    // If they are not, check if it's time to turn them on
    } else {
      checkEndTime1();      // Otherwise, check if it's time to turn them off
    }
 
    if (validStart1 == 1) { // If our timer is flagged to start, turn the lights on
      pump1GO();
    }
    if (validEnd1 == 1) {   // If our time is flagged to end, turn the lights off
      pump1STOP();
    }
  }




  //DateTime now = RTC.now(); // Read in what our current datestamp is from RTC
 
  if (now.second() == 0) { // Only process if we have ticked over to new minute
    if (poweredOn2 == 0) {  // Check if lights currently powered on
      checkStartTime2();    // If they are not, check if it's time to turn them on
    } else {
      checkEndTime2();      // Otherwise, check if it's time to turn them off
    }
 
    if (validStart2 == 1) { // If our timer is flagged to start, turn the lights on
      pump2GO();
    }
    if (validEnd2 == 1) {   // If our time is flagged to end, turn the lights off
      pump2STOP();
    }
  }


  //PRINT DATE TIME TO SERIAL MONITOR
    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();

   
  delay(1000); 
 
 
}



//PUMP 1 Routines
byte checkStartTime1() {
  DateTime now = RTC.now();  // Read in what our current datestamp is from RTC
 
  if (now.hour() == startHour1 && now.minute() == startMinute1) {
    validStart1 = 1;  // If our start hour and minute match the current time, set 'on' flags
    poweredOn1 = 1;
  } else {
    validStart1 = 0;  // Otherwise we don't need to power up the lights yet
  }
 
  return validStart1; // Return the status for powering up
}

byte checkEndTime1() {
  DateTime now = RTC.now();  // Read in what our current datestamp is from RTC
 
  if (now.hour() == endHour1 && now.minute() == endMinute1) {
    validEnd1 = 1;    // If our end hour and minute match the current time, set the 'off' flags
    poweredOn1 = 0;
  } else {
    validEnd1 = 0;    // Otherwise we don't need to power off the lights yet
  }
 
  return validEnd1;   // Return the status for powering off
}




//PUMP 2 Routines
byte checkStartTime2() {
  DateTime now = RTC.now();  // Read in what our current datestamp is from RTC
 
  if (now.hour() == startHour2 && now.minute() == startMinute2) {
    validStart2 = 1;  // If our start hour and minute match the current time, set 'on' flags
    poweredOn2 = 1;
  } else {
    validStart2 = 0;  // Otherwise we don't need to power up the lights yet
  }
 
  return validStart2; // Return the status for powering up
}

byte checkEndTime2() {
  DateTime now = RTC.now();  // Read in what our current datestamp is from RTC
 
  if (now.hour() == endHour2 && now.minute() == endMinute2) {
    validEnd2 = 1;    // If our end hour and minute match the current time, set the 'off' flags
    poweredOn2 = 0;
  } else {
    validEnd2 = 0;    // Otherwise we don't need to power off the lights yet
  }
 
  return validEnd2;   // Return the status for powering off
}

Go Up