Auto-fish-feeder...how to keep track of time?

I suppose you could do something like this

if (time >= feedfishtime && !(feedfishtime < 86400000)){
feedfishtime = time + 86400000;
feed fish;
}

I'll have to think about it for a bit to see what the ramifications are.

Drift is fine. The fish won't mind; it just has to be reliable.

In that case, the pseudo-code you posted will work fine.

Is double-feeding a problem? This would occur because of a power outage.

My code as posted would have an extra feeding cycle every 49 days, it would only do it once every long loop, since feedfishtime is set from the time variable. In the case of a power outage, it would miss a feeding. Fish are pretty tolerant about feeding, they get what they can when they can in the wild. They keep the tank cleaner if you under feed them :wink:

In the case of a power outage, it would miss a feeding

That was going to be my next round of questions: What feedback should / could the system provide if the normal feeding was disrupted?

BetterSense indicated his wife would be displeased if the fish expired. I'd hate for his marital bliss to be in jeopardy because he didn't include a "been fed today" LED on the fish-feeder.

I suppose you could do something like this

Not really necessary.

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.

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. :-/

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);
}
  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 =.

Ok, so it should look more like this:

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.

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

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

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

Oops! :-[ Thanks.

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

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.

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.

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.

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

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

Thanks

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.

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.

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.