Using digitalWrite to pin on a given time

This is my first post, please forgive me for being a newby.

My comfortable environment is Xcode, Mac Os X, and Objective C.

I need to have a bell being rang at some given times of the day to alert my employees way in, lunch time, leave time, etc…

I have done the hardware set up, protected arduino, etc…

I can have Arduino to ring the bell, when I load the code to it, manually.

I installed embedXcode, and I am able to load the compiled lines of code to Arduino UNO, and I can get the bell to ring.

This is my code at this moment:

void loop()
    Serial.print("First long bell ring:\r\n");
    digitalWrite(LED_BUILTIN, HIGH);
    delay (2000);
    digitalWrite(LED_BUILTIN, LOW);
    delay (720000);
    Serial.print("Ok, I waited XXX seconds...\r\n");

void bellLong(){
    Serial.print("About to make 3 long bell rings:--- \r\n");
    for (int x = 0; x < 3; x++) {
        digitalWrite(LED_BUILTIN, HIGH);
        digitalWrite(12, HIGH);
        digitalWrite(LED_BUILTIN, LOW);
        digitalWrite(12, LOW);
        Serial.print("Done with:\r\n");
    Serial.print("Done with 3 rings - loop ends... ");

I have noticed that Arduino will not “delay” if the value is too high, or if I use an usual programming expression such as:

delay (1000*60*60);

(1 hour), This does not work for me.

  1. So my main doubt is whether I should use Arduino’s clock capabilities or my Mac’s clock to trigger the events to “ring the bell”.

  2. I can use Cocoa’s NSTimer to trigger my events, but I have no idea if I can use objective C and embedXcode to interact with Arduino.

The time schedule for ringing should be:


  1. If I could compile a plain executable, I could write a helper app that could be launched by Apple’s launchd (something like Unix’s crond), and run the executable on the correct time.

Can a helping soul please guide on how to get Arduino to digitalWrite on given times of the day?


Bernardo Höhl
Rio de Janeiro - Brazil

Long period delays are not accurate. For the mentioned application, using a dedicated time module is an option, if you want to have another system that keeps time to ping the Arduino, then really the Arduino is unnecessary. If you still want to use the Arduino anyway, then serial communication is pretty universal.

Use e.g

delay (1000UL * 60 * 60);

In C/C++ you need to help the compiler a little; specifying one of the numbers as unsigned long will tell the compiler not to use integers for the calculation but unsignrd longs. Integers result in truncation of the result.

You can not expect the Arduino to keep good time.

You should NOT be using delay() at all. The blink without delay example shows how not to.

You need a state machine, to keep track of the states - before work, ringing the get to work bell, between start and lunch time, ringing the lunch bell, etc.

You really should have an RTC. Then, the issues you are having would be a lot simpler to deal with. Is it start time? if so, call the ringTheBell() function with a time. Is it lunch time? If so, call the ringThBell() function with a time.

You can then implement the ringTheBell() function any way you like, using delay() so you call it just once, or using the blink without delay technique, calling the function over and over.