Need help with time alarms

Well this is the first time ive ever used a forum in my life, as usually google solves everything, but im totally stuck here…
im a newbie to this, and im working on an aquarium controller which i will post later, but working on a section of it which i would like it to do the following :
1: at 2 intervals during the day to rotate my servo.
i’ve looked at some other projects like jarduino etc, but cant seem to find what im looking for.

i have got 2 times set in the code below as a test and that part works correctly, but what i would like to do is instead of show the current time, show the time left until the next servo will move. i.e time = 8am and servo moves at 8:30am so want it to display 00:30 left till next alarm

here is the code below , any assistance would be greatly appreciated.

#include <Time.h>
#include <TimeAlarms.h>
#include <Servo.h>
#include <Wire.h>

Servo myservo1;

long time;

void setup()
{
Serial.begin(9600);
Serial.println(“FISHFEEDER V1”);

setTime(8,29,40,1,1,10); // set time to 8:29:40am Jan 1 2010

Alarm.alarmRepeat(8,30,0, MorningAlarm); // 8:30am every day
Alarm.alarmRepeat(17,45,0,EveningAlarm); // 5:45pm every day

myservo1.attach(9,550,2150);
myservo1.write(10);

}

void loop()
{
digitalClockDisplay();
Alarm.delay(1000); // wait one second between clock display
}

// functions to be called when an alarm triggers:
void MorningAlarm(){
Serial.println(" 1 Feeding Fish");
delay (1000);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay(100);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay (100);
myservo1.write(200);
delay (100);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay(100);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay (100);
myservo1.write(200);
delay (100);
myservo1.write(10);
}
void EveningAlarm(){
Serial.println(“2 Feeding Fish”);
time = millis();
Serial.println(time);
delay (1000);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay(100);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay (100);
myservo1.write(200);
delay (100);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay(100);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay (100);
myservo1.write(200);
delay (100);
myservo1.write(10);
}
void digitalClockDisplay()
{
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}

void printDigits(int digits)
{
// utility function for digital clock display: prints preceding colon and
//leading 0
Serial.print(":");
if(digits < 10)
Serial.print(‘0’);
Serial.print(digits);
}

Got Slightly closer , now i get " 8 Hours 429 mins" displayed on Serial.print

Anyone able to help?

just need to find out how to change the 429 minutes to a time if that makes sense, so it doesnt go higher than 60. It must be something simple, that im over looking ??

#include <Time.h>
#include <TimeAlarms.h>
#include <Servo.h>
#include <Wire.h>

Servo myservo1;

long time;
long hour1;
long time1;
long minute1;
long second1;
long milli1;
long nextfeed;
//long nextfeed;
//long feedingtime;

void setup()
{
Serial.begin(9600);
Serial.println(“FISHFEEDER V1”);

setTime(00,00,00,1,1,10); // set time to 8:29:40am Jan 1 2010

Alarm.alarmRepeat(8,0,0, MorningAlarm); // 8:30am every day
Alarm.alarmRepeat(16,0,0,EveningAlarm); // 5:45pm every day
Alarm.alarmRepeat(0,0,0,MidnightAlarm); // midnight

myservo1.attach(9,550,2150);
myservo1.write(10);

}

void loop()
{
digitalClockDisplay();
Alarm.delay(1000); // wait one second between clock display
}

// functions to be called when an alarm triggers:
void MorningAlarm(){
Serial.println(" 1 Feeding Fish");
delay (1000);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay(100);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay (100);
myservo1.write(200);
delay (100);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay(100);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay (100);
myservo1.write(200);
delay (100);
myservo1.write(10);
}
void EveningAlarm(){
Serial.println(“2 Feeding Fish”);
time = millis();
Serial.println(time);
delay (1000);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay(100);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay (100);
myservo1.write(200);
delay (100);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay(100);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay (100);
myservo1.write(200);
delay (100);
myservo1.write(10);
}

void MidnightAlarm(){
Serial.println(“2 Feeding Fish”);
time = millis();
Serial.println(time);
delay (1000);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay(100);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay (100);
myservo1.write(200);
delay (100);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay(100);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay (100);
myservo1.write(200);
delay (100);
myservo1.write(10);
}

void digitalClockDisplay()
{

hour1 = (hour() * 3600000);
minute1 = (minute() * 60000);
second1 = (second() * 1000);
milli1 = (hour1 + minute1 + second1);
nextfeed = (8 * 3600000);

// digital clock display of the time
Serial.println("");
Serial.print(hour());
Serial.print(":");
Serial.print(minute());
Serial.print(":");
Serial.print(second());
Serial.println("");
Serial.println(“NEXT FEED TIME”);
Serial.print((nextfeed - milli1) / 3600000);
Serial.print(" Hours “);
Serial.print((nextfeed - milli1) / 60000);
Serial.print(” Mins “);
Serial.println(”");

}

Anarkie: Got Slightly closer , now i get " 8 Hours 429 mins" displayed on Serial.print

void digitalClockDisplay(){

  hour1 = (hour() * 3600000);
  minute1 = (minute() * 60000);
  second1 = (second() * 1000); //why is this needed if you only output hours 
      //and minutes until next feed?
  milli1 = (hour1 + minute1 + second1);
  nextfeed = (8 * 3600000);
  
  // digital clock display of the time
  Serial.println("");
  Serial.print(hour());
  Serial.print(":");
  Serial.print(minute());
  Serial.print(":");
  Serial.print(second());
  Serial.println("");
  Serial.println("NEXT FEED TIME");
  Serial.print((nextfeed - milli1) / 3600000);
  Serial.print(" Hours ");
  Serial.print((nextfeed - milli1) / 60000); //Your error is here.
//Try instead: Serial.print(((nextfeed - milli1) % 3600000)/ 60000); 
  Serial.print(" Mins  ");  
  Serial.println("");
}

Why use milliseconds in your calculations when using minutes ( or seconds, if needed) make them much easier to understand?

Thankyou Henry, indeed that changes it into minutes.
The only weird thing happening though is when i run the code i get he following strange time change :

You can see it countdowns fine until it hit 33 seconds then says its 8 hours 0 mins again, but when the when it reaches 0:1:0

NEXT FEED TIME
7 Hours 59 Mins

then is correctly showing ;
0:1:1
NEXT FEED TIME
7 Hours 58 Mins

but this happens all the time if u see below:

FISHFEEDER V1

0:0:0
NEXT FEED TIME
8 Hours 0 Mins

0:0:1
NEXT FEED TIME
7 Hours 59 Mins

0:0:2
NEXT FEED TIME
7 Hours 59 Mins

0:0:3
NEXT FEED TIME
7 Hours 59 Mins

0:0:4
NEXT FEED TIME
7 Hours 59 Mins

0:0:5
NEXT FEED TIME
7 Hours 59 Mins

0:0:6
NEXT FEED TIME
7 Hours 59 Mins

0:0:7
NEXT FEED TIME
7 Hours 59 Mins

0:0:8
NEXT FEED TIME
7 Hours 59 Mins

0:0:9
NEXT FEED TIME
7 Hours 59 Mins

0:0:10
NEXT FEED TIME
7 Hours 59 Mins

0:0:11
NEXT FEED TIME
7 Hours 59 Mins

0:0:12
NEXT FEED TIME
7 Hours 59 Mins

0:0:13
NEXT FEED TIME
7 Hours 59 Mins

0:0:14
NEXT FEED TIME
7 Hours 59 Mins

0:0:15
NEXT FEED TIME
7 Hours 59 Mins

0:0:16
NEXT FEED TIME
7 Hours 59 Mins

0:0:17
NEXT FEED TIME
7 Hours 59 Mins

0:0:18
NEXT FEED TIME
7 Hours 59 Mins

0:0:19
NEXT FEED TIME
7 Hours 59 Mins

0:0:20
NEXT FEED TIME
7 Hours 59 Mins

0:0:21
NEXT FEED TIME
7 Hours 59 Mins

0:0:22
NEXT FEED TIME
7 Hours 59 Mins

0:0:23
NEXT FEED TIME
7 Hours 59 Mins

0:0:24
NEXT FEED TIME
7 Hours 59 Mins

0:0:25
NEXT FEED TIME
7 Hours 59 Mins

0:0:26
NEXT FEED TIME
7 Hours 59 Mins

0:0:27
NEXT FEED TIME
7 Hours 59 Mins

0:0:28
NEXT FEED TIME
7 Hours 59 Mins

0:0:29
NEXT FEED TIME
7 Hours 59 Mins

0:0:30
NEXT FEED TIME
7 Hours 59 Mins

0:0:31
NEXT FEED TIME
7 Hours 59 Mins

0:0:32
NEXT FEED TIME
7 Hours 59 Mins

0:0:33
NEXT FEED TIME
8 Hours 0 Mins

0:0:34
NEXT FEED TIME
8 Hours 0 Mins

0:0:35
NEXT FEED TIME
8 Hours 0 Mins

0:0:36
NEXT FEED TIME
8 Hours 0 Mins

0:0:37
NEXT FEED TIME
8 Hours 0 Mins

0:0:38
NEXT FEED TIME
8 Hours 0 Mins

0:0:39
NEXT FEED TIME
8 Hours 0 Mins

0:0:40
NEXT FEED TIME
8 Hours 0 Mins

0:0:41
NEXT FEED TIME
8 Hours 0 Mins

0:0:42
NEXT FEED TIME
8 Hours 0 Mins

0:0:43
NEXT FEED TIME
8 Hours 0 Mins

0:0:44
NEXT FEED TIME
8 Hours 0 Mins

0:0:45
NEXT FEED TIME
8 Hours 0 Mins

0:0:46
NEXT FEED TIME
8 Hours 0 Mins

0:0:47
NEXT FEED TIME
8 Hours 0 Mins

0:0:48
NEXT FEED TIME
8 Hours 0 Mins

0:0:49
NEXT FEED TIME
8 Hours 0 Mins

0:0:50
NEXT FEED TIME
8 Hours 0 Mins

0:0:51
NEXT FEED TIME
8 Hours 0 Mins

0:0:52
NEXT FEED TIME
8 Hours 0 Mins

0:0:53
NEXT FEED TIME
8 Hours 0 Mins

0:0:54
NEXT FEED TIME
8 Hours 0 Mins

0:0:55
NEXT FEED TIME
8 Hours 0 Mins

0:0:56
NEXT FEED TIME
8 Hours 0 Mins

0:0:57
NEXT FEED TIME
8 Hours 0 Mins

0:0:58
NEXT FEED TIME
8 Hours 0 Mins

0:0:59
NEXT FEED TIME
8 Hours 0 Mins

0:1:0
NEXT FEED TIME
7 Hours 59 Mins

0:1:1
NEXT FEED TIME
7 Hours 58 Mins

0:1:2
NEXT FEED TIME
7 Hours 58 Mins

0:1:3
NEXT FEED TIME
7 Hours 58 Mins .

// Here is the code at present : ( if you want try the code out)…
Thankyou in advance for any help .

#include <Time.h>
#include <TimeAlarms.h>
#include <Servo.h>
#include <Wire.h>

Servo myservo1;

long time;
long hour1;
long time1;
long minute1;
long second1;
long milli1;
long nextfeed;
//long nextfeed;
//long feedingtime;

void setup()
{
Serial.begin(9600);
Serial.println(“FISHFEEDER V1”);

setTime(00,00,00,1,1,10); // set time to 8:29:40am Jan 1 2010

Alarm.alarmRepeat(8,0,0, MorningAlarm); // 8:30am every day
Alarm.alarmRepeat(16,0,0,EveningAlarm); // 5:45pm every day
Alarm.alarmRepeat(0,0,0,MidnightAlarm); // midnight

myservo1.attach(9,550,2150);
myservo1.write(10);

}

void loop()
{
digitalClockDisplay();
Alarm.delay(1000); // wait one second between clock display
}

// functions to be called when an alarm triggers:
void MorningAlarm(){
Serial.println(" 1 Feeding Fish");
time = millis();
delay (1000);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay(100);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay (100);
myservo1.write(200);
delay (100);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay(100);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay (100);
myservo1.write(200);
delay (100);
myservo1.write(10);
}
void EveningAlarm(){
Serial.println(“2 Feeding Fish”);
time = millis();
Serial.println(time);
delay (1000);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay(100);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay (100);
myservo1.write(200);
delay (100);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay(100);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay (100);
myservo1.write(200);
delay (100);
myservo1.write(10);
}

void MidnightAlarm(){
Serial.println(“2 Feeding Fish”);
time = millis();
Serial.println(time);
delay (1000);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay(100);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay (100);
myservo1.write(200);
delay (100);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay(100);
myservo1.write(180);
delay(100);
myservo1.write(150);
delay (100);
myservo1.write(200);
delay (100);
myservo1.write(10);
}

void digitalClockDisplay()
{

hour1 = (hour() * 3600000);
minute1 = (minute() * 60000);
second1 = (second() * 1000);
milli1 = (hour1 + minute1 + second1);
nextfeed = (8 * 3600000);

// digital clock display of the time
Serial.println("");
Serial.print(hour());
Serial.print(":");
Serial.print(minute());
Serial.print(":");
Serial.print(second());
Serial.println("");
Serial.println(“NEXT FEED TIME”);
Serial.print((nextfeed - milli1) / 3600000);
Serial.print(" Hours “);
Serial.print(((nextfeed - milli1) % 3600000)/ 60000);
Serial.print(” Mins “);
Serial.println(”");

}

Hi and welcome to the wonderful world of Arduino.

Good on your for the effort.

Just a couple of things before someone else bites you for your posts.

When including code/sketch stuff: Start it with

and end it with

That way it is a sub-window in your post.

There is a possible problem in that every time you flash the sketch, the time is re-set to the value. It may be easier if you bought an RTC chip/card.

Sure it is a bit more code but it simplifies the timing and all that stuff.

Good luck.

Hi Oz, thanks for the reply.
I do have a RTC chip, and have coded that seperetly , but i could not find out how to call a function once a specific time has been met with the RTC.

What i have done at present is :
1: Have an RTC clock displaying time and date on and UFTF screen.
2: Show Temp , which i need to add in code that i’ve done.
3 : 3 x touch buttons which turn off relays.

thats is all working fine.
This part i was trying to make a function , so that once a specific time had been met that the servo would fire, it could either be done by a set time, or a 8 hour timer.
Im not sure which would be easier to code?
Also show on the screen time until next feeding.

Not sure how to even go about it :S.

will attach a pic of the controller so far ( RTC does work but not plugged in at present , waiting on some cables) :
pH level ( thnk im going to have to scratch that, i cant make heads or tails of the code for it )

Controlle-v1.3.jpg

A lot of the constants in your digitalClockDisplay routine are too big to fit in a signed int e.g. 60000, To avoid overflow issues, put an L suffix on them to indicate that they are long.

Anarkie:
trying to make a function , so that once a specific time had been met that the servo would fire, it could either be done by a set time, or a 8 hour timer.

I don’t understand any of that stuff above, but here it appears that you want to do something a specific time. This code

  GetClock();
  if (hour == 0 && minute == 0 && second <2)
  {
    getFileName();
  }

enables me to change a filename within two seconds of midnight. Getclock is just a subroutine for getting the time from a DS1307 module. I can’t imagine why you would ever want anything more complicated than this method. I think Time and TimeAlarms.h are more appropriate for setting time windows but this is simply an event and no library is needed.

Anarkie:
Thankyou Henry, indeed that changes it into minutes.
The only weird thing happening though is when i run the code i get he following strange time change :

You can see it countdowns fine until it hit 33 seconds then says its 8 hours 0 mins again, but when the when it reaches 0:1:0

Please use [ code][ /code] tags when posting code.

Try simplifying your code:

   myMinutes  = (hour() * 60) + minute(); //myMinutes can be an int
   nextfeed = (8 * 60); //nextfeed can be an int
  
  // digital clock display of the time
  Serial.println("");
  Serial.print(hour());
  Serial.print(":");
  Serial.print(minute());
  Serial.print(":");
  Serial.print(second());
  Serial.println("");
  Serial.println("NEXT FEED TIME");
  Serial.print((nextfeed - myMinutes) / 60);
  Serial.print(" Hours ");
  Serial.print((nextfeed - myMinutes) % 60);
  Serial.print(" Mins  ");  
  Serial.println("");
}

Does that make any difference?

How's it going with the code?

Any luck with how to make the alarm function/thing?

I am not going to try and compete with the other replies, and as you have had a few, I shall let you digest the replies and work out what you want to do.

All the best.