Pages: [1] 2   Go Down
Author Topic: Do action at certain time using RTC  (Read 6535 times)
0 Members and 1 Guest are viewing this topic.
Sheffield
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey everyone,

Ive just started with arduino and am slowly getting my head around it, im currently trying to build a Fishtank controller that is automated with a manual switch that can be pressed to enable feeding.

Now i have the basics in place, the switch works and activates the feeding cycle, but i am struggling to find any readup on how to program an RTC, what i want it to do is at X time turn on the lights and at x time turn them off, but then i also need this for the other relays as well.

Heres my current sketch -

Code:
/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:

// RELAYS

int pumprelay = 3;
int filterrelay = 4;
int lightsrelay = 5;
int heaterrelay =6;

//BUTTONS

int FeedingButton = 7;
int HeaterButton = 18;

//LEDS

int lightsled = 8;
int pumpled = 9;
int filterled = 10;
int FeedingLED = 11;
int heaterled = 12;


// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(lightsrelay, OUTPUT);
  pinMode(lightsled, OUTPUT);   
  pinMode(heaterrelay, OUTPUT);
  pinMode(heaterled, OUTPUT);   
  pinMode(pumprelay, OUTPUT);
  pinMode(pumpled, OUTPUT); 
  pinMode(filterrelay, OUTPUT);
  pinMode(filterled, OUTPUT);   
  pinMode(FeedingButton, INPUT);
  pinMode(FeedingLED, OUTPUT);
 
}

// the loop routine runs over and over again forever:
void loop() {
 
    // read the state of the pushbutton value:
  buttonState = digitalRead(FeedingButton);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // Start Feeding Cyle   
      Feeding();
  }
  else {
    // Continue Normal Program

 }
}

void Feeding() {
   digitalWrite(FeedingLED, HIGH); // Set Feeding LED On
  LightsOn();
  PumpOff();
  FilterOff();
  delay(600000);
  digitalWrite(FeedingLED, LOW); // Set Feeding LED Off
  LightsOff();
  PumpOn();
  FilterOn();
  delay(1000);

}

void LightsOn() {
 digitalWrite(lightsrelay, HIGH); // Set Lights On
 digitalWrite(lightsled, HIGH); // Set LED On
}

void LightsOff() {
 digitalWrite(lightsrelay, LOW); // Set Lights Off
 digitalWrite(lightsled, LOW); // Set LED Off
}

void PumpOn() {
 digitalWrite(pumprelay, HIGH); // Set Pump On
 digitalWrite(pumpled, HIGH); // Set LED On
}

void PumpOff() {
 digitalWrite(pumprelay, LOW); // Set Pump Off
 digitalWrite(pumpled, LOW); // Set LED Off
}

void FilterOn() {
 digitalWrite(filterrelay, HIGH); // Set Filter On
 digitalWrite(filterled, HIGH); // Set LED On
}

void FilterOff() {
 digitalWrite(filterrelay, LOW); // Set Filter Off
 digitalWrite(filterled, LOW); // Set LED Off
}

Anyone know of any really good guides for RTC at all ?
Logged

Matthew Hadfield

Current Project (s) - Fishtank Controller - Arduino Uno

I maybe new, but i learn fast smiley-wink

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Which RTC?
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Sheffield
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Apologies, forgot to add the most important part !

I have been considering the DS1307 RTC, do you think this will be suitable ?
Logged

Matthew Hadfield

Current Project (s) - Fishtank Controller - Arduino Uno

I maybe new, but i learn fast smiley-wink

Offline Offline
Edison Member
*
Karma: 64
Posts: 2488
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I have been considering the DS1307 RTC, do you think this will be suitable ?
Yes this is a good choice.
The Time library contains examples for the DS1307 RTC. http://playground.arduino.cc/Code/time
It also contains TimeAlarms which can be used to do things on a timed basis.
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i am struggling to find any readup on how to program an RTC

The simplest would be to use your arduino to keep time: presumably the arduino is running all the time.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i am struggling to find any readup on how to program an RTC

The simplest would be to use your arduino to keep time: presumably the arduino is running all the time.


... and the least accurate.

Your 8pm light switch-on will end up at 4am after a while smiley-wink

The Arduino isn't great at timing, chiefly because the resonator used for it isn't that accurate.  Plus the millis() function isn't all that accurate - it relies on a timer interrupt, and that can be delayed by other things happening when interrupts are disabled, or other interrupts are happening.

Not a good idea.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Sheffield
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i am struggling to find any readup on how to program an RTC

The simplest would be to use your arduino to keep time: presumably the arduino is running all the time.


It is, however i may be away from my arduino for extended periods of time, so therefore would rather use an RTC so that even if the power drops once the power comes back on the whole thing restarts itself.

Quote
I have been considering the DS1307 RTC, do you think this will be suitable ?
Yes this is a good choice.
The Time library contains examples for the DS1307 RTC. http://playground.arduino.cc/Code/time
It also contains TimeAlarms which can be used to do things on a timed basis.

Thanks for that Larry, id been reading the library but had missed a very obvious link in there, reading through some of the examples this should be fairly easy to do.
Logged

Matthew Hadfield

Current Project (s) - Fishtank Controller - Arduino Uno

I maybe new, but i learn fast smiley-wink

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 47
Posts: 2202
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I use the DS1307 and it is fine for this. You can simply use the standard time display procedure to do something specific. I use the snippet below to change a file name after midnight.

Code:
void loop() {
  GetClock();
  if (hour == 0 && minute == 0 && second <2)
  {
    getFileName();
  }
myFile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<< OPEN

tra la la.....

Logged

Offline Offline
Edison Member
*
Karma: 64
Posts: 2488
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Using TimeAlarms you can:

Code:
#include <TimeAlarms.h>    //Changed Number of  alarms dtNBR_ALARMS to 30 max 255

void setup()
{
Alarm.alarmRepeat(12,15,0, Feed);              //  Lunch                      hh,mm,ss
Alarm.alarmRepeat(8,15,0, LightOn);           //  Turn on the light
//other alarm events

}
void loop()
{
}

//***********************************************
//Lunch
void Feed()
{
  // Put Lunch code here
}
//***********************************************
//Turn on the light
void LightOn()
{
  // Put LightOn code here
}
//***********************************************

Logged

Sheffield
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok guys, got my DS1307 today, wired her up and shes working fine, however got one problem, the alarms wont trigger ! - any idea where im going wrong here as ive been reading the examples etc and cant see anything obvious -

Code:
#include <Wire.h>
#include "RTClib.h"
#include <Time.h>
#include <TimeAlarms.h>

RTC_DS1307 RTC;


// RELAYS

int pumprelay = 3;
int filterrelay = 4;
int lightsrelay = 5;
int heaterrelay = 6;

//BUTTONS

int FeedingButton = 7;
int HeaterButton = 18;

//LEDS

int lightsled = 8;
int pumpled = 9;
int filterled = 10;
int FeedingLED = 11;
int heaterled = 12;


// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

// the setup routine runs once when you press reset:
void setup() {
    Serial.begin(9600);
    Wire.begin();
    RTC.begin();
 
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
   
  }
 
  // initialize the digital pin as an output.
  pinMode(lightsrelay, OUTPUT);
  pinMode(lightsled, OUTPUT);   
  pinMode(heaterrelay, OUTPUT);
  pinMode(heaterled, OUTPUT);   
  pinMode(pumprelay, OUTPUT);
  pinMode(pumpled, OUTPUT); 
  pinMode(filterrelay, OUTPUT);
  pinMode(filterled, OUTPUT);   
  pinMode(FeedingButton, INPUT);
  pinMode(FeedingLED, OUTPUT);
  Serial.println("Startup Completed"); 
  FilterOn();
    Alarm.alarmRepeat(23,00,0, Night);  // 8:30am every day
    Alarm.alarmRepeat(06,00,0, Day);  // 8:30am every day
    Alarm.alarmRepeat(21,55,30, Feeding);  // 8:30am every day
}

// the loop routine runs over and over again forever:
void loop() {
 
  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();

 
    // read the state of the pushbutton value:
  buttonState = digitalRead(FeedingButton);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // Start Feeding Cyle   
      Feeding();
  }
  else {
    // Continue Normal Program

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

void Feeding() {
   digitalWrite(FeedingLED, HIGH); // Set Feeding LED On
     Serial.println("Feeding Cycle Started"); 
  LightsOn();
  PumpOff();
  FilterOff();
  delay(6000);
  digitalWrite(FeedingLED, LOW); // Set Feeding LED Off
    Serial.println("Feeding Cycle Ended"); 
  LightsOff();
  PumpOn();
  FilterOn();
  delay(6000);

}

void Night () {
  LightsOff();
  PumpOff();
}

void Day () {
  LightsOn();
  PumpOn();
}

void LightsOn() {
 digitalWrite(lightsrelay, HIGH); // Set Lights On
 digitalWrite(lightsled, HIGH); // Set LED On
   Serial.println("Lights On"); 
}

void LightsOff() {
 digitalWrite(lightsrelay, LOW); // Set Lights Off
 digitalWrite(lightsled, LOW); // Set LED Off
   Serial.println("Lights Off"); 
}

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

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

void FilterOn() {
 digitalWrite(filterrelay, HIGH); // Set Filter On
 digitalWrite(filterled, HIGH); // Set LED On
   Serial.println("Filter On"); 
}

void FilterOff() {
 digitalWrite(filterrelay, LOW); // Set Filter Off
 digitalWrite(filterled, LOW); // Set LED Off
   Serial.println("Filter Off"); 
}
Logged

Matthew Hadfield

Current Project (s) - Fishtank Controller - Arduino Uno

I maybe new, but i learn fast smiley-wink

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi MattHadfield,

Are you saying all other functions (leds, etc) are working?  When you push the button you get the Feeding cycle actions?

Have you run successfully the TimeAlarms sample sketch(s)?

How do you verify that your sketch is not working...have you waited til 23:00, 06:00, etc?  Or did you substitute sooner times for testing...

Nicely structured code btw smiley

Cheers,
John
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4084
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not the cause of the problem but, as an observation, you don't need the else after finding the pushbutton pressed unless you plan to do something specific in the future when you find it not pressed.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Sheffield
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

John,

Thanks, being a webdeveloper helps with the structuring a little smiley-wink

Ive run the TimeAlarms Samples, they work fine, pressing the switch the feeding cycle kicks in fine and runs, i was subbing the times to test but nothing at all happens at the time, ive tried everything i can think of, using Arduino Uno, pulling time back from the arduino works fine (See http://gyazo.com/4a7299e4d4e2c8eafaeaffd972ca5bba.png?1359500543) - Any ideas ?
Logged

Matthew Hadfield

Current Project (s) - Fishtank Controller - Arduino Uno

I maybe new, but i learn fast smiley-wink

NE PA
Offline Offline
Full Member
***
Karma: 5
Posts: 156
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Sheffield
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Quick5pnt0,

Its an alarm issue, theres no LED change either on the relay or on a seperate status LED, however these do work when called by the switch, so the alarm just is not triggering. - See here for Screenshot of what happens when button pressed - http://gyazo.com/804dadf4df3828d889f5abdc8c631c7f.png?1359502250
« Last Edit: January 29, 2013, 06:34:22 pm by MattHadfield » Logged

Matthew Hadfield

Current Project (s) - Fishtank Controller - Arduino Uno

I maybe new, but i learn fast smiley-wink

Pages: [1] 2   Go Up
Jump to: