Go Down

Topic: Do action at certain time using RTC (Read 14772 times) previous topic - next topic

johncc


I don't see anything obvious. How about putting a few serial.printlns inyour day, night, lightson, lightsoff, etc functions to see if the alarm is triggering them. At least you'll narrow it dow to a function problem or alarm problem.

He does actually have Serial.printlns in most of those already...

I don't see anything obvious either...

MattHadfield

#16
Jan 30, 2013, 12:41 am Last Edit: Jan 30, 2013, 12:55 am by MattHadfield Reason: 1
Have i really managed to stump the knowledge of all the great Arduino Coders ?

Edit - Not sure if im on the right tracks here, but i thought as a test id put this in to see what the arduinos internal time actually is, and theres a mismatch, could this be the issue ?

http://gyazo.com/ee1a4d9dfa5e6e058e29bc5735d21984.png?1359503649
Matthew Hadfield

Current Project (s) - Fishtank Controller - Arduino Uno

I maybe new, but i learn fast ;)

HazardsMind

From the example, setTime(8,29,0,1,1,11); is included in the setup, did you put this in to test it? Other than that, it should be working.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

MattHadfield


From the example, setTime(8,29,0,1,1,11); is included in the setup, did you put this in to test it? Other than that, it should be working.


Tried this, changed the alarm time to 8:30 as per the example and it worked ! So why isnt it reading from the RTC time thats my question ?
Matthew Hadfield

Current Project (s) - Fishtank Controller - Arduino Uno

I maybe new, but i learn fast ;)

HazardsMind

did you try the example TimeRTC? Look in the Setup(), it might be just what your missing.
Code: [Select]
/*
* TimeRTC.pde
* example code illustrating Time library with Real Time Clock.
*
*/

#include <Time.h> 
#include <Wire.h> 
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t

void setup()  {
  Serial.begin(9600);
  setSyncProvider(RTC.get);   // ***the function to get the time from the RTC***
  if(timeStatus()!= timeSet)
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");     
}

void loop()
{
   digitalClockDisplay(); 
   delay(1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

SFO_user

I'm trying my first hand at Arduino and want to do the same with controlling a fish tank.

Can you provide some help on what sensors and hardware you are using.

Many thanks!


jordachefl

Hello,

Here is my version of aquarium controller based on the examples above.
I will post the hardware used and photos in different post later.

Could anyone have a look at my code and let me know any improvements to simplify it.


Code: [Select]

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Time.h>
#include <TimeAlarms.h>
#include "virtuabotixRTC.h"
// Creation of the Real Time Clock Object
//CLK -> 12, DAT -> 7, RST -> 8, VCC -> +5v, GND -> GND
virtuabotixRTC myRTC(12, 7, 8);

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
//set device address for DS18B20 sensor
DeviceAddress temp_addr = {0x28, 0xFF, 0x8F, 0x06, 0x66, 0x14, 0x01, 0x18};
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float temperature;
int max_temp = 30; //maximum temperature of aquarium - switch off voltage to heater for safety

// RELAYS
int filterrelay = 9;
int lightsrelay = 6;
int heaterrelay = 5;
//int pumprelay = 10; // uncomment if the pump relay is to be used

//BUTTONS
int FilterButton = 3;
int LightButton = 11;


//feeding button variables
int FilterButtonState = LOW;
int Freading;           // the current reading from the input pin
int Fprevious = HIGH;    // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long Ftime = 0;         // the last time the output pin was toggled
long Fdebounce = 100;   // the debounce time, increase if the light output flickers

//light button variables
int LightButtonState = LOW;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = HIGH;    // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 100;   // the debounce time, increase if the light output flickers


// the setup routine runs once when you press reset:
void setup() {
     Serial.begin(9600);
     Wire.begin();
 
     lcd.begin(20,4);// set the colomns to 20 and rows to 4

     sensors.begin();// temp sensor
     sensors.setResolution(temp_addr, 10);
   
// Set the current date, and time in the following format:
// seconds, minutes, hours, day of the week, day of the month, month, year

//myRTC.setDS1302Time(30, 6, 16, 2, 25, 8, 2015);//run once only to set time to RTC // comment when test finished
//myRTC.setDS1302Time(59, 59, 8, 6, 22, 8, 2015);  //for test
 
// initialize the digital pin as an output.
  pinMode(lightsrelay, OUTPUT);     
  pinMode(heaterrelay, OUTPUT);   
 // pinMode(pumprelay, OUTPUT);
  pinMode(filterrelay, OUTPUT);

  // initialize the feed and light button as INPUT PULLUP (when button is pressed the status is LOW).
  pinMode(FilterButton, INPUT_PULLUP);
  pinMode(LightButton, INPUT_PULLUP);

  Serial.println("Startup Completed"); 

myRTC.updateTime();
setTime(myRTC.hours,myRTC.minutes,myRTC.seconds,myRTC.dayofmonth,myRTC.month,myRTC.year);
//setTime(8,59,59,1,1,11); //test settime //set time syntax setTime(hour,minutes,secundes,day,month,YY)

  //set initial mode
    if ((myRTC.hours >= 9) && (myRTC.hours < 12) ) {
       Day ();
    }
    else if ((myRTC.hours >= 12) && (myRTC.hours < 16) ) {
      Afternoon();
    }
     else if ((myRTC.hours >= 16) && (myRTC.hours < 22) ) {
      Day ();
      }
      else {
      Night ();
    }
   
    HeaterOn();//start heater
    //PumpOn();//start pump
 
    //Alarms schedule
     
    //test
    /*Alarm.alarmRepeat(18,44,5, Feeding); 
    Alarm.alarmRepeat(18,44,15, Day);
    Alarm.alarmRepeat(18,44,25, Afternoon); 
    Alarm.alarmRepeat(18,44,35, Feeding);
    Alarm.alarmRepeat(18,44,45, Day); 
    Alarm.alarmRepeat(18,44,55, Night);*/
   
    //real
    Alarm.alarmRepeat(9,0,0, Feeding);  //start day and feed for 15 min (feeder timer set at 09:05)
    Alarm.alarmRepeat(9,15,0, Day);
    Alarm.alarmRepeat(12,0,0, Afternoon);  //switch off lights in the afternoon
    Alarm.alarmRepeat(16,0,0, Feeding);  //evening feeding for 15 min (feeder timer set at 16:05)
    Alarm.alarmRepeat(16,15,0, Day);
    Alarm.alarmRepeat(22,0,0, Night); //night mode
}

// the loop routine runs over and over again forever:
void loop() {

myRTC.updateTime(); //read time from RTC DS1302

    Serial.print(myRTC.hours, DEC);
    Serial.print(':');
    Serial.print(myRTC.minutes, DEC);
    Serial.print(':');
    Serial.print(myRTC.seconds, DEC);

   
  // Print date/time to the LCD.
   
//lcd.backlight();

  lcd.setCursor(0, 0);
  lcd.print(myRTC.dayofmonth); lcd.print("/");
  lcd.print(myRTC.month); lcd.print("/");
  lcd.print(myRTC.year); lcd.print(" - ");
  lcd.print(myRTC.hours); lcd.print(":");
  lcd.print(myRTC.minutes); lcd.print(":");
  lcd.print(myRTC.seconds); lcd.print(" ");

  //print on LCD
  lcd.setCursor(0, 1);
     lcd.print("Mode:");
lcd.setCursor(10, 2);
     lcd.print("Temp:");
 lcd.setCursor(0, 2);
   lcd.print("Light:");
lcd.setCursor(0, 3);
   lcd.print("Filter:");       
lcd.setCursor(11, 3);
   lcd.print("Heat:");
 
  // temp sensor reading
    sensors.requestTemperatures();
    temperature = sensors.getTempC(temp_addr);
   Serial.print(" temp - ");
   Serial.println(temperature);

   lcd.setCursor(15, 2);
     lcd.print(temperature);

  //safety precaution - heater off if temperature > 30 C
  if (temperature > max_temp){
    HeaterOff();
  }
  else if (temperature < max_temp - 1){
HeaterOn();
  }
     
   
       //Feeding pushbutton
  Freading = digitalRead(FilterButton);
  if (Freading == HIGH && Fprevious == LOW && millis() - Ftime > Fdebounce) {
    if (FilterButtonState == HIGH){
         FilterOff ();}
    else{
        FilterOn ();}
      Ftime = millis();   
  }
  Fprevious = Freading;
 

       //Light pushbutton:
  reading = digitalRead(LightButton);
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (LightButtonState == HIGH){
         LightsOff ();}
    else{
        LightsOn ();}
      time = millis();   
  }
  previous = reading;

Alarm.delay(100); // wait one second between clock display
}

void Feeding() {
       Serial.println("***Feeding Cycle Started***"); 
  FilterOff();
  LightsOn();
// PumpOff();
   lcd.setCursor(5, 1);
     lcd.print(" FEEDING   ");
    }

void Day () {
  Serial.println("Day mode");
  LightsOn();
  FilterOn();
// PumpOn();
      lcd.setCursor(5, 1);
     lcd.print(" DAY       ");
}

void Night () {
   Serial.println("***Night mode***");
  LightsOff();
  FilterOn();
  // PumpOff();
       lcd.setCursor(5, 1);
     lcd.print(" NIGHT     ");
}

void Afternoon () {
  Serial.println("***Afternoon mode***");
  LightsOff();
  FilterOn();
// PumpOn();
       lcd.setCursor(5, 1);
     lcd.print(" AFTERNOON ");
}

void LightsOn() {
  digitalWrite(lightsrelay, LOW); // Set Lights On
  LightButtonState = HIGH;
  lcd.backlight();// LCD backlight on
   Serial.println("Lights On");
    lcd.setCursor(6, 2);
   lcd.print("ON "); 
}

void LightsOff() {
  digitalWrite(lightsrelay, HIGH); // Set Lights Off
  LightButtonState = LOW;
  lcd.noBacklight(); // LCD backlight off
   Serial.println("Lights Off");
     lcd.setCursor(6, 2);
   lcd.print("OFF");   
}

void FilterOn() {
  digitalWrite(filterrelay, LOW); // Set Filter On
  FilterButtonState = HIGH;
   Serial.println("Filter On");
     lcd.setCursor(7, 3);
   lcd.print("ON ");   
}

void FilterOff() {
  digitalWrite(filterrelay, HIGH); // Set Filter Off
  FilterButtonState = LOW;
   Serial.println("Filter Off");
        lcd.setCursor(7, 3);
   lcd.print("OFF");   
}
   
void HeaterOn() {
  digitalWrite(heaterrelay, LOW); // Set Heater On
        lcd.setCursor(16, 3);
   lcd.print("ON ");   
}

void HeaterOff() {
  digitalWrite(heaterrelay, HIGH); // Set Heater Off
        lcd.setCursor(16, 3);
   lcd.print("OFF");     
}

/* uncomment if the pump relay is to be used
 
 void PumpOn() {
   digitalWrite(pumprelay, HIGH); // Set Pump On
   Serial.println("Pump On"); 
 }

 void PumpOff() {
   digitalWrite(pumprelay, LOW); // Set Pump Off
   Serial.println("Pump Off"); 
 }*/

UKHeliBob

Make all the variables the appropriate size and make those that refer to Arduino pins const

Code: [Select]
Alarm.delay(100); // wait one second between clock displayCheck comments and make sure that they reflect what the code does.  Use Ctrl/T to format the code consistently.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

jordachefl

#23
Aug 27, 2015, 01:07 pm Last Edit: Aug 27, 2015, 01:08 pm by jordachefl
Are you refering at this

Code: [Select]

// RELAYS
int filterrelay = 9;
int lightsrelay = 6;
int heaterrelay = 5;
//int pumprelay = 10; // uncomment if the pump relay is to be used

//BUTTONS
int FilterButton = 3;
int LightButton = 11;


To be changed in this

Code: [Select]

// RELAYS
const filterrelay = 9;
const lightsrelay = 6;
const heaterrelay = 5;
//const pumprelay = 10; // uncomment if the pump relay is to be used

//BUTTONS
const FilterButton = 3;
const LightButton = 11;


What is the reason

UKHeliBob

Quote
Are you refering at this
Yes, but the suggestion was to change them to
Code: [Select]

// RELAYS
const byte filterrelay = 9;
const byte lightsrelay = 6;
const byte heaterrelay = 5;
//const byte pumprelay = 10; // uncomment if the pump relay is to be used

//BUTTONS
const byte FilterButton = 3;
const byte LightButton = 11;

const because none of the values will change when the program runs.  If you do try to change them by accident within the program if you revise it then the compiler will stop you making that mistake.

byte because none of them will ever have a value larger than 255 and ints take twice as much memory as bytes.  Not particularly important in this program but it is good practice to use variables of an appropriate size, which can be faster to manipulate and it is always good to save memory.  There are other reasons too to do with interrupts but they don't apply unless/until you use them.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

jordachefl

Did the modifications and auto format. New sketch is smaller by 10 bytes.

Code: [Select]

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Time.h>
#include <TimeAlarms.h>
#include "virtuabotixRTC.h"
// Creation of the Real Time Clock Object
//CLK -> 12, DAT -> 7, RST -> 8, VCC -> +5v, GND -> GND
virtuabotixRTC myRTC(12, 7, 8);

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
//set device address for DS18B20 sensor
DeviceAddress temp_addr = {0x28, 0xFF, 0x8F, 0x06, 0x66, 0x14, 0x01, 0x18};
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float temperature;
int max_temp = 30; //maximum temperature of aquarium - switch off voltage to heater for safety

// RELAYS
const byte filterrelay = 9;
const byte lightsrelay = 6;
const byte heaterrelay = 5;
//const byte pumprelay = 10; // uncomment if the pump relay is to be used

//BUTTONS
const byte FilterButton = 3;
const byte LightButton = 11;


//feeding button variables
int FilterButtonState = LOW;
int Freading;           // the current reading from the input pin
int Fprevious = HIGH;    // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long Ftime = 0;         // the last time the output pin was toggled
long Fdebounce = 100;   // the debounce time, increase if the light output flickers

//light button variables
int LightButtonState = LOW;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = HIGH;    // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 100;   // the debounce time, increase if the light output flickers


// the setup routine runs once when you press reset:
void setup() {
  Serial.begin(9600);
  Wire.begin();

  lcd.begin(20, 4); // set the colomns to 20 and rows to 4

  sensors.begin();// temp sensor
  sensors.setResolution(temp_addr, 10);

  // Set the current date, and time in the following format:
  // seconds, minutes, hours, day of the week, day of the month, month, year

  //myRTC.setDS1302Time(30, 6, 16, 2, 25, 8, 2015);//run once only to set time to RTC // comment when test finished
  //myRTC.setDS1302Time(59, 59, 8, 6, 22, 8, 2015);  //for test

  // initialize the digital pin as an output.
  pinMode(lightsrelay, OUTPUT);
  pinMode(heaterrelay, OUTPUT);
  // pinMode(pumprelay, OUTPUT);
  pinMode(filterrelay, OUTPUT);

  // initialize the feed and light button as INPUT PULLUP (when button is pressed the status is LOW).
  pinMode(FilterButton, INPUT_PULLUP);
  pinMode(LightButton, INPUT_PULLUP);

  Serial.println("Startup Completed");

  myRTC.updateTime();
  setTime(myRTC.hours, myRTC.minutes, myRTC.seconds, myRTC.dayofmonth, myRTC.month, myRTC.year);
  //setTime(8,59,59,1,1,11); //test settime //set time syntax setTime(hour,minutes,secundes,day,month,YY)

  //set initial mode
  if ((myRTC.hours >= 9) && (myRTC.hours < 12) ) {
    Day ();
  }
  else if ((myRTC.hours >= 12) && (myRTC.hours < 16) ) {
    Afternoon();
  }
  else if ((myRTC.hours >= 16) && (myRTC.hours < 22) ) {
    Day ();
  }
  else {
    Night ();
  }

  HeaterOn();//start heater
  //PumpOn();//start pump

  //Alarms schedule

  //for test
  /*Alarm.alarmRepeat(18,44,5, Feeding);
  Alarm.alarmRepeat(18,44,15, Day);
  Alarm.alarmRepeat(18,44,25, Afternoon);
  Alarm.alarmRepeat(18,44,35, Feeding);
  Alarm.alarmRepeat(18,44,45, Day);
  Alarm.alarmRepeat(18,44,55, Night);*/

  //real
  Alarm.alarmRepeat(9, 0, 0, Feeding); //start day and feed for 15 min (feeder timer set at 09:05)
  Alarm.alarmRepeat(9, 15, 0, Day);
  Alarm.alarmRepeat(12, 0, 0, Afternoon); //switch off lights in the afternoon
  Alarm.alarmRepeat(16, 0, 0, Feeding); //evening feeding for 15 min (feeder timer set at 16:05)
  Alarm.alarmRepeat(16, 15, 0, Day);
  Alarm.alarmRepeat(22, 0, 0, Night); //night mode
}

// the loop routine runs over and over again forever:
void loop() {

  myRTC.updateTime(); //read time from RTC DS1302

  Serial.print(myRTC.hours, DEC);
  Serial.print(':');
  Serial.print(myRTC.minutes, DEC);
  Serial.print(':');
  Serial.print(myRTC.seconds, DEC);


  // Print date/time to the LCD
  //lcd.backlight();

  lcd.setCursor(0, 0);
  lcd.print(myRTC.dayofmonth); lcd.print("/");
  lcd.print(myRTC.month); lcd.print("/");
  lcd.print(myRTC.year); lcd.print(" - ");
  lcd.print(myRTC.hours); lcd.print(":");
  lcd.print(myRTC.minutes); lcd.print(":");
  lcd.print(myRTC.seconds); lcd.print(" ");

  //print on LCD
  lcd.setCursor(0, 1);
  lcd.print("Mode:");
  lcd.setCursor(10, 2);
  lcd.print("Temp:");
  lcd.setCursor(0, 2);
  lcd.print("Light:");
  lcd.setCursor(0, 3);
  lcd.print("Filter:");
  lcd.setCursor(11, 3);
  lcd.print("Heat:");

  // temp sensor reading
  sensors.requestTemperatures();
  temperature = sensors.getTempC(temp_addr);
  Serial.print(" temp - ");
  Serial.println(temperature);

  lcd.setCursor(15, 2);
  lcd.print(temperature);

  //safety precaution - heater off if temperature > 30 C
  if (temperature > max_temp) {
    HeaterOff();
  }
  else if (temperature < max_temp - 1) {
    HeaterOn();
  }


  //Feeding pushbutton
  Freading = digitalRead(FilterButton);
  if (Freading == HIGH && Fprevious == LOW && millis() - Ftime > Fdebounce) {
    if (FilterButtonState == HIGH) {
      FilterOff ();
    }
    else {
      FilterOn ();
    }
    Ftime = millis();
  }
  Fprevious = Freading;


  //Light pushbutton:
  reading = digitalRead(LightButton);
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (LightButtonState == HIGH) {
      LightsOff ();
    }
    else {
      LightsOn ();
    }
    time = millis();
  }
  previous = reading;

  // Alarm.delay(1000); // wait one second between clock display
}

void Feeding() {
  Serial.println("***Feeding Cycle Started***");
  FilterOff();
  LightsOn();
  // PumpOff();
  lcd.setCursor(5, 1);
  lcd.print(" FEEDING   ");
}

void Day () {
  Serial.println("***Day mode***");
  LightsOn();
  FilterOn();
  // PumpOn();
  lcd.setCursor(5, 1);
  lcd.print(" DAY       ");
}

void Night () {
  Serial.println("***Night mode***");
  LightsOff();
  FilterOn();
  // PumpOff();
  lcd.setCursor(5, 1);
  lcd.print(" NIGHT     ");
}

void Afternoon () {
  Serial.println("***Afternoon mode***");
  LightsOff();
  FilterOn();
  // PumpOn();
  lcd.setCursor(5, 1);
  lcd.print(" AFTERNOON ");
}

void LightsOn() {
  digitalWrite(lightsrelay, LOW); // Set Lights On
  LightButtonState = HIGH;
  lcd.backlight();// LCD backlight on
  Serial.println("Lights On");
  lcd.setCursor(6, 2);
  lcd.print("ON ");
}

void LightsOff() {
  digitalWrite(lightsrelay, HIGH); // Set Lights Off
  LightButtonState = LOW;
  lcd.noBacklight(); // LCD backlight off
  Serial.println("Lights Off");
  lcd.setCursor(6, 2);
  lcd.print("OFF");
}

void FilterOn() {
  digitalWrite(filterrelay, LOW); // Set Filter On
  FilterButtonState = HIGH;
  Serial.println("Filter On");
  lcd.setCursor(7, 3);
  lcd.print("ON ");
}

void FilterOff() {
  digitalWrite(filterrelay, HIGH); // Set Filter Off
  FilterButtonState = LOW;
  Serial.println("Filter Off");
  lcd.setCursor(7, 3);
  lcd.print("OFF");
}

void HeaterOn() {
  digitalWrite(heaterrelay, LOW); // Set Heater On
  lcd.setCursor(16, 3);
  lcd.print("ON ");
}

void HeaterOff() {
  digitalWrite(heaterrelay, HIGH); // Set Heater Off
  lcd.setCursor(16, 3);
  lcd.print("OFF");
}

/* uncomment if the pump relay is to be used

 void PumpOn() {
   digitalWrite(pumprelay, HIGH); // Set Pump On
   Serial.println("Pump On");
 }

 void PumpOff() {
   digitalWrite(pumprelay, LOW); // Set Pump Off
   Serial.println("Pump Off");
 }*/

Go Up