Go Down

Topic: Auto-fish-feeder...how to keep track of time? (Read 3 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 ==.

Go Up