Arduino Timer/Clock

Would anybody know how to make an arduino turn into a a Timer/Clock?

The basic idea is the user can select a time during the day, for example, 6am-7pm, with an LCD Keypad and data can be transferred between 'Pin A' and output to 'Pin B'?

Any ideas?

Willing to pay up to $10 :slight_smile:

Thank you!

Have you looked at the Time.h library in the playground?

http://playground.arduino.cc/Code/Time

What does PinA and PinB have to do with Time/Clock?

CrossRoads:
Have you looked at the Time.h library in the playground?

Arduino Playground - Time

What does PinA and PinB have to do with Time/Clock?

No I haven't, but I have really no idea what I'm doing with an Arduino.

Pin A and Pin B aren't actual pins, they're examples. So the user will set the time during the day at which Pin A is able to detect an input and output to Pin B.

Thanks for the help!

I'll move this to Gigs & Collaborations for you.

Thank you :slight_smile:

Anybody got any ideas? Willing to pay :slight_smile:

Type "Arduino Programmable Timer" into Google. :slight_smile:

I have these codes that make an Arduino Alarm clock that you can modify for your use.

// simple sketch to display a digital Alarm clock on an LCD keypad shield without RTC
// see the LiquidCrystal documentation for more info on this
// 

/*
The circuit:
* LCD RS (Data or Signal Display Selection) pin to digital pin 8
* LCD Enable pin to digital pin 9
* LCD D4 pin to digital pin 4
* LCD D5 pin to digital pin 5
* LCD D6 pin to digital pin 6
* LCD D7 pin to digital pin 7
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
* Backlit Control to Digital 10
* Button (select, up, right, down and left) to Analog 0
*/


#include <LiquidCrystal.h>
// include header file for time function
#include <Time.h>
// Button defination for LCD Keypad shield
#define btnRIGHT  0		// Okay
#define btnUP     1		// inc
#define btnDOWN   2		// dec
#define btnLEFT   3		// Select
#define btnSELECT 4		// Menu
#define btnNONE   5
// Observed values:
//     NONE:    1023
//     SELECT:  723
//     LEFT:    481
//     DOWN:    307
//     UP:      133
//     RIGHT:   0

#define beeper A1      // Alarm buzzer
#define shortBeep 100
#define longBeep  500

#define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER  'T'   // Header tag for serial time sync message
#define TIME_REQUEST  7    // ASCII bell character requests a time sync message 


// define variables
int lcd_key = 0;
int adc_key_in = 0;
int lastDay = 0;
int lastMonth = 0;
int lastYear = 0;
int lastHour = 0;
int lastMinute = 0;
int movementTimer = 0;
int menuOptions = 4;
int menuOption = 0;
int alarmHours = 0;
int alarmMinutes = 0;
bool alarmSet = 0;
bool backLightOn = 1;
int fadeValue = 255;


// define constants
const int backLight = 10; // pin 10 will control the LCD backlight

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void setup()
{
	pinMode(backLight, OUTPUT);
	pinMode(beeper, OUTPUT);
	digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
	Serial.begin(9600);
	setSyncProvider( requestSync);  //set function to call when sync required
}

void loop(){ 
  if(Serial.available() ) 
  {
    processSyncMessage();
  }
  else
  if(timeStatus()== timeNotSet)  
	{
//    setTime(1356210000);
		setTime(8, 0, 0, 20, 1, 2013); // hour, min, sec, day, month, year 
											// Replace this with the most current time
	}
    
	if(timeStatus()!= timeNotSet)   
	{
		digitalWrite(13,timeStatus() == timeSet); // on if synced, off if needs refresh  
		digitalClockDisplay();  
	}

	for (int i = 0; i < 9000; i++)
		{
			button_loop(); //check for button pushed
		}
}

void printDigits(byte digits){
	// utility function for digital clock display: prints preceding colon and leading 0
	lcd.print(":");
	if(digits < 10)
	lcd.print('0');
	lcd.print(digits,DEC);
}


void digitalClockDisplay(){
	//lcd.clear();
	lcd.begin(16,2);
	lcd.setCursor(3,0);
        lcd.print("          ");
	lcd.setCursor(3,1);
        lcd.print("          ");
	lcd.setCursor(3,0);
	if(day() <10)
	lcd.print('0');
	lcd.print(day(),DEC);
	lcd.print("/");

	if(month() <10)
	lcd.print('0');
	lcd.print(month(),DEC);
	lcd.print("/");
	lcd.print((year()));

	//lcd.print(" ");
	if(hour() <10)
	lcd.setCursor(5,1);
	lcd.setCursor(4,1);

	// digital clock display of current time
	lcd.print(hour(),DEC);
	printDigits(minute());
	printDigits(second());
	// for time set
    lastDay = day();
    lastMonth = month();
    lastYear = year();
    lastHour = hour();
    lastMinute = minute();
        
          //check for alarm
  if (alarmSet)
  {
    // alarm set 
    if (alarmHours == lastHour && alarmMinutes == lastMinute)
    {
      //sound alarm
      setOffAlarm();
    } 
  }
}

void processSyncMessage() {
  // if time sync available from serial port, update time and return true
  while(Serial.available() >=  TIME_MSG_LEN ){  // time message consists of a header and ten ascii digits
    char c = Serial.read() ; 
    Serial.print(c);  
    if( c == TIME_HEADER ) {       
      time_t pctime = 0;
      for(int i=0; i < TIME_MSG_LEN -1; i++){   
        c = Serial.read();          
        if( c >= '0' && c <= '9'){   
          pctime = (10 * pctime) + (c - '0') ; // convert digits to a number    
        }
      }   
      setTime(pctime);   // Sync Arduino clock to the time received on the serial port
    }  
  }
}

time_t requestSync()
{
  Serial.write(TIME_REQUEST);  
  return 0; // the time will be sent later in response to serial mesg
}


void button_loop()
{
  int button = read_LCD_buttons();
  if (button == btnSELECT)
  {
    timedBeep(shortBeep,1); 
    selectMenu();
  }
   if (button == btnDOWN)
  {
    fadeValue = fadeValue -5;
    if (fadeValue < 5) { fadeValue = 0; }
		analogWrite (backLight, fadeValue); 
    delay (100);		
  }
  if (button == btnUP)
  {
	fadeValue = fadeValue +5;
	if (fadeValue > 254) { fadeValue = 255; }
    	analogWrite (backLight, fadeValue); 
	delay (100);
  }  
}

void selectMenu()
{
  int button = 0; 
  menuOption = 1;
  lcdClear();
  lcd.print("Minute Timer");  

  while (menuOption <= menuOptions)
  {
    button = read_LCD_buttons();
    if (button == btnSELECT)
    {
      timedBeep(shortBeep,1);   
      menuOption++;

      if (menuOption == 2)
      {
        lcdClear();
        // clearAlarm feature
        lcd.print("Set/Clear Alarm");            
      }
      if (menuOption == 3)
      {
        lcdClear();
        lcd.print("Set Date/Time");            
      }
      if (menuOption == 4)
      {
        lcdClear();
        lcd.print("Stop Watch Timer");            
      }
    } 

    if (button == btnLEFT)
    {
      if (menuOption == 1)
      {
        timedBeep(shortBeep,1);
        minuteTimer();
        return;
      }
      if (menuOption == 2)
      {
        timedBeep(shortBeep,1);
        // clearAlarm feature
        //check for existing alarm
        if (alarmSet)
        {
          clearAlarm();
        }
        else
        {
          setAlarm();
        }
        return;
      }
      if (menuOption == 3)
      {
        timedBeep(shortBeep,1);
        // setDateTime feature
        setDateTime();
        return;
      } 
      if (menuOption == 4)
      {
	timedBeep(shortBeep,1);
	stopwatch_time_counter();
	return;
      }
    }
  }
}  

// clearAlarm feature
void clearAlarm()
{
  int button = 0;
  bool clearIt = true;

  lcdClear();
  lcd.print("Alarm Set For");
  lcd.setCursor(0,1);
  lcd.print(alarmHours);   
  lcd.print(":");
  lcd.print(alarmMinutes);
  delay(2000);
  lcdClear();
  lcd.print("Clear Alarm?");
  lcd.setCursor(0,1);
  lcd.print("Yes");  

  while (button != btnSELECT)
  {
    button = read_LCD_buttons();
    if (button == btnUP)
    {
      timedBeep(shortBeep,1);
      clearIt = !clearIt;
    }
    if (button == btnDOWN)
    {
      timedBeep(shortBeep,1);
      clearIt = !clearIt;
    }
    if (button == btnRIGHT)
    {
      timedBeep(shortBeep,1);
      alarmSet = !clearIt;
      if (clearIt)
      {
        lcdClear();
        timedBeep(shortBeep,2);
        lcd.print("Alarm Cleared!");
        delay(2000);
      }
      return; 
    }
    lcd.setCursor(0,1);
    if (clearIt)
    {
      lcd.print("Yes"); 
    }
    else{
      lcd.print("No ");
    }
  }   
}

void minuteTimer()
{
  // Pass maxCount to getTimerMinutes
  int timerMinutes = getTimerMinutes("Set Minutes", 0, 60);
  if (timerMinutes > 0)
  {
    timedCountDown(timerMinutes*60, "Minute Timer");
  }
  else
  {
    timerCancelled("Timer");       
  }
  return;
}

Continue due to size limit...

void stopwatch_time_counter()
{
	static unsigned long elapsed_time = 0;
	static unsigned long last_read;
	static unsigned char is_ticking = 0;
	static unsigned char top_is_pressed = 0;
	static unsigned char bottom_is_pressed = 0;
	static unsigned char last_seconds = 0;
	int button = 0;
	
	  while (button != btnSELECT)
  {

	unsigned long current_time = millis();
	if(is_ticking)
	{
		elapsed_time += current_time - last_read;
	}
	last_read = current_time;

	button = read_LCD_buttons();
	if(button == btnUP)
	{
		if(!top_is_pressed)
		{
			// reset
			top_is_pressed = 1;
			is_ticking = 0;
			elapsed_time = 0;
			timedBeep(shortBeep,1);

			//wait_for_button_release(TOP_BUTTON);clear();// tmphax to make this work on org06a01
			lcdClear();
			lcd.print("Stop Watch Timer"); 
		}
	}
	else
	{
		top_is_pressed = 0;
	}

	if(button == btnDOWN)
	{
		if(!bottom_is_pressed)
		{
			// start/stop
			bottom_is_pressed = 1;
			is_ticking = !is_ticking;
			timedBeep(shortBeep,1);
		}
	}
	else
	{
		bottom_is_pressed = 0;
	}

	lcd.setCursor(0,1);
	lcd.print(elapsed_time/1000/60/60/10%10); // tens of hours
	lcd.print(elapsed_time/1000/60/60%10);    // hours
	lcd.print(":");
	lcd.print(elapsed_time/1000/60/10%10%6);    // tens of minutes
	lcd.print(elapsed_time/1000/60%10);       // minutes
	lcd.print(":");
	lcd.print(elapsed_time/1000%60/10);       // tens of seconds
	unsigned char seconds = elapsed_time/1000%60%10;
	lcd.print(seconds);
	lcd.print(".");
	lcd.print(elapsed_time/100%10);           // tenths of seconds
	lcd.print(elapsed_time/10%10);            // hundredths of seconds

	// beep every second
	if(seconds != last_seconds && elapsed_time != 0)
	{
		//timedBeep(shortBeep,1);
	}
	last_seconds = seconds;
	}
	
  
    timerCancelled("StopWatch");       
  

}

void setAlarm()
{
  int button = 0;
  // Pass maxCount to getTimerMinutes
  alarmHours = getTimerMinutes("Set Alarm Hour", alarmHours, 23);  
  // Validate alarm hours > 0 and < 24
  if (alarmHours >= 0 && alarmHours < 24)
  {
    // Pass maxCount to getTimerMinutes
    alarmMinutes = getTimerMinutes("Set Minutes", alarmMinutes, 59);
    // allow alarm minutes to be 0
    if (alarmMinutes < 60)
    {
      lcdClear();
      lcd.setCursor(0,1);
      //display alarm time
      lcd.print(alarmHours);       
      lcd.print(":");
      if (alarmMinutes < 10)
        lcd.print("0");
      lcd.print(alarmMinutes);
      if (button == btnRIGHT)
      {
        timedBeep(shortBeep,1);
        alarmSet = true;   
        lcd.setCursor(0,0);
        lcd.print("Alarm Set for");
        delay(1000);
        return;       
      }
      else
      {
        timerCancelled("Alarm");
        return;  
      }   
    }
    else
    {
      timerCancelled("Alarm");     
    }    
  }     
  else
  {
    timerCancelled("Alarm");       
  }
}

// setDateTime feature
void setDateTime()
{
  int button = 0;

  //get month
  int setMonth = getTimerMinutes("Set Month", lastMonth, 12);
  if (setMonth > 0 && setMonth < 13)
  {
    //get day
    // default day and hour settings on set date/time
    int setDay = getTimerMinutes("Set Day", lastDay, 31);
    if (setDay > 0 && setDay < 32)
    {
      //get year
      int setYear = getTimerMinutes("Set Year", lastYear, 2999);
      if (setYear > 2000 && setYear < 3000)
      {
        //get hour
        int thisHour = lastHour;
        // default day and hour settings on set date/time
        int setHour = getTimerMinutes("Set Hour", thisHour, 23);
        if (setHour >= 0 && setHour < 24)
        {
          //get minutes
          int setMinute = getTimerMinutes("Set Minute", lastMinute, 59);
          if (setMinute < 60)
          {
             // RTC.adjust(DateTime(setYear,setMonth,setDay,setHour,setMinute)); // for DS1307
                setTime(setHour, setMinute, 0, setDay, setMonth, setYear); // sec, min, hour, date, month, year

              lcd.setCursor(0,0);
              lcd.print("Saving...     ");
              delay(1000);
              return;       
            }
            else
            {
              timerCancelled("");
              return;  
            }  
          }
          else
          {
            timerCancelled("");     
          }    
        }     
        else
        {
          timerCancelled("");       
        }
      }
      else
      {
        timerCancelled("");     
      }    
    }     
    else
    {
      timerCancelled("");       
    }

}

// read the buttons from LCD keypad shield
int read_LCD_buttons()
{
  adc_key_in = analogRead(0);      // read the value from the sensor
  // my buttons when read are centered at these valies: 0, 131, 307, 481, 722
  // we add approx 50 to those values and check to see if we are close
  // No button pressed should be 1023
  if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
  if (adc_key_in < 50)   return btnRIGHT; 
  if (adc_key_in < 195)  return btnUP;
  if (adc_key_in < 380)  return btnDOWN;
  if (adc_key_in < 555)  return btnLEFT;
  if (adc_key_in < 790)  return btnSELECT;  
  return btnNONE;  // when all others fail, return this...

}

void timedCountDown(int secondCount, char countLabel[])
{
  long seconds = 0;
  long minutes = 0; 

  lcdClear();
  lcd.print(countLabel);
  for (int i = secondCount; i >= 0; i--)
  {
    seconds = i;
    minutes = i / 60;
    if (minutes > 0)
    {
      seconds = seconds - (minutes * 60);  
    }     

    if (minutes > 0)
    {
      lcd.setCursor(0,1);
      lcd.print(minutes);
      lcd.print(" min ");
    }
    else
    {
      lcd.setCursor(0,1);
    }
    if (seconds < 10) lcd.print("0");
    lcd.print(seconds);
    lcd.print(" sec remaining");
    if (seconds > 0) delay(1000); 
    if (read_LCD_buttons() == btnSELECT) //cancel
    {
      timerCancelled("Timer");
      i = 0;
      return;
    }
  }
  lcd.setCursor(6,1);
  timedBeep(longBeep,3);
}

// Pass maxCount to getTimerMinutes
int getTimerMinutes(char timerText[], int startNum, int maxCount)
{
  int minutes = startNum;
  int button = 0;
  lcdClear();
  lcd.print(timerText);
  lcd.setCursor(0,1);
  lcd.print(minutes);   

  while (button != btnSELECT)
  {
    button = read_LCD_buttons();
    Serial.println(button);

    if (button == btnLEFT)
    {
      if ((minutes + 10) <= maxCount)
      {
        timedBeep(shortBeep,1);
        minutes = minutes + 10;
      }
      else
      {
        timedBeep(shortBeep,2); 
      }
    }

    if (button == btnUP)
    {
      if (minutes < maxCount)
      {
        timedBeep(shortBeep,1);
        minutes++;
      }
      else
      {
        timedBeep(shortBeep,2);
		minutes = 0;
      }
    }
    if (button == btnDOWN)
    {
      if (minutes > 0)
      {
        timedBeep(shortBeep,1);
        minutes--;
      }
      else
      {
        timedBeep(shortBeep,2); 
		minutes = maxCount;
      }   
    } 
    if (button == btnRIGHT)
    {
      timedBeep(shortBeep,1);
      return minutes; 
    }
    lcd.setCursor(0,1);
    lcd.print(minutes); 
    lcd.print("   ");
  }
  return 0;
}

void timedBeep(int beepTime, int beepCount)
{
  for (int i = 0; i < beepCount; i ++)
  {
    digitalWrite(beeper, HIGH);
    delay(beepTime);
    digitalWrite(beeper, LOW);
    delay(beepTime);
  }
}

void lcdClear(){
  lcd.clear();
  lcd.begin(16,2);
  lcd.setCursor(0,0); 
}

void timerCancelled(char message[])
{
  lcdClear();
  lcd.print(message);
  lcd.print(" Cancelled");
  timedBeep(shortBeep,3);    
}

void setOffAlarm()
{
  int button = 0;
  int i = 0;
  Serial.println(i);
  digitalWrite(backLight, HIGH); // turn backlight on
  while (button != btnSELECT)
  {
    button = read_LCD_buttons();
    lcdClear();
    i++;
    if (i > 50)
    {
      lcdClear();
      lcd.print("Alert Alert");
      lcd.setCursor(0,1);
      lcd.print("     Alert Alert");      
      i = 0;
      timedBeep(shortBeep,3);
    }

  }     
  timerCancelled("Alarm"); 
  alarmSet = false;  
}

I Tried The code on arduino uno with keypad lcd but it stops compiling:
Any guess to get it running?

It says:
time_t does not name a type

Button:57: error: 'time_t' does not name a type
Button.ino: In function 'void setup()':
Button:79: error: 'requestSync' was not declared in this scope
Button:79: error: 'setSyncProvider' was not declared in this scope
Button.ino: In function 'void loop()':
Button:88: error: 'timeStatus' was not declared in this scope
Button:88: error: 'timeNotSet' was not declared in this scope
Button:91: error: 'setTime' was not declared in this scope
Button:95: error: 'timeStatus' was not declared in this scope
Button:95: error: 'timeNotSet' was not declared in this scope
Button:97: error: 'timeSet' was not declared in this scope
Button.ino: In function 'void digitalClockDisplay()':
Button:124: error: 'day' was not declared in this scope
Button:126: error: 'day' was not declared in this scope
Button:129: error: 'month' was not declared in this scope
Button:131: error: 'month' was not declared in this scope
Button:133: error: 'year' was not declared in this scope
Button:136: error: 'hour' was not declared in this scope
Button:141: error: 'hour' was not declared in this scope
Button:142: error: 'minute' was not declared in this scope
Button:143: error: 'second' was not declared in this scope
Button.ino: In function 'void processSyncMessage()':
Button:169: error: 'time_t' was not declared in this scope
Button:169: error: expected `;' before 'pctime'
Button:173: error: 'pctime' was not declared in this scope
Button:176: error: 'pctime' was not declared in this scope
Button:176: error: 'setTime' was not declared in this scope
Button.ino: At global scope:
Button:181: error: 'time_t' does not name a type
Button.ino: In function 'void setDateTime()':
Button:515: error: 'setTime' was not declared in this scope

#include <LiquidCrystal.h>

#include <Time.h>

These are libraries you must have 'installed' on your computer. Usually in the "libraries" folder under the Arduino directory.
You should be able to find them here -
http://playground.arduino.cc//Main/LibraryList

Everything you need to know about Arduino libraries....

If you are interested in building your very own clock, you should also take a look at the
"Doomsday Clock Shield Kit"
http://samuraicircuits.com/merch/index.php?id_category=14&controller=category

It is designed to work with the "Extremely Accurate Real Time Clock". This way you can build a clock that can keep very accurate time. According to the datasheet it will lose less than one minute per year.

BillHo:
#include <LiquidCrystal.h>
// include header file for time function
#include <Time.h>
.....

I tried, but I got the following message:

Time.ino: In function 'void digitalClockDisplay()':
Time:157: error: 'setOffAlarm' was not declared in this scope
Time.ino: In function 'void button_loop()':
Time:189: error: 'read_LCD_buttons' was not declared in this scope
Time:192: error: 'timedBeep' was not declared in this scope
Time.ino: In function 'void selectMenu()':
Time:215: error: 'lcdClear' was not declared in this scope
Time:220: error: 'read_LCD_buttons' was not declared in this scope
Time:223: error: 'timedBeep' was not declared in this scope
Time:248: error: 'timedBeep' was not declared in this scope
Time:254: error: 'timedBeep' was not declared in this scope
Time:263: error: 'setAlarm' was not declared in this scope
Time:269: error: 'timedBeep' was not declared in this scope
Time:271: error: 'setDateTime' was not declared in this scope
Time:276: error: 'timedBeep' was not declared in this scope
Time:277: error: 'stopwatch_time_counter' was not declared in this scope
Time.ino: In function 'void clearAlarm()':
Time:290: error: 'lcdClear' was not declared in this scope
Time:304: error: 'read_LCD_buttons' was not declared in this scope
Time:307: error: 'timedBeep' was not declared in this scope
Time:312: error: 'timedBeep' was not declared in this scope
Time:317: error: 'timedBeep' was not declared in this scope
Time.ino: In function 'void minuteTimer()':
Time:342: error: 'getTimerMinutes' was not declared in this scope
Time:345: error: 'timedCountDown' was not declared in this scope
Time:349: error: 'timerCancelled' was not declared in this scope

What is wrong?

The code had two part, you need to copy both parts into one file.

hi which is the output for the buzzer :slight_smile:

slayter89:
hi which is the output for the buzzer :slight_smile:

Buzzer output on Analog input pin A1

i try using a speaker it nv have any beep sound just lthe keypad showing alert alert alert

slayter89:
i try using a speaker it nv have any beep sound just lthe keypad showing alert alert alert

Is this type of Buzzer not speaker, Buzzer only need to supply voltage it will sound.

hmm but is there a way to use thus

slayter89:
hmm but is there a way to use thus

Your image had error & can not display.