To start, I will give you a bit of info on my project. I am doing an associative learning experiment with fish. This will require a training period of a couple of weeks, and I would like to fully automate the process so that I will only have to spend a minute or two a day refilling the food. I am wondering if the Arduino has an internal clock function?
I will be using a servo motor to rotate a plastic tube containing a pre-rationed amount of food, and I would like this to happen every 2 hours for 6 hours from 10 AM to 4 PM (first at 10 am, second at 12 pm, third at 2 pm, last at 4 pm).
I was wondering if there was anyway for the arduino board to tell the time? For example, if I plug the Arduino board in at 9:50 AM, is there a way for it to know that it is 10:00 AM and to rotate the servo? Or do I have to tell the board to wait ten minutes in the code? And then is there anyway for me to tell it to rotate the motor again at 12:00 PM?
My reasoning for wanting to do this, is, should the board lose power, if I was to tell the board to wait a specific amount of time instead of having it reference the actual time, then the whole time schedule would get messed up. Is what I am describing possible? Please let me know if you are confused or if I am not being clear. I am very new to programming and could use as much help as possible.
I think I will try to use the default Arduino Sweep sketch and modify that. Is there an easier way to do it?
I am wondering if the Arduino has an internal clock function?
Given your requirements you should have a look at a Real Time Clock or RTC.
The DS1307 is a reasonable one, far more accurate is the DS3231.
For both libraries and examples are available.
Thank you for the reply! But why exactly would this be the best option? Is it because this would be a definite way incase the board powers down, or would it simply not be practical to try to do it without an RTC?
it sort of has a time function. what it has is "millis().
this starts counting once ever 1/1000 second fronm the moment you plug in the arduino.
using this you can tell it when to open and close the feeding window/door/whatever.
so something like this may work.
/* written by Justin Holman aka Remy5405 of Diminsional Designs
on 10/26/2014 to act as a referance point to help a fellow coder understand the logic layout of a automated fish feeding program
feel free to use as much or little of this code as you want. just please keep this text in it
*/
#include <Servo.h>
Servo foodMotor; // using a servo to rotate the food tube door. called it foodMotor
unsigned long timeSinceFeed; // make a int for the time since last feed
const long timeToFeed = 60*60*2*1000; // a value to set the 2 hrs to leave door open. 60sec times 60min times 2 hrs times 1000 for the mills
const int timeBetweenFeeding = 60*60*4*1000; // making that 6 hr delay
long timeOfFeed; // makes a blank int to assign the current feeding millis to
void setup(){
foodMotor.attach(1); // hooked your servo to pin one of arduino
foodMotor.write(0); // set the servo starting point, taking it this is the closed postion
delay(100); // lets the servo get to 0 before anymore code runs
timeSinceFeed = millis(); // starting the time tell next feeding timer. this writes the current mills time to the timeSinceFeed
}
void loop(){
if (timeSinceFeed >= timeBetweenFeeding + timeToFeed){ // checks to see if its been 6 hrs since last feed
foodMotor.write(180); // moves the servo to open the feeder
timeSinceFeed = millis(); //resets timer between feedings
timeOfFeed = millis(); //starts the time of this current feeding cycle
}
if (timeOfFeed >= timeToFeed){ // checks to see if the 2 hrs to stay open have passed
foodMotor.write(0); // if so it closes the servo controlled feeding door/window/whatever
timeOfFeed = millis(); //resets the current time stamp so if loop wont stall
}
}
If you want the system to remember the time during power off, you need a battery backed real time clock, they are available, we sell them if you are in the UK.
They connect over I2C and then you use a library to talk to it. They really are quite useful.
On it's own an Arduino can count milliseconds but can't keep counting whilst it is off.