Grove Tick Tock Shield Kit

Giving the Tick Tock Shield Kit by Grove a try, I made same changes to the real time clock code. First I wanted to automaticaly change the brightness not only at the start of the code. And second I wanted the date to displayed to. Now it displays alternately with the temperature. (To set the date, do this once when hour and minutes are set, see comment there. This should be enough, no need to alter with the buttons). Had to change the library too. So find all the code here. RealTimeClock2:

/****************************************************************************/
//  Demo function:
//
//  Author:Frankie.Chu
//  Date:23 September, 2012
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//
//  Modified record:
//  Johannes, added function for auto brighness and showing date 11/2013
//
/*******************************************************************************/

#include "Wire.h"
#include <TimerOne.h>
#include <MsTimer2.h>
#include <EEPROM.h>
#include "TM1636.h"
#include "TickTockShield.h"

#define ON 1
#define OFF 0

//definitions of global variables
boolean flag_update;//1 = update the time on the 4-digit display
unsigned char halfsecond;//each time the timer2 interrupts,halfsecond plus one.
boolean flag_clockpoint = 1;//change between 0 and 1 every 500ms. If it is 1,
//the clockpoint is on.Else the clockpoint is off.
boolean flag_2s;//2 seconds flag.1 = to the end of 2 seconds so as to set 
//flag_display_time
boolean flag_10s;//10 seconds flag.1 = to the end of 10 seconds so as to 
//display temperature
boolean flag_display_time = 1;//1 = can display time that get from the RTC
uint8_t counter_times = 20;//20*halfsecond = 10seconds.
//If it is 20,to count for 10s,so display time for 10s.
//If it is 4,to count for 2s,so display the temperture for 2s
boolean flag_500ms_blink;//when adjusting the time or the alarm,the hour or minute
//displayed on the 4-digit display will blink every 500ms.
boolean flag_scan_again;//1 = can scan key again at the state of SYSTEM_NORAML
//To avoid the mistake of entering the state of SYSTEM_ADJUSTING 
//again when pressing MENU for 3s to confirm the setting.
uint8_t counter_500ms;//when counter_500 reach 6,flag_scan_again is set 1.

boolean flag_temp_shown; // added by Johannes

/*status definitions for the tick shield control system*/
uint8_t system_states;
#define SYSTEM_NORAML    0 //default status,and the system will turn to "normal" status 
#define SYSTEM_ADJUSTING 1 //triggered when key MENU is pressed at the "normal" status.
#define SYSTEM_ALARMING  2

//--Declare a TickTockShield Class object--//
TickTockShield ticktockshield;

extern int8_t disp[4];//external global array defined in TickTockShield.cpp

extern byte g_hand;//external global variable.
//the clock hand, HOUR shows that it is adjusting the hour, 
//MINUTE shows that it is adjusting the minute.

void setup() 
{
#ifdef DEBUG
  Serial.begin(9600);
#endif
  ticktockshield.init();

  /*Run the 4 LEDs from left to right*/
  ticktockshield.runLED();
  if(isFirstLoad())//if it is the first time to load the firmware?
  {
    ticktockshield.setAlarm(12,0);//Yes,the alarm clock is initialized to 12:00
    //and the data in the EEPROM.
  }
  else ticktockshield.getAlarm();//No,read the alarm clock stored in EEPROM
  /*When system starts, adjust the brightness of the digital tube 
   			according to the ambient light intensity.*/
  uint8_t lightlevel; 
  lightlevel = ticktockshield.getLightLevel();
  ticktockshield.adjustBrightness(lightlevel);
  /*Read the ambient temperature and display on the digital tube.*/
  ticktockshield.displayTemperature(ticktockshield.getTemperature());
  delay(1000);
  MsTimer2::set(500, Timer2ISR);//Timer2 generates an interrupt every 500ms
  MsTimer2::start();//Timer2 starts counting
}
void loop() 
{
  clockStart();
}
/****************************************************************/
/*Function:Implement the function of a clock with an alarm clock, and */
/*			the buttons can adjust the clock.*/
void clockStart()
{
  if(system_states == SYSTEM_NORAML)//if it is normal states?
  {
    /*Yes, the clock will update every 500ms,and check whether the Menu is pressed */
    /*to enter SYSTEM_ADJUSTING state and check if it should be enter SYSTEM_ALARMING state.*/
    if(flag_update)//if update flag is not zero?
    { 
      flag_update = 0;
      if(flag_clockpoint)
      {
        tm1636.point(POINT_ON);
      }
      else tm1636.point(POINT_OFF); 
      ticktockshield.getTime();
      if(ticktockshield.isAlarmEnable())
      {
        tm1636.point(POINT_ON);
        ticktockshield.displayTime();
        system_states = SYSTEM_ALARMING;
        return;
      }
      if(flag_display_time)ticktockshield.displayTime();

      if(flag_2s)
      {
        flag_2s = 0;
        flag_display_time = 1;

        counter_times = 20;
        halfsecond = 0;
      }
      if(flag_10s)
      {
        flag_10s = 0;
        flag_display_time = 0;

        if(flag_temp_shown == 0){
          tm1636.point(POINT_OFF);
          ticktockshield.displayTemperature(ticktockshield.getTemperature());
          flag_temp_shown = 1;
        } 
        else {

          // Johannes: Set the new brightness; bug fixed 7 - lightlevel as parameter
          uint8_t lightlevel; 
          lightlevel = ticktockshield.getLightLevel();
          ticktockshield.adjustBrightness(7 - lightlevel);

          tm1636.point(POINT_ON);
          ticktockshield.displayDate(); 
          flag_temp_shown = 0;

        }

        counter_times = 4;
        halfsecond = 0;

        /*
        int8_t temp[4];
         temp[0] = ticktockshield.g_dayOfMonth/10;
         temp[1] = g_dayOfMonth%10;
         temp[2] = g_month/10;
         temp[3] = g_month%10;
         	tm1636.display(temp);
         */
      }
      if((flag_scan_again)&&(KEY_MENU == ticktockshield.scanKey()))
      {
        ticktockshield.writeToAdjustArea();
        ticktockshield.processKey();
        system_states = SYSTEM_ADJUSTING;
      }

    }
  }
  else if(system_states == SYSTEM_ADJUSTING)
  {
    ticktockshield.scanKey();
    ticktockshield.processKey();
    ticktockshield.processSystemStatus();
    if(ticktockshield.getQuitReq())
    {
      system_states = SYSTEM_NORAML;
      counter_500ms = 0;
      flag_scan_again = 0;
    }
    else flag_scan_again = 1;
  }
  else if(system_states == SYSTEM_ALARMING)
  {
    /*It will sound alarm for a minute untill the "MENU" key is pressed*/
    if(ticktockshield.isAlarmEnable())
    {
      ticktockshield.alarming();
    }
    else 
    {
      ticktockshield.turnOffAlarm();
      system_states = SYSTEM_NORAML;
    }
    if(KEY_MENU == ticktockshield.scanKey())
    {
      ticktockshield.turnOffAlarm();
      system_states = SYSTEM_NORAML;
    }
  }
}

//--------------------------------------
boolean isFirstLoad()
{
  unsigned char mark[] = "ALARM";
  unsigned char temp_data[5];
  for(unsigned char i = 0;i < 5;i ++)
  {
    temp_data[i] = EEPROM.read(i);
    if(temp_data[i] != mark[i])
    {
      EEPROM.write(0,mark[0]);
      EEPROM.write(1,mark[1]);
      EEPROM.write(2,mark[2]);
      EEPROM.write(3,mark[3]);
      EEPROM.write(4,mark[4]);
      return true;
    }
  }
  return false;
}

/*Function:It is timer 2 interrupt service routine.Timer2 generates an interrupt*/
/*  every 500ms.And every time it interrupts,this function will be executed.*/
void Timer2ISR()
{
  halfsecond ++;
  if(halfsecond  == counter_times)
  {
    halfsecond  = 0;
    if(counter_times == 4)flag_2s = 1;
    else if(counter_times == 20)flag_10s = 1;
  }
  flag_update = 1;
  flag_clockpoint = (~flag_clockpoint) & 0x01;//change between 0 and 1 every 500ms.
  flag_500ms_blink = ~flag_500ms_blink;
  if(ticktockshield.isAdjustingTime())
  {
    if(g_hand == HOUR)
    {
      if(flag_500ms_blink)
      {
        disp[0] = INDEX_BLANK;
        disp[1] = INDEX_BLANK;
      }
    }
    else
    {
      if(flag_500ms_blink)
      {
        disp[2] = INDEX_BLANK;
        disp[3] = INDEX_BLANK;
      }
    }
    tm1636.display(disp);
  }
  counter_500ms ++;
  if(counter_500ms == 6)
  {
    counter_500ms = 0;
    flag_scan_again = 1;
  }

}

TickTockShield.h:

#ifndef TICKTOCKSHIELD_H_
#define TICKTOCKSHIELD_H_

#include "TM1636.h"

#define DS1307_I2C_ADDRESS 0x68

//debug configuration
#define DEBUG 0
//-------pin definition of keys---------------//
#define KEY_MENU	11
#define KEY_UP		10
#define KEY_DOWN	9

//-------pin definition of LEDs---------------//
#define LED_CLOCK_ADJUST	5
#define LED_ALARM_ADJUST	4
#define LED_ALARM_ENABLE	3
#define LED_BRIGHT_ADJUST	2

#define ALARM_BUZZER		6

//-------pin definition of sensors-------------//
#define TEMPERATURE_SENSOR	A0
#define LIGHT_SENSOR		A1

#define LED_ON	1
#define LED_OFF 0

#define HOUR	0
#define MINUTE	1
/*Direction of the LEDs to run*/
#define LEFT_TO_RIGHT 0
#define RIGHT_TO_LEFT 1
//-------Status bit definition of the Status flag----//
/*volatile typedef enum{
  SS_NO_INPUT = 0,//no keys input
  SS_CLOCKH_ADJUST,//adjust the hour of the clock
  SS_CLOCKM_ADJUST,	//adjust the minute of the clock
  SS_ALARMH_ADJUST,	//adjust the hour of the alarm
  SS_ALARMM_ADJUST,	//adjust the minute of the alarm
  SS_ALARM_ENABLE,  //to enable or disable the alarm
  SS_BRIGHT_ADJUST,	//adjust the brightness of the 4-digit display
  SS_CONFIRM_EXIT,  //It will confirm the setting and exit after pressing the MENU button for more than 3s
  					//or after pressing it again at the status of adjust brightness.
  SS_QUIT,			//It will exit without saving any setting if no button is pressed within 5s.
}keyStatusType;*/
//extern keyStatusType system_status;

#define SS_NO_INPUT 		0//no keys input
#define SS_CLOCKH_ADJUST	1//adjust the hour of the clock
#define SS_CLOCKM_ADJUST	2//adjust the minute of the clock
#define SS_ALARMH_ADJUST	3//adjust the hour of the alarm
#define SS_ALARMM_ADJUST	4//adjust the minute of the alarm
#define SS_ALARM_ENABLE  	5//to enable or disable the alarm
#define SS_BRIGHT_ADJUST	6//adjust the brightness of the 4-digit display
#define SS_CONFIRM_EXIT  	7//It will confirm the setting and exit after pressing the MENU button for more than 3s
							 //or after pressing it again at the status of adjust brightness.
#define SS_QUIT				8//It will exit without saving any setting if no button is pressed within 5s.

#define KEY_PRESSED 1
#define KEY_RELEASE 2

#define RESISTOR_CONNECT_THERMISTOR	10000
struct AlarmStruct
{
  byte hour;
  byte minute;
  boolean flag_enable;
};
class TickTockShield
{
public:
  void init();
  void setLed(unsigned char led_status, int pinLED);
  void turnOffLED();
  void turnOnLED();
  void runLED(byte = 1, byte = LEFT_TO_RIGHT);
  int16_t scanKey();
  void processKey();
  void processSystemStatus();
  void quit();
  void saveChanges();
  inline void modifyAlarmFlag();
  void adjustClock(uint8_t hand);
  void adjustAlarm(uint8_t hand);
  void adjustBrightness();
  void adjustBrightness(uint8_t grayscale);
  int8_t getTemperature();
  void displayTemperature(int8_t temperature);
  float getLightIntensity();
  uint8_t getLightLevel();
  void ringOn();
  void ringOff();
  void alarming();
  void turnOffAlarm();
  boolean isAlarmEnable();
  boolean compareWithAlarm();
  inline void alarmDisable();
  void setAlarm(struct AlarmStruct alarm_);
  void setAlarm(uint8_t hour,uint8_t minute,uint8_t = 0);
  void getAlarm();

  void writeToAdjustArea();
  void writeToNormalArea();
  void writeTime();
  void getTime();
  void displayTime();
  void displayDate();  //added by Johannes
  void display(int8_t DispData []);
  void clearTime(uint8_t hand);
  byte decToBcd(byte val);
  byte bcdToDec(byte val);
  byte getQuitReq(){return flag_require_quit;}
  boolean isAdjustingTime();
private:
  /*Globle variables in the class*/
  byte g_second, g_minute, g_hour;
  /*when the clock is being adjusted, these two variables save the temporary data.*/
  byte g_minute_temp,g_hour_temp;
  byte g_dayOfWeek;
  byte g_dayOfMonth, g_month, g_year;
  AlarmStruct alarm;
  byte g_brightness;
  byte g_brightness_temp;
  /*when the alarm is being adjusted, this variable save the temporary data.*/
  AlarmStruct alarm_temp;
  int16_t pre_pin_number;//pin number of the key pressed before
  int16_t key_pin_pressed;//pin number of the key pressed now
  uint8_t tempKeyValue;
  uint8_t stateMenu;
  uint8_t stateUp;
  uint8_t stateDown;

  uint8_t system_status;
  byte flag_require_quit;
  boolean flag_adjust_time;
  boolean flag_alarm_over;

  void keyInit();
  void ledInit();
  void sensorInit();
  void buzzerInit();
};
extern TM1636 tm1636;

#endif

TickTockShield.cpp

#include "TickTockShield.h"
#include <Arduino.h>
#include <EEPROM.h>
#include <Wire.h>
#include <TimerOne.h>
#include "TM1636.h"

TM1636 tm1636(7,8);
void timerIsr();
/*global variables also used in timer interrupt serve routine */
uint8_t key_menu_status;
uint16_t count_10ms_pressed;
uint16_t count_10ms_release;
boolean flag_pressed_3s;
boolean flag_release_10s;
boolean alarm_on_off;

int8_t disp[4];
int8_t disp_buff[4];
byte g_hand;

//--------------------------------//
void TickTockShield::init()
{
	g_brightness = BRIGHT_TYPICAL;
  tm1636.set(g_brightness);
  tm1636.init();
  keyInit();
  ledInit();
  sensorInit();
	buzzerInit();
  Wire.begin();
	pre_pin_number = -1;
	Timer1.initialize(100000); // set a timer of length 100000 microseconds 
		//(or 0.1 sec - or 10Hz => the led will blink 5 times, 5 cycles of on-and-off, per second)
  Timer1.attachInterrupt( timerIsr ); // attach the service routine here
}

void TickTockShield::keyInit()
{
  //Set all three keys to be input and internal pull-up

  pinMode(KEY_MENU, INPUT);
  digitalWrite(KEY_MENU, HIGH);
  pinMode(KEY_UP, INPUT);
  digitalWrite(KEY_UP, HIGH);
  pinMode(KEY_DOWN, INPUT);
  digitalWrite(KEY_DOWN, HIGH);
}
void TickTockShield::ledInit()
{
  pinMode(LED_CLOCK_ADJUST, OUTPUT);
  pinMode(LED_ALARM_ADJUST, OUTPUT);
  pinMode(LED_BRIGHT_ADJUST, OUTPUT);
  pinMode(LED_ALARM_ENABLE, OUTPUT);
	turnOffLED();
}
void TickTockShield::sensorInit()
{
  pinMode(TEMPERATURE_SENSOR, INPUT);
  pinMode(LIGHT_SENSOR, INPUT);
}
void TickTockShield::buzzerInit()
{
	pinMode(ALARM_BUZZER, OUTPUT);
  pinMode(ALARM_BUZZER, OUTPUT);
}
void TickTockShield::setLed(unsigned char led_status, int pinLED)
{
	digitalWrite(pinLED, led_status);
}
/********************************/
void TickTockShield::turnOffLED()
{
	setLed(LOW,LED_CLOCK_ADJUST);
	setLed(LOW,LED_ALARM_ADJUST);
	setLed(LOW,LED_BRIGHT_ADJUST);
	setLed(LOW,LED_ALARM_ENABLE);
}
/********************************/
void TickTockShield::turnOnLED()
{
	setLed(HIGH,LED_CLOCK_ADJUST);
	setLed(HIGH,LED_ALARM_ADJUST);
	setLed(HIGH,LED_BRIGHT_ADJUST);
	setLed(HIGH,LED_ALARM_ENABLE);
}

/*******************************/
void TickTockShield::runLED(byte speed, byte direction)
{
	if((speed > 0)&&(speed < 11))//If the value of speed is valid?
	{
		turnOffLED();
		uint8_t shifter = 0x01;
		if(LEFT_TO_RIGHT == direction)
		{
			for(uint8_t i = 0;i < 4;i ++)
			{
				if(shifter&0x01) setLed(LED_ON,  LED_CLOCK_ADJUST);
				else						 setLed(LED_OFF, LED_CLOCK_ADJUST);
				if(shifter&0x02) setLed(LED_ON,  LED_ALARM_ADJUST);
				else					   setLed(LED_OFF, LED_ALARM_ADJUST);
				if(shifter&0x04) setLed(LED_ON,  LED_ALARM_ENABLE);
				else						 setLed(LED_OFF, LED_ALARM_ENABLE);
				if(shifter&0x08) setLed(LED_ON,  LED_BRIGHT_ADJUST);
				else						 setLed(LED_OFF, LED_BRIGHT_ADJUST);
				shifter <<= 1;
				delay(500/speed);
			}
		}
		else
		{
			for(uint8_t i = 0;i < 4;i ++)
			{
				if(shifter&0x01) setLed(LED_ON,  LED_BRIGHT_ADJUST);
				else						 setLed(LED_OFF, LED_BRIGHT_ADJUST);
				if(shifter&0x02) setLed(LED_ON,  LED_ALARM_ENABLE);
				else					   setLed(LED_OFF, LED_ALARM_ENABLE);
				if(shifter&0x04) setLed(LED_ON,  LED_ALARM_ADJUST);
				else						 setLed(LED_OFF, LED_ALARM_ADJUST);
				if(shifter&0x08) setLed(LED_ON,  LED_CLOCK_ADJUST);
				else						 setLed(LED_OFF, LED_CLOCK_ADJUST);
				shifter <<= 1;
				delay(500/speed);
			}
		}
		turnOffLED();
	}
}
//--------------------------------//
//-Return:the pin number of the key pressed
//--------------------------------//
//
int16_t TickTockShield::scanKey()
{
	int16_t pin_number = 0;
	static boolean pre_key_menu_level = HIGH;
	boolean cur_key_menu_level;
	if(digitalRead(KEY_MENU) == LOW)
	{
		delay(30);
		if(digitalRead(KEY_MENU) == LOW)
		{
			cur_key_menu_level = LOW;
			pin_number = KEY_MENU;
		}
		else cur_key_menu_level = HIGH;
	}
	else
	{
		cur_key_menu_level = HIGH;
	}
	if(pre_key_menu_level > cur_key_menu_level)
	{

		key_menu_status = KEY_PRESSED;
		if(system_status < 7)system_status ++;
		count_10ms_pressed = 0;
		flag_pressed_3s = 0;
	#ifdef DEBUG
		Serial.print("system_status = ");
	  Serial.println(system_status);
		Serial.println("Key_Menu pressed");
	#endif
	}
	else if(pre_key_menu_level < cur_key_menu_level)
	{
		key_menu_status = KEY_RELEASE;
		count_10ms_release = 0;
		flag_release_10s = 0;
	#ifdef DEBUG
		Serial.println("Key_Menu release");
  #endif
	}
	pre_key_menu_level = cur_key_menu_level;

	if(digitalRead(KEY_UP) == LOW)
	{
		delay(20);
		if(digitalRead(KEY_UP) == LOW)
		{
			pin_number = KEY_UP;
			count_10ms_release = 0;
			flag_release_10s = 0;
		#ifdef DEBUG
			Serial.println("Key_UP pressed");
		#endif
		}

	}
	else if(digitalRead(KEY_DOWN) == LOW)
	{
		delay(20);
		if(digitalRead(KEY_DOWN) == LOW)
		{
			pin_number = KEY_DOWN;
			count_10ms_release = 0;
			flag_release_10s = 0;
		#ifdef DEBUG
			Serial.println("KEY_DOWN pressed");
		#endif
		}
	}
	if(pin_number == 0)pin_number = -1;
	key_pin_pressed = pin_number;
	return key_pin_pressed;
}
/*************************************************/
/*Function:*/
/*Return:   void*/
void TickTockShield::processKey()
{
	if((key_pin_pressed > 0)&&(pre_pin_number != key_pin_pressed))
	{
		ringOn();
		delay(100);
		ringOff();
	}
	pre_pin_number = key_pin_pressed;
	if(flag_pressed_3s)
	{
		flag_pressed_3s = 0;
		system_status = SS_CONFIRM_EXIT;
	#ifdef DEBUG
		Serial.print("system_status = ");
	  Serial.println(system_status);
		Serial.println("Press for 3s...");
	#endif
	}
	if(flag_release_10s)
	{
		flag_release_10s = 0;
		system_status = SS_QUIT;
	#ifdef DEBUG
		Serial.print("system_status = ");
	  Serial.println(system_status);
		Serial.println("Release for 5s");
	#endif
	}	
}
void TickTockShield::processSystemStatus()
{
	switch(system_status)
	{
		case SS_NO_INPUT:			
													break;										
		case SS_CLOCKH_ADJUST:adjustClock(HOUR);
													break;	
		case SS_CLOCKM_ADJUST:adjustClock(MINUTE);
													break;	
		case SS_ALARMH_ADJUST:adjustAlarm(HOUR);
													break;	
		case SS_ALARMM_ADJUST:adjustAlarm(MINUTE);
													break;	
		case SS_ALARM_ENABLE: flag_adjust_time = 0;
													modifyAlarmFlag();
													break;	
		case SS_BRIGHT_ADJUST:adjustBrightness();
													break;	
		case SS_CONFIRM_EXIT: saveChanges();
													system_status = SS_NO_INPUT;
													break;	
		case SS_QUIT:					quit();
													system_status = SS_NO_INPUT;
													break;
		default:break;	
	}

}
void TickTockShield::quit()
{
	flag_require_quit = 1;
	flag_adjust_time = 0;
	tm1636.set(g_brightness);
	turnOffLED();
	if(alarm.flag_enable)
		setLed(HIGH,LED_ALARM_ENABLE);
	else setLed(LOW,LED_ALARM_ENABLE);

}
void TickTockShield::saveChanges()
{
	g_hour   = g_hour_temp;
	g_minute = g_minute_temp;

    // added by Johannes
    // do this once to set the date
 //   g_dayOfMonth = 3;
 //   g_month = 11;

	writeTime();

	alarm.hour        = alarm_temp.hour;
	alarm.minute      = alarm_temp.minute;
	alarm.flag_enable = alarm_temp.flag_enable;
	setAlarm(alarm);

	g_brightness 			= g_brightness_temp;
	tm1636.set(g_brightness);

	flag_alarm_over = 0;
	quit();
}
inline void TickTockShield::modifyAlarmFlag()
{
	turnOffLED();
	if(key_pin_pressed == KEY_UP)
	{
		alarm_temp.flag_enable = ~alarm_temp.flag_enable;
	}
	if(alarm_temp.flag_enable)
	{
		setLed(HIGH,LED_ALARM_ENABLE);
	}
	else
	{
		setLed(LOW,LED_ALARM_ENABLE);
	}
	disp[0] = alarm_temp.hour/10;
	disp[1] = alarm_temp.hour%10;
	disp[2] = alarm_temp.minute/10;
	disp[3] = alarm_temp.minute%10;
	tm1636.display(disp);
}
void TickTockShield::adjustClock(uint8_t hand)
{
	turnOffLED();
	setLed(HIGH,LED_CLOCK_ADJUST);
	if(hand == HOUR)
	{
		g_hand = HOUR;
		if(key_pin_pressed == KEY_DOWN)
		{
			if(g_hour_temp> 0)
				g_hour_temp--;
		}
		else if(key_pin_pressed == KEY_UP)
		{
			if(g_hour_temp< 23)
				g_hour_temp++;
			else g_hour_temp = 0;
		}
	}
	else
	{
		g_hand = MINUTE;
		if(key_pin_pressed == KEY_DOWN)
		{
			if(g_minute_temp> 0)
				g_minute_temp--;
		}
		else if(key_pin_pressed == KEY_UP)
		{
			if(g_minute_temp< 59)
				g_minute_temp++;
			else g_minute_temp = 0;
		}
	}
	disp[0] = g_hour_temp/10;
	disp[1] = g_hour_temp%10;
	disp[2] = g_minute_temp/10;
	disp[3] = g_minute_temp%10;
//	tm1636.display(disp);
}
void TickTockShield::adjustAlarm(uint8_t hand)
{
	turnOffLED();
	setLed(HIGH,LED_ALARM_ADJUST);
	if(hand == HOUR)
	{
		g_hand = HOUR;
		if(key_pin_pressed == KEY_DOWN)
		{
			if(alarm_temp.hour> 0)
				alarm_temp.hour--;
		}
		else if(key_pin_pressed == KEY_UP)
		{
			if(alarm_temp.hour< 23)
				alarm_temp.hour++;
			else alarm_temp.hour = 0;
		}
	}
	else
	{
		g_hand = MINUTE;
		if(key_pin_pressed == KEY_DOWN)
		{
			if(alarm_temp.minute> 0)
				alarm_temp.minute--;
		}
		else if(key_pin_pressed == KEY_UP)
		{
			if(alarm_temp.minute< 59)
				alarm_temp.minute++;
			else alarm_temp.minute = 0;
		}
	}
	disp[0] = alarm_temp.hour/10;
	disp[1] = alarm_temp.hour%10;
	disp[2] = alarm_temp.minute/10;
	disp[3] = alarm_temp.minute%10;
//	tm1636.display(disp);
}
/***********************************/
void TickTockShield::adjustBrightness()
{
	turnOffLED();
	setLed(HIGH,LED_BRIGHT_ADJUST);
	if(key_pin_pressed == KEY_DOWN)
	{
		if(g_brightness_temp> 0)
			g_brightness_temp--;
	}
	else if(key_pin_pressed == KEY_UP)
	{
		if(g_brightness_temp< 7)
			g_brightness_temp++;
	}
	tm1636.set(g_brightness_temp);
	disp[0] = alarm_temp.hour/10;
	disp[1] = alarm_temp.hour%10;
	disp[2] = alarm_temp.minute/10;
	disp[3] = alarm_temp.minute%10;
	tm1636.display(disp);
}
void TickTockShield::adjustBrightness(uint8_t grayscale)
{
	g_brightness = grayscale;
	tm1636.set(g_brightness);
}
/****************************************************************/
/*Return:int8_t,Temperature that range from -40 to 125 degrees.					*/
int8_t TickTockShield::getTemperature()
{
	float temperature,resistance;
	int a;
	int B = 3975;
	a = analogRead(TEMPERATURE_SENSOR);
	resistance   = (float)(1023-a)*RESISTOR_CONNECT_THERMISTOR/a; 
	temperature  = 1/(log(resistance/RESISTOR_CONNECT_THERMISTOR)/B+1/298.15)-273.15;
	return (int8_t)temperature;
}
/**********************************************************************/
/*Function:   Display the temperature on the 4-digit display										*/
/*Parameter:-int8_t temperature,Temperature that range from -40 to 125 degrees. */
/*Return:	  void																												*/
void TickTockShield::displayTemperature(int8_t temperature)
{
  int8_t temp[4];
  if(temperature < 0)
	{
		temp[0] = INDEX_NEGATIVE_SIGN;
		temperature = abs(temperature);
	}
	else if(temperature < 100)temp[0] = INDEX_BLANK;
	else temp[0] = temperature/100;
	temperature %= 100;
	temp[1] = temperature / 10;
	temp[2] = temperature % 10;
	temp[3] = 12;	          //index of 'C' for celsius degree symbol.
	tm1636.display(temp);
}

float TickTockShield::getLightIntensity()
{
  int sensorValue = analogRead(LIGHT_SENSOR);
  float rsensor;
  rsensor=(float)(1023-sensorValue)*10/sensorValue;
	return rsensor;
}

uint8_t TickTockShield::getLightLevel()
{
	uint16_t resistance;
	uint8_t light_level;
	resistance = (uint16_t)getLightIntensity();
	if(resistance < 10) light_level = 0;
	else if(resistance < 50)light_level = 1;
	else if(resistance < 80)light_level = 2;
	else if(resistance < 110)light_level = 3;
	else if(resistance < 140)light_level = 4;
	else if(resistance < 170)light_level = 5;
	else if(resistance < 200)light_level = 6;
	else light_level = 7;
	return light_level;
}

void TickTockShield::ringOn()
{
	digitalWrite(ALARM_BUZZER, HIGH);
}
void TickTockShield::ringOff()
{
	digitalWrite(ALARM_BUZZER, LOW);
}
void TickTockShield::alarming()
{
	if(alarm_on_off)ringOn();
	else ringOff();
}
void TickTockShield::turnOffAlarm()
{
	ringOff();
	flag_alarm_over = 1;
}
boolean TickTockShield::isAlarmEnable()
{
	if(compareWithAlarm())
	{
		if(!flag_alarm_over)

			return true;
	}
	else flag_alarm_over = 0;
	return false;
}
/*******************************/
boolean TickTockShield::compareWithAlarm()
{
	if(alarm.flag_enable > 0)
	{
		if((alarm.hour == g_hour)&&(alarm.minute == g_minute))
		{
			return true;
		}
	}
	return false;
}

inline void TickTockShield::alarmDisable()
{}
void TickTockShield::setAlarm(struct AlarmStruct alarm_)
{
	EEPROM.write(5,alarm_.hour);
	EEPROM.write(6,alarm_.minute);
	EEPROM.write(7,alarm_.flag_enable);

}
void TickTockShield::setAlarm(uint8_t hour,uint8_t minute,uint8_t flag_enable)
{
	EEPROM.write(5,hour);
	EEPROM.write(6,minute);
	EEPROM.write(7,flag_enable);
}

/************************************************************/
/*Function:Read the alarm clock information from the built-in EEPROM  */
/*Return:   void																							   */
void TickTockShield::getAlarm()
{
	alarm.hour        = EEPROM.read(5);
	alarm.minute      = EEPROM.read(6);
	alarm.flag_enable = EEPROM.read(7);
#ifdef DEBUG
	Serial.print("alarm = ");
  Serial.print(alarm.hour);
	Serial.print(" : ");
	Serial.print(alarm.minute);
#endif
	if(alarm.flag_enable)
		setLed(HIGH,LED_ALARM_ENABLE);
	else setLed(LOW,LED_ALARM_ENABLE);
}
/***********************************************************/
/*Function:write data to the global variables in Ajust Area 					  */
/*Return:   void																							   */
void TickTockShield::writeToAdjustArea()
{
	g_hour_temp       = g_hour;
	g_minute_temp     = g_minute;
	g_brightness_temp = g_brightness;
	alarm_temp.hour        = alarm.hour;
	alarm_temp.minute      = alarm.minute;
	alarm_temp.flag_enable = alarm.flag_enable;
	flag_require_quit = 0;
	flag_adjust_time = 1;
	pre_pin_number = -1;
	tm1636.point(POINT_ON);
	turnOffLED();
}

/***********************************************************/
/*Function:write data to the global variables in Normal Area    			  */
/*Return:   void																							   */
void TickTockShield::writeToNormalArea()
{
	g_hour       = g_hour_temp;
	g_minute     = g_minute_temp;
	g_brightness = g_brightness_temp;
	alarm.hour        = alarm_temp.hour;
	alarm.minute      = alarm_temp.minute;
	alarm.flag_enable = alarm_temp.flag_enable;
}

/************************************************************/
/*Frunction: Write the time that includes the date to the RTC chip			*/
/*Return:	  void																							*/
void TickTockShield::writeTime()
{
	Wire.beginTransmission(DS1307_I2C_ADDRESS);
	Wire.write((byte)0x00);
	Wire.write(decToBcd(g_second)); 	 // 0 to bit 7 starts the clock
	Wire.write(decToBcd(g_minute));
	Wire.write(decToBcd(g_hour)); 	 // If you want 12 hour am/pm you need to set
									// bit 6 (also need to change readDateDs1307)
	Wire.write(decToBcd(g_dayOfWeek));
	Wire.write(decToBcd(g_dayOfMonth));
	Wire.write(decToBcd(g_month));
	Wire.write(decToBcd(g_year));
	Wire.endTransmission();
}
/************************************************/
/*Frunction: Read time from RTC											*/
/*Parameter:A 2-byte array that tells the hour and			*/ 
//			minute at the end of the function.

void TickTockShield::getTime()
{
    // Reset the register pointer
	Wire.beginTransmission(DS1307_I2C_ADDRESS);
	Wire.write((byte)0x00);
	Wire.endTransmission();  
	Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
	// A few of these need masks because certain bits are control bits
	g_second	   = bcdToDec(Wire.read() & 0x7f);
	g_minute	   = bcdToDec(Wire.read());
	g_hour	     = bcdToDec(Wire.read() & 0x3f);// Need to change this if 12 hour am/pm
	g_dayOfWeek  = bcdToDec(Wire.read());
	g_dayOfMonth = bcdToDec(Wire.read());
	g_month      = bcdToDec(Wire.read());
	g_year	     = bcdToDec(Wire.read());
}
void TickTockShield::displayTime()
{
  unsigned char time[4];
  time[0] = g_hour/10;
  time[1] = g_hour%10;
  time[2] = g_minute/10;
  time[3] = g_minute%10;
  tm1636.display((int8_t*)time);
}
void TickTockShield::displayDate()
{
    unsigned char time[4];
    time[0] = g_dayOfMonth/10;
    time[1] = g_dayOfMonth%10;
    time[2] = g_month/10;
    time[3] = g_month%10;
    tm1636.display((int8_t*)time); //added by Johannes
}

void TickTockShield::display(int8_t DispData [])
{
	tm1636.display(DispData);
}

void TickTockShield::clearTime(uint8_t hand)
{
	unsigned char time[4];
	if(hand == HOUR)
	{
		time[0] = INDEX_BLANK;
  	time[1] = INDEX_BLANK;
  	time[2] = g_minute/10;
  	time[3] = g_minute%10;
	}
	else 
	{
		time[0] = g_hour/10;
	  time[1] = g_hour%10;
	  time[2] = INDEX_BLANK;
	  time[3] = INDEX_BLANK;
	}
	tm1636.display((int8_t*)time);
}
// Convert normal decimal numbers to binary coded decimal
byte TickTockShield::decToBcd(byte val)
{
	return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte TickTockShield::bcdToDec(byte val)
{
	return ( (val/16*10) + (val%16) );
}

boolean TickTockShield::isAdjustingTime()
{
	if(flag_adjust_time)return true;
	else return false;
}
/// --------------------------
/// Custom ISR Timer Routine
/// --------------------------
void timerIsr()
{
	alarm_on_off = (~alarm_on_off) & 0x01;
  if(key_menu_status == KEY_PRESSED)
 	{
 		count_10ms_pressed ++;
		if(count_10ms_pressed == 30)
		{
			flag_pressed_3s = 1;
			key_menu_status = 0;
		}
 	}
	else if(key_menu_status == KEY_RELEASE)
	{
		count_10ms_release ++;
		if(count_10ms_release == 100)
		{
			flag_release_10s = 1;
			key_menu_status = 0;
		}
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *