Go Down

Topic: Auto-fish-feeder...how to keep track of time? (Read 3888 times) previous topic - next topic

crimony

Quote
I suppose you could do something like this


Not really necessary.

Code: [Select]
unsigned long time;
unsigned long feedfishtime = 0;

void loop() {

   time = millis();

   if ((time - feedfishtime) > 86400000) {
       feedFish();
       feedfishtime = time;
   }
}


The magic of unsigned two-complement arithmetic will ensure that even when millis() rolls over and time < feedfishtime the result of the subtraction will trigger the feeding at the correct time.

svenofix

Hi,

So what if you want to stagger the times? ie having a LED light on for 10min, off for 6, on again for 10, and so on?

Here´s what I´ve got. Since I don´t have a LED light, I figured I could test it by having the arduino send the time via serial, but I´m still not sure if it´s working or would work. :-/
Code: [Select]

unsigned long time;
unsigned long ledontime = 0;
unsigned long ledofftime = 0;
int Pin = 13;

void setup() {
 pinMode(Pin, OUTPUT);
 Serial.print(9600);
}

void loop() {
 time = millis();
 
 if ((time - ledontime) == 648 + ledofftime) {
   ledOn();
   ledontime == time + ledofftime;
 }
 if ((time - ledofftime) == 216 + ledontime) {
   ledOff();
   ledofftime == time + ledontime;
 }
}

void ledOn() {
 digitalWrite(Pin, HIGH);
 Serial.Print(ledontime);
 
}

void ledOff() {
 digitalWrite(Pin, LOW);
 Serial.Print(ledofftime);
}
 

PaulS

Code: [Select]
 if ((time - ledontime) == 648 + ledofftime) {
   ledOn();
   ledontime == time + ledofftime;
 }


The first use of == is correct. It is a equality comparison operator.

The 2nd use of == is wrong. A comparison is performed, but the result is never used. To assign the value of time + ledofftime to ledontime, use =.

svenofix

Ok, so it should look more like this:

Code: [Select]
if ((time - ledontime) == 648 + ledofftime) {
   ledOn();
   ledontime = time + ledofftime;
 }


In order to test the code, I tried to get the arduino to send data whenever ledOn() or ledOff() is called with Serial.print(ledontime) or (ledofftime) or with Serial.print("Hello world"), but it doesn´t seem to be working. At least nothing gets printed when I click on Serial Monitor in the Arduino IDE.

PaulS

You probably want to use >= rather than ==.

svenofix

Nope, nothing. Just for info, I´m using the Antipasto Arduino Aardvark v0.8.36 IDE.

PaulS

In setup, that should be Serial.begin(9600), not print.

svenofix

Oops! :-[ Thanks.

Although depending on the value I put into the if statements, either the ledOn() or ledOff() will be called.

Code: [Select]
if ((time - ledontime) >= 2160 + ledofftime) {
   ledOn();
   ledontime = time + ledofftime;
 }
 if ((time - ledofftime) >= 6400 + ledontime) {
   ledOff();
   ledofftime = time + ledontime;
 }


If the first if statement has a higher value then ledOff() will be called nonstop and vice versa. Maybe if I did a switch case rather than two if statements. What I basically want to do is have the led on for a certain amount of time, then have it off for a different amount of time, then on, off, on, theoretically looping forever.

vindicate

Not to ruin the arduino fun, but wouldn't one of the outlet timers(like the kind for christmas lights) be the best bet?  It would be way cheaper and accurate.

svenofix

Quote
Not to ruin the arduino fun, but wouldn't one of the outlet timers(like the kind for christmas lights) be the best bet?  It would be way cheaper and accurate.


lol Granted, but that´d ruin the arduino fun.  ;D But I want to eventually have the information displayed on a screen (such as the touchshield slide), this way gives me a lot more options, fun, and learning experience. Rather than a christmas lights timer I could go for a DS1037 RTC.

PaulS

@svenofix
Look through your code. Imagine that millis starts at 0. go through the code, and write down what will be in time, ledontime, ledofftime, assuming the loop takes 10 milliseconds each time.

I think that you will see that you need to keep track of whether the led is on, or off, and only be concerned about the time relative to the next state change time.

In other words, you don't need ledontime and ledofftime. You need ledOn (containing 0 or 1/ true or false) and time and ledchangetime. Set ledchangetime to be some time in the future based on whether the led is currently on or off.

GnobarEl

I'm also making a fish feeder.
Could you please share your project? Hardware and software.

Thanks

Mark Bramwell

Many years ago I saw an engineering buddy make a device that did something once a day. He used a commodore PET to do the processing but had difficulties with the time of day routines.  

In the end, he purchased a $1 LCD clock module and set the alarm on the module to go off the same time each day.  

He soldered the sensor wires onto the piezo (makes the beeping noise) and had the computer react to the signal.  His plants were nicely watered each day at the same time each day.

Go buy a cheap LCD watch/clock module for a $1 and hook a pin to the speaker/sounder.  Use double sided tape and stick it to the arduino. Can't get any easier than that!

Another idea is to have the module alarm go off each day and use that as a time reference to set the time in your arduino program.


GnobarEl

#28
May 23, 2010, 04:14 pm Last Edit: May 23, 2010, 04:22 pm by GnobarEl Reason: 1
For now i'm having 2 problems.
first: no multitask with arduino, I can switch tasks every 50 milisec but for complex tasks that doesn't work.
second: time, but i will build a RTC but i liked that idea. The problem is that I will need more than on alarm a day.
Morning and Evening at least. But that's a very good idea.

thanks for your advice.

Mark Bramwell

You could still use the clock module idea.  Once a day you will know exactly what time it is and you can use millis counters to derive other times of the day.  Even if the arduino drifts a bit during the day (and I doubt if it would be more than a few seconds), tomorrow it will be dead on again when the alarm goes off and it resets itself.


Go Up