using arduino timer

I am taking user input, so timer should run in arduino as per user requirements. For example if user enters three times i.e 13:00 , 16:00, 20:00, then motor should start at these intervals only. I found the millis() example but I dont think I can use it in my code. Help me to run the motor. Any help would be appreciated :slight_smile:

You want an RTC (like DS3231 Module I2C Interface) and say an LCD screen with a few buttons.

This could be done without an RTC.

You can get a time library (easy enough to write your own to be honest), but I am unsure of the arduinos crystal's accuracy. It could drift even by a few minutes a day.

You want an RTC (like DS3231 Module I2C Interface) and say an LCD screen with a few buttons.

This could be done without an RTC.


Actually I am sending time intervals through an android app. So I need to get RTC library? Is there anything like timer , so that after certain time has passed motor can start.

You need an RTC (a module) to keep an accurate time...otherwise as a few days go by, your motor will be starting at the wrong time.

The RTC has a very accurate clock...it only loses say 1 second every 100,000 (0.0001 %) . An arduino can lose 500seconds every 100,000 (~0.5%)

In 100 days, your arduino will think that 6pm is actually 6am...see the problem?

The RTC can be polled by the arduino to get an accurate time.

The 0.5% with the arduino with no RTC doesn't sound a lot ("What is half a percent!?")...but there are 1440 minutes a day...0.5% of a day is 7.2 minutes.

So each day, your arduino's idea of the time could drift 7.2 minutes.
That is nearly an hour a week.

After your comment

Johnny010:
You need an RTC (a module) to keep an accurate time...otherwise as a few days go by, your motor will be starting at the wrong time.

I read about the RTC module after your comment. Is there any other way? This is my college mini project and I am trying to build it with minimum cost. So do you have any idea about any such library which can do the work I need? I was planning to calculate time difference between each time interval and then use some library that can start my motor after each of these calculated time differences .

If the motor starts at 13:00 and 16:00, what is it doing at 15:55 for example?

VaniDarji:
Actually I am sending time intervals through an android app. So I need to get RTC library? Is there anything like timer , so that after certain time has passed motor can start.

The android device is your clock then?

If so then does it only send regular time data (like on the minute or hour) or does it handle scheduling?

Oh heck, what/how-much does/can the app do for you?

There may get to be point where Arduino millis timing will suit some part of this.

VaniDarji:
I read about the RTC module after your comment. Is there any other way? This is my college mini project and I am trying to build it with minimum cost. So do you have any idea about any such library which can do the work I need? I was planning to calculate time difference between each time interval and then use some library that can start my motor after each of these calculated time differences .

Try this yourself, The Time library adds timekeeping functionality to Arduino with or without external timekeeping hardware.

https://playground.arduino.cc/Code/Time
https://www.pjrc.com/teensy/td_libs_TimeAlarms.html

/*
 * TimeAlarmExample.pde
 *
 * This example calls alarm functions at 8:30 am and at 5:45 pm (17:45)
 * and simulates turning lights on at night and off in the morning
 * A weekly timer is set for Saturdays at 8:30:30
 *
 * A timer is called every 15 seconds
 * Another timer is called once only after 10 seconds
 *
 * At startup the time is set to Jan 1 2011  8:29 am
 */
 
#include <Time.h>
#include <TimeAlarms.h>

void setup()
{
  Serial.begin(9600);
  setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011
  // create the alarms 
  Alarm.alarmRepeat(8,30,0, MorningAlarm);  // 8:30am every day
  Alarm.alarmRepeat(17,45,0,EveningAlarm);  // 5:45pm every day 
  Alarm.alarmRepeat(dowSaturday,8,30,30,WeeklyAlarm);  // 8:30:30 every Saturday 

 
  Alarm.timerRepeat(15, Repeats);            // timer for every 15 seconds    
  Alarm.timerOnce(10, OnceOnly);             // called once after 10 seconds 
}

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

// functions to be called when an alarm triggers:
void MorningAlarm(){
  Serial.println("Alarm: - turn lights off");    
}

void EveningAlarm(){
  Serial.println("Alarm: - turn lights on");           
}

void WeeklyAlarm(){
  Serial.println("Alarm: - its Monday Morning");      
}

void ExplicitAlarm(){
  Serial.println("Alarm: - this triggers only at the given date and time");       
}

void Repeats(){
  Serial.println("15 second timer");         
}

void OnceOnly(){
  Serial.println("This timer only triggers once");  
}

void digitalClockDisplay()
{
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println(); 
}

void printDigits(int digits)
{
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

Android app implies WiFi and if you have WiFi you can use NTC so no need for a RTC.
(acronym overload... sorry about that).

aarg:
If the motor starts at 13:00 and 16:00, what is it doing at 15:55 for example?


I only want functionality of running my motor at the respective time, say for example 13:00 and 16:00, so at 15:55 it wont be doing anything, the loop will be running and wait for another 5 minutes and then start the motor.

GoForSmoke:
The android device is your clock then?

If so then does it only send regular time data (like on the minute or hour) or does it handle scheduling?

Oh heck, what/how-much does/can the app do for you?

There may get to be point where Arduino millis timing will suit some part of this.

My android app just sends the timings through server to arduino. So after getting these time intervals, I want my motor to start at specific time. I tried using millis(), used the example BlinkWithoutDelay but it didnt work, If you have any other way, then may b that could work :slight_smile:

billhowl:
Try this yourself, The Time library adds timekeeping functionality to Arduino with or without external timekeeping hardware.

Arduino Playground - HomePage
TimeAlarms Library, Run Functions At Specific Times

Can I write Alarm.repeat() inside loop? Because in my program I am getting data from server and so I cannot have predefined time.

VaniDarji:
Can I write Alarm.repeat() inside loop? Because in my program I am getting data from server and so I cannot have predefined time.

Why not? let try it and see what happen.

Arduino Uno and some others use resonators as clock sources rather than more expensive crystal-and-caps.
So the Uno is usually off by minutes per 24 hours, you can set two blinking and see difference in 1 or 2 hours.
The boards with crystal sources are much more accurate but even cheap RTC modules are better.

There are threads on the forum with long term scheduling of actions have been worked out, all used RTC by the end.
Search ebay arduino rtc to get an idea how cheap you can get an RTC for, compare to the cost of a coin cell battery.

Smart move would be to find and read through at least two of those threads just to get an idea of what it takes.
If you decide that one fits you or is close then you can save a lot of work, headaches and hair.

Your choice. We're still here. I just don't want to type post after post reinventing the wheel.

wvmarle:
Android app implies WiFi and if you have WiFi you can use NTC so no need for a RTC.
(acronym overload... sorry about that).

How can I use NTC here? It would be helpful if you could share some examples here. Thank you :slight_smile:

Ah, just notice a typo. That's NTP not NTC.
Plenty of examples: arduino ntp library - Google Search

wvmarle:
Ah, just notice a typo. That's NTP not NTC.
Plenty of examples: arduino ntp library - Google Search

I guess this might work. Thanks a lot :slight_smile:

I would like to make a dog feeder that is activated on a millisecond scale at 4 separate times during the day.
I have all kinds of parts (servos, stepper motors, various Arduinos, etc). I am not worried about the mechanical setup, just the circuit and code to turn an auger on and off at these times for--probably--less than 2 seconds each time. Direct me to the correct forum if I am in the wrong place. Thanks.

lirva1:
Direct me to the correct forum if I am in the wrong place. Thanks.

This is the correct forum but the wrong place.
If you have a specific question related to your own project, open a new thread. For more info on how to ask a good question and get good replies, read the "how to use this forum" sticky.

lirva1:
I would like to make a dog feeder that is activated on a millisecond scale at 4 separate times during the day.
I have all kinds of parts (servos, stepper motors, various Arduinos, etc). I am not worried about the mechanical setup, just the circuit and code to turn an auger on and off at these times for--probably--less than 2 seconds each time. Direct me to the correct forum if I am in the wrong place. Thanks.

You do want your project to have its own thread. It's easier to find and relate to.

If you get (or make) an Arduino that uses a crystal and don't mind adjusting the time once a week then you can avoid buying and dealing with an RTC. I doubt that your dog(s) will notice if chow comes a few seconds late. With an Uno it could be off minutes per day, you'd have to adjust twice a week or more maybe. And adjust could be a button you press at 7AM or some other set time and bam it's done.