1. Trammell Hudson
  2. alphaclock

Commits

Trammell Hudson  committed 6c53bf5

whitespace

  • Participants
  • Parent commits dd1b4b6
  • Branches default

Comments (0)

Files changed (1)

File alphaclock_18_Rev1_0/alphaclock_18_Rev1_0.pde

View file
 /*
-alphaclock_18.pde 
- 
- 
+alphaclock_18.pde
+
+
  Software for the Five Letter Word Clock desiged by
  Evil Mad Scientist Laboratories http://www.evilmadscientist.com
- 
+
  Target: ATmega644A, clock at 16 MHz.
- 
+
  Designed to work with Arduino 23; untested with other versions.
  Also (1) requires the "old" DateTime library:  http://www.arduino.cc/playground/Code/DateTime
- Also (2) requires Sanguino extensions to Arduino, rev 0023, available from: 
+ Also (2) requires Sanguino extensions to Arduino, rev 0023, available from:
  http://code.google.com/p/sanguino/downloads/list
- 
+
  * May require "boards.txt" file to be edited as well, for atmega644 (not -P).
  * May require "pins_arduino.c" and "pins_arduino.h" to be edited as well, for 644.
- - Arduino does not directly support the '644A, so the outgoing serial port may not be usable from 
+ - Arduino does not directly support the '644A, so the outgoing serial port may not be usable from
  within the Arduino IDE.
  * Bootloader should be set to give device signature as ATmega644P.
- 
+
  Untested with newer versions of Arduino.
- 
- 
+
+
  Version 1.0 - 12/17/2011
  Copyright (c) 2011 Windell H. Oskay.  All right reserved.
  http://www.evilmadscientist.com/
- 
+
  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 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 General Public License for more details.
- 
+
  You should have received a copy of the GNU General Public License
  along with this library.  If not, see <http://www.gnu.org/licenses/>.
- 	  
+ 	
  */
 
 
  * are indexed from right to left.
  */
 static inline void
-led_digit_select(
+digit_select(
 	const byte j
 )
 {
 
 /** Turn off the currently selected LED digit driver */
 static inline void
-led_digit_off(void)
+digit_off(void)
 {
 	sbi(PORTA, 6);
 }
 
 /** Turn on the currently selected LED digit driver */
 static inline void
-led_digit_on(void)
+digit_on(void)
 {
 	cbi(PORTA, 6);
 }
 
 
 // The buttons are located at PB0, PB1, PB2, PB3
-#define buttonmask 15 
+#define buttonmask 15
 
 // Default "idle" time to wait before saving settings in EEPROM, usually 5 seconds (5000U)
 #define IdleDelay  5000U
 
 byte PINBLast;
 
-byte SecNow; 
+byte SecNow;
 
 byte MinNow;
 byte MinNowTens;
         1 << 0xF,
         1 << 0x4,
         1 << 0xA,
-        
+
         // Chaser goes around the outside of the box
         1 << 0x0,
         1 << 0x1,
 
 
 /*
- * Write a digit to the LED display.
- *
- * The top two bits are used to select the dots.
+ * Write a digit to the currently selected LED display.
  *
  * \note Blocks until it is done.
  */
 display_string(
 	const char WordIn[]
 )
-{ 
+{
 	for (byte i = 0 ; i < NUM_DIGITS ; i++)
 		display_char(i, WordIn[i]);
 }
 	unsigned int durationMillis
 )
 {
-	WordStopTime = millis() + durationMillis; 
+	WordStopTime = millis() + durationMillis;
 	display_string(WordIn);
-	DisplayWordMode = 1; 
+	DisplayWordMode = 1;
 	OptionNameSequence++;
 }
 
 byte MainBright;
 byte FadeMode;  // Not used at present.
 byte HourMode24;
-byte AlarmEnabled; // If the "ALARM" function is currently turned on or off. 
+byte AlarmEnabled; // If the "ALARM" function is currently turned on or off.
 byte AlarmTimeHr;
 byte AlarmTimeMin;
 byte NightLightType;   //0
 
 // "Factory" default configuration can be configured here:
 #define MainBrightDefault 5
-#define FadeModeDefault 1 
+#define FadeModeDefault 1
 #define HourMode24Default 0
 #define AlarmEnabledDefault 0
 #define AlarmTimeHrDefault 7
 
     /*if (Serial.read() != TIME_HEADER)
      {
-     while((Serial.peek() != TIME_HEADER) &&  (Serial.peek() >= 0))  // Flush buffer up until next 0xFF.    
+     while((Serial.peek() != TIME_HEADER) &&  (Serial.peek() >= 0))  // Flush buffer up until next 0xFF.
      Serial.read();
-     
+
      display_sequence("FLUSH",500);     //TODO: Remove this debug message
      }
      */
 
-    if( Serial.read() == TIME_HEADER) { 
+    if( Serial.read() == TIME_HEADER) {
 
       //   display_sequence("RECV ",100);  //TODO: Remove this debug message
       //    Read command, next two bytes:
       charTemp = Serial.read();
       charTemp2 = Serial.read();
 
-      if( charTemp == 'S' ){ 
+      if( charTemp == 'S' ){
 
         // display_sequence("RECVS",100);  //TODO: Remove this debug message
-        if( charTemp2 == 'T' ){ 
+        if( charTemp2 == 'T' ){
           // Time setting mode:
           //display_sequence("RECVT",100);  //TODO: Remove this debug message
 
           time_t pctime = 0;
-          for( i=0; i < 10; i++){   
-            char c= Serial.read();          
-            if( c >= '0' && c <= '9'){   
-              pctime = (10 * pctime) + (c - '0') ; // convert digits to a number    
+          for( i=0; i < 10; i++){
+            char c= Serial.read();
+            if( c >= '0' && c <= '9'){
+              pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
             }
-          }   
+          }
           DateTime.sync(pctime);   // Sync Arduino clock to the time received on the serial port
           return true;   // return true if time message received on the serial port
         }
 		for( i=0; i < NUM_DIGITS; i++)
 			display_dots[i] = Serial.read();
 
-          DisplayWordMode = 1;  
+          DisplayWordMode = 1;
           SerialDisplayMode  = 1;
           // Serial.println("Writing Text!");
         }
       }
-      else if( charTemp == 'M' ){ 
+      else if( charTemp == 'M' ){
         if( charTemp2 == 'T' )
         {
           // Clock display mode
           SerialDisplayMode  = 0;
           // Serial.println("Resuming clock display!");
           for( i=0; i < 10; i++){     // Read dummy input....
-            charTemp = Serial.read();          
-          } 
+            charTemp = Serial.read();
+          }
         }
 
       }
   Serial.print(" ");
   Serial.print(DateTimeStrings.monthStr(DateTime.Month));
   Serial.print(" ");
-  Serial.println(DateTime.Day,DEC); 
+  Serial.println(DateTime.Day,DEC);
 }
 
 
-// Modes: 
+// Modes:
 
 byte VCRmode;  // In VCR mode, the clock blinks at you because the time hasn't been set yet.
 //byte FactoryResetDisable;  // To make sure that we don't accidentally reset the settings...
 
 byte SettingTime;
-byte SettingAlarm; 
+byte SettingAlarm;
 byte AlarmNow;    // Alarm is actually going off, right now.
 
 byte HoldTimeSet;
 byte HoldAlarmSet;
 byte HoldLoopCount;
 
-byte     MomentaryOverridePlus; 
-byte     MomentaryOverrideMinus; 
+byte     MomentaryOverridePlus;
+byte     MomentaryOverrideMinus;
 
 unsigned long prevtime;
-unsigned long millisCopy; 
+unsigned long millisCopy;
 unsigned long NextAdvance;
 unsigned long NextAdvanceSound;
 unsigned long endIdleTime;
 void ApplyDefaults (void) {
 
   // VARIABLES THAT HAVE EEPROM STORAGE AND DEFAULTS...
-  // FadeMode = FadeModeDefault; 
+  // FadeMode = FadeModeDefault;
   MainBright = MainBrightDefault;
   HourMode24 = HourMode24Default;
   AlarmEnabled = AlarmEnabledDefault;
   AlarmTimeHr = AlarmTimeHrDefault;
   AlarmTimeMin = AlarmTimeMinDefault;
   AlarmTone = AlarmToneDefault;
-  NightLightType = NightLightTypeDefault;  
+  NightLightType = NightLightTypeDefault;
 }
 
-void EEReadSettings (void) { 
+void EEReadSettings (void) {
 
-  byte detectBad = 0; 
+  byte detectBad = 0;
   byte value = 255;
 
-  value = EEPROM.read(0);      
+  value = EEPROM.read(0);
 
   if ((value >= 1) && (value <= 13))
-    MainBright = value;  // MainBright has maximum possible value of 13.    
+    MainBright = value;  // MainBright has maximum possible value of 13.
   else  {
     MainBright = MainBrightDefault;  // Turn back on when power goes back on-- don't leave it dark.
     EESaveSettings();
   value = EEPROM.read(1);
   if (value > 1)
     detectBad = 1;
-  else  
+  else
     HourMode24 = value;
 
 
   value = EEPROM.read(2);
   if (value > 1)
     detectBad = 1;
-  else  
+  else
     AlarmEnabled = value;
 
   value = EEPROM.read(3);
   if (value > 23)
     detectBad = 1;
-  else  
-    AlarmTimeHr = value;   
+  else
+    AlarmTimeHr = value;
 
   value = EEPROM.read(4);
   if (value > 59)
     detectBad = 1;
-  else  
-    AlarmTimeMin = value;   
+  else
+    AlarmTimeMin = value;
 
   value = EEPROM.read(5);
   if (value > 4)
     detectBad = 1;
-  else  
-    AlarmTone = value;      
+  else
+    AlarmTone = value;
 
   value = EEPROM.read(6);
   if (value > 4)
     detectBad = 1;
-  else  
-    NightLightType = value;      
+  else
+    NightLightType = value;
 /*
 if (detectBad){
     ApplyDefaults();
 void EESaveSettings (void){
   //EEPROM.write(Addr, Value);
 
-  byte detectBad = 0; 
-  byte EEPROMwritten = 0; 
+  byte detectBad = 0;
+  byte EEPROMwritten = 0;
   byte value = 255;
 
   // Careful if you use  this function: EEPROM has a limited number of write
 
   if (MainBright > 13)
     detectBad = 1;
-  else  
+  else
   {
-    value = EEPROM.read(0);  
+    value = EEPROM.read(0);
     if (MainBright != value){
       EEPROM.write(0, MainBright);
       EEPROMwritten = 1;
 
   if (HourMode24 > 1)
     detectBad = 1;
-  else  
+  else
   {
-    value = EEPROM.read(1);  
+    value = EEPROM.read(1);
     if (HourMode24 != value){
-      EEPROM.write(1, HourMode24); 
+      EEPROM.write(1, HourMode24);
       EEPROMwritten = 1;
     }
   }
 
   if (AlarmEnabled > 1)
     detectBad = 1;
-  else  
+  else
   {
-    value = EEPROM.read(2);  
+    value = EEPROM.read(2);
     if (AlarmEnabled != value){
-      EEPROM.write(2, AlarmEnabled); 
+      EEPROM.write(2, AlarmEnabled);
       EEPROMwritten = 1;
     }
   }
 
   if (AlarmTimeHr > 23)
     detectBad = 1;
-  else  
+  else
   {
-    value = EEPROM.read(3);  
+    value = EEPROM.read(3);
     if (AlarmTimeHr != value){
       EEPROM.write(3, AlarmTimeHr);
       EEPROMwritten = 1;
 
   if (AlarmTimeMin > 59)
     detectBad = 1;
-  else  
+  else
   {
-    value = EEPROM.read(4);  
+    value = EEPROM.read(4);
     if (AlarmTimeMin != value){
       EEPROM.write(4, AlarmTimeMin);
       EEPROMwritten = 1;
 
   if (AlarmTone > 4)
     detectBad = 1;
-  else  
+  else
   {
-    value = EEPROM.read(5);  
+    value = EEPROM.read(5);
     if (AlarmTone != value){
       EEPROM.write(5, AlarmTone);
       EEPROMwritten = 1;
 
   if (NightLightType > 4)
     detectBad = 1;
-  else  
+  else
   {
-    value = EEPROM.read(6);  
+    value = EEPROM.read(6);
     if (NightLightType != value){
-      EEPROM.write(6, NightLightType); 
+      EEPROM.write(6, NightLightType);
       EEPROMwritten = 1;
     }
   }
 
 
   if (EEPROMwritten)
-  {   
+  {
     led_all_off();
     delay(100);
   }
 
   Wire.send(sh); //Send hours as BCD
 
-  Wire.endTransmission();  
+  Wire.endTransmission();
 
 }
 
 { // Read out time from RTC module, if present
   // send request to receive data starting at register 0
 
-  byte status = 0; 
+  byte status = 0;
   Wire.beginTransmission(104); // 104 is DS3231 device address
   Wire.send(0); // start at register 0
   Wire.endTransmission();
   byte updatetime = 0;
 
   while(Wire.available())
-  { 
+  {
     status = 1;
     seconds = Wire.receive(); // get seconds
     minutes = Wire.receive(); // get minutes
     minutes = (((minutes & 0b11110000)>>4)*10 + (minutes & 0b00001111)); // convert BCD to decimal
     hours = (((hours & 0b00110000)>>4)*10 + (hours & 0b00001111)); // convert BCD to decimal (assume 24 hour mode)
 
-    //Optional: report time:: 
+    //Optional: report time::
     // Serial.print(hours); Serial.print(":"); Serial.print(minutes); Serial.print(":"); Serial.println(seconds);
 
 
       temptime2 = 3600*HrNow + 60*MinNow + SecNow;    // Internally stored time estimate.
 
       if (temptime1 > temptime2)
-      { 
+      {
         if ((temptime1 - temptime2) > 2)
           updatetime = 1;
       }
       else
-      { 
+      {
         if ((temptime2 - temptime1) > 2)
           updatetime = 1;
-      } 
+      }
     }
 
     if (ExtRTC == 0)
 
   if (MinNow > 59){
     MinNow -= 60;
-    HrNow++; 
+    HrNow++;
 
     if  (HrNow > 23)
-      HrNow -= 24; 
+      HrNow -= 24;
   }
 
 
     {
       HrNowTens = 1;
       HrNowOnes = 2;
-    } 
-    else{   
+    }
+    else{
       HrNowTens = HrNowTemp / 10;
       HrNowOnes = HrNowTemp - 10 * HrNowTens;
 
 
   if (AlarmTimeMin > 59){
     AlarmTimeMin -= 60;
-    AlarmTimeHr++; 
+    AlarmTimeHr++;
 
     if  (AlarmTimeHr > 23)
-      AlarmTimeHr = 0; 
+      AlarmTimeHr = 0;
   }
 
   MinAlrmTens = AlarmTimeMin / 10;
     {
       HrAlrmTens = 1;
       HrAlrmOnes = 2;
-    } 
-    else{   
+    }
+    else{
       HrAlrmTens = AlarmTimeHrTemp / 10;
       HrAlrmOnes = AlarmTimeHrTemp - 10 * HrAlrmTens;
 
 
   snoozed = 0;  //  Recalculating alarm time *turns snoose off.*
 
-  if (AlarmEnabled)  
+  if (AlarmEnabled)
     if ((AlarmTimeHr == HrNow ) && (AlarmTimeMin == MinNow ))
     {
       AlarmNow = 1;
       NextAdvanceSound = 0;
       SoundSequence = 0;
-    } 
+    }
 
 }
 
 	const byte on_period,
 	const byte off_period
 )
-{  
+{
 	digit_write(c, dots);
 	Latch();
-	led_digit_select(digit);
+	digit_select(digit);
 
 	// For low brightness levels, we use a very low duty cycle PWM.
 	// That's normally fine, but the Arduino interrupts in the background
 	// for just long enough to turn on the LEDs for a few clock cycles
 	// (under 1 us).   Once interrupts are back on, pending interrupt
 	// requests will be serviced, so we should not lose any time in the
-	// process.  *However* take great care if you extend this 
+	// process.  *However* take great care if you extend this
 	// "interrupt free" section of the code to any longer duration.
 
 	if (on_period != 0)
 	{
-		led_digit_on();
+		digit_on();
 		delayMicroseconds(on_period);
-		led_digit_off();
+		digit_off();
 	} else
 	{
 		// Lower power mode
 		const byte SREGtemp = SREG;
 		cli(); // Disable interrupts
-		led_digit_on();
+		digit_on();
 
-		asm volatile("nop\n\t"
-				"nop\n\t"
-				"nop\n\t"
-				"nop\n\t"
-				"nop\n\t"
-				::);
+		asm volatile(
+			"nop\n\t"
+			"nop\n\t"
+			"nop\n\t"
+			"nop\n\t"
+			"nop\n\t"
+			::
+		);
 
-		led_digit_off();
+		digit_off();
 		SREG = SREGtemp; // reenable interrupts.
 	}
 
 	byte bright = MainBright;
 
 	if (VCRmode && (SecNow % 2) == 0)
-		bright = 0; 
+		bright = 0;
 
 	// If the brightness == 0, blank the LED driver and do nothing else
 	if (bright == 0)
 	{
-		led_digit_off();
+		digit_off();
 		return;
 	}
 
 
 static void
 TurnAlarmOff(void)
-{       
+{
 	AlarmNow = 0;
 	noTone(13);
 	speaker_off();
 
 
 void setup()                    // run once, when the sketch starts
-{ 
+{
 
   PORTA = 127;
   PORTB = buttonmask;  // Pull-up resistors for buttons
   PORTD = 0;
 
   DDRD = _BV(6) | _BV(7);  // LED on PD6, PD7
-  DDRC = _BV(2);  // Latch pin, PC2    
-  DDRB = _BV(4) | _BV(5) | _BV(7);  // SS, SCK, MOSI are outputs 
+  DDRC = _BV(2);  // Latch pin, PC2
+  DDRB = _BV(4) | _BV(5) | _BV(7);  // SS, SCK, MOSI are outputs
   DDRA = 127;  // Outputs: PA0-PA5 ("rows") and Blank pin, PA6
 
   //ENABLE SPI, MASTER, CLOCK RATE fck/4:	
   digit_write(' ', '_');
   Latch();
 
-  SecNow = 0; 
+  SecNow = 0;
   MinNow = 0;
   MinNowTens = 0;
   MinNowOnes = 0;
   OptionMode = 0;
 
   LastAlarmCheckMin = 0;
-  HoldLoopCount = 0; 
+  HoldLoopCount = 0;
 
   EEReadSettings();   // Read stored settings from EEPROM.
 
 
 
   VCRmode = 1;  // Time is NOT yet set.
-  //FactoryResetDisable = 0;    
+  //FactoryResetDisable = 0;
 
   PINBLast =  PINB & buttonmask;
 
   HoldAlarmSet = 0;
   HoldOption = 0;
 
-  MomentaryOverridePlus = 0; 
+  MomentaryOverridePlus = 0;
   MomentaryOverrideMinus = 0;
 
 
-  SettingTime = 0;  // Normally 0, while not setting time.   
+  SettingTime = 0;  // Normally 0, while not setting time.
 
   SettingAlarm = 0;  // Same deal.
 
   PORTA |= _BV(6); // Blank LED driver
 
 #if 0
-  display_sequence("HELLO",1000); 
+  display_sequence("HELLO",1000);
 
-  while (  millis() < WordStopTime){    
+  while (  millis() < WordStopTime){
     display_refresh();
     DisplayWordMode = 0;
   }
 
   display_sequence("WORLD",2000);
 
-  while (  millis() < WordStopTime){    
+  while (  millis() < WordStopTime){
     display_refresh();
     DisplayWordMode = 0;
   }
   VCRmode = 1;  // Time is NOT yet set.
 
   Serial.begin(19200);
-  DateTime.sync(0); 
+  DateTime.sync(0);
 
   Wire.begin();
 
   CalculateNewTime();
   CalculateNewAlarm();
 
-  TurnAlarmOff(); 
+  TurnAlarmOff();
   snoozed = 0;
   noTone(13);	
   speaker_off();
 
 
 void loop()
-{ 
+{
   //byte HighLine, LowLine;
   byte PINBcopy;
   //byte alphaPosTemp;
   byte i,j;  // Dummy indices
 
 
-  millisCopy = millis(); 
+  millisCopy = millis();
 
   PINBcopy = PINB & buttonmask;
 
   if (PINBcopy != PINBLast)  // Button change detected
-  { 
+  {
 
     endIdleTime = millisCopy + IdleDelay; //  Idle time for EEPROM purposes is 5 seconds.
     IdleTimeEnded = 0;
     VCRmode = 0;  // End once any buttons have been pressed...
     //TimeSinceButton = 0;
 
-    if ((PINBcopy & 1) && ((PINBLast & 1) == 0)) 
+    if ((PINBcopy & 1) && ((PINBLast & 1) == 0))
     { //"Snooze" / Set Alarm Time Button was pressed and just released!
 
 
         else
           OptionMode = OptionsMax;
         OptionNameSequence = 0;
-        TurnAlarmOff(); 
+        TurnAlarmOff();
       }
       else if (SettingAlarm) {
         SettingAlarm = 0;  // End alarm time display/setting mode, when the "Snooze" button is released.
-      }  
+      }
       else if (AlarmNow)
-      {         
+      {
         snoozed = 1;
         TurnAlarmOff(); 	
-        display_sequence("SNOOZ",2500); 
+        display_sequence("SNOOZ",2500);
 
         AlarmTimeSnoozeHr = HrNow;
         AlarmTimeSnoozeMin  = MinNow + 9;    // Nine minutes, from time *snooze button pressed*
         }
         if (AlarmTimeSnoozeHr > 23)
           AlarmTimeSnoozeHr -= 24;
-      } 
+      }
 
 
     }
 
-    if ((PINBcopy & 2) && ((PINBLast & 2) == 0)) 
+    if ((PINBcopy & 2) && ((PINBLast & 2) == 0))
     { //"Time" / Alarm On/off Button was pressed and just released!
 
 
           OptionMode = 1;
 
         OptionNameSequence = 0;
-        TurnAlarmOff(); 
+        TurnAlarmOff();
       }
       else if ((AlarmNow) || (snoozed)){  // Just Turn Off Alarm
 
-        TurnAlarmOff();  
+        TurnAlarmOff();
         snoozed = 0;
 
         delay (100);
         display_sequence("ALARM",1000);
 
-        while (  millis() < WordStopTime){    
+        while (  millis() < WordStopTime){
           display_refresh();
           DisplayWordMode = 0;
         }
 
         display_sequence(" OFF ",1000);
 
-        while (  millis() < WordStopTime){    
+        while (  millis() < WordStopTime){
           display_refresh();
           DisplayWordMode = 0;
         }
 
         if  (ExtRTC) // Write the new time to the RTC, RIGHT NOW.
         {
-          RTCsetTime(HrNow,MinNow,SecNow);             
+          RTCsetTime(HrNow,MinNow,SecNow);
         }
 
 
-      } 
-      else { 
+      }
+      else {
         // Normal adjustment mode.
         if (AlarmEnabled)
           AlarmEnabled = 0;
-        else  
-          AlarmEnabled = 1;    
+        else
+          AlarmEnabled = 1;
 
 
-      }  
+      }
     }
 
     if ((PINBcopy & 4) && ((PINBLast & 4) == 0))  //"+" Button was just released!
-    { 
+    {
       if ( MomentaryOverridePlus)
-      {  
-        MomentaryOverridePlus = 0; 
+      {
+        MomentaryOverridePlus = 0;
         // Ignore this transition if it was part of a hold sequence.
       }
       else
           else if (OptionMode == 2) {
             if (NightLightType < 2)
               NightLightType++;
-            else 
+            else
               NightLightType = 0;
 
             updateNightLight();
           else if (OptionMode == 3) {
             if (AlarmTone < 3)
               AlarmTone++;
-            else 
+            else
               AlarmTone = 0;
 
           }
           else if (OptionMode == 4) {
             // ACTIVATE Alarm test mode!   Momentarily turn the alarm on.
-            SoundSequence = 0; 
+            SoundSequence = 0;
             AlarmNow = 1;
           }
 
         }
         else if ((PINBcopy & 1) == 0)   {  // Alarm-setting button is currently depressed
 
-          SettingAlarm = 1;  // Individual step mode 
-          AlarmTimeMin++;       // Advance the Alarm time!   
+          SettingAlarm = 1;  // Individual step mode
+          AlarmTimeMin++;       // Advance the Alarm time!
 
           CalculateNewAlarm();
 
-        } 
-        else {  
+        }
+        else {
           // Brightness control mode
           if (MainBright < 13)
-            MainBright++; 
-        } 
+            MainBright++;
+        }
       }
     }
 
     { //"-" Button was pressed and just released!
 
       if ( MomentaryOverrideMinus)
-      {  
-        MomentaryOverrideMinus = 0; 
+      {
+        MomentaryOverrideMinus = 0;
         // Ignore this transition if it was part of a hold sequence.
       }
       else
           else if (OptionMode == 2) {
             if (NightLightType > 0)
               NightLightType--;
-            else 
+            else
               NightLightType = 2;
             updateNightLight();
           }
           else if (OptionMode == 3) {
             if (AlarmTone > 0)
               AlarmTone--;
-            else 
+            else
               AlarmTone = 3;
           }
           else if (OptionMode == 4) {
             // Alarm test mode!
-            TurnAlarmOff(); 
+            TurnAlarmOff();
           }
         }
         else if ((PINBcopy & 2) == 0)   {  // Time-setting button is currently depressed
 
-          SettingTime = 1;  // Declare that we are in individual step mode 
+          SettingTime = 1;  // Declare that we are in individual step mode
 
-          if (MinNow > 0) 
+          if (MinNow > 0)
             MinNow--;       // Decrement the time!
-          else 
+          else
           {
             MinNow = 59;
             if (HrNow > 0)
         }
         else if ((PINBcopy & 1) == 0)   {  // Alarm-setting button is currently depressed
 
-          SettingAlarm = 2;  // Individual step mode  
+          SettingAlarm = 2;  // Individual step mode
 
-          if (AlarmTimeMin > 0) 
+          if (AlarmTimeMin > 0)
             AlarmTimeMin--;       // Decrement the alarm time!
-          else 
+          else
           {
             AlarmTimeMin = 59;
             if (AlarmTimeHr > 0)
 
           CalculateNewAlarm();
 
-        } 
+        }
         else {  //Normal brightness adjustment mode
           if (MainBright > 0)
-            MainBright--; 
-        }   
-      } 
+            MainBright--;
+        }
+      }
     }
   }
 
 
-  PINBLast = PINBcopy; 
+  PINBLast = PINBcopy;
 
   // The next if statement detects and deals with the millis() rollover.
-  // This introduces an error of up to  1 s, about every 50 days.  
+  // This introduces an error of up to  1 s, about every 50 days.
   //
   // (If you have the standard quartz timebase, this will not dominate the inaccuracy.
   // If you have the optional RTC, this error will be corrected next time we read the
   // time from the RTC.)
 
-  if (millisCopy < LastTime) { 
+  if (millisCopy < LastTime) {
     LastTime = 0;
     NextAdvance = 0;      // Other variables that could cause issues if millis rolls over!
     WordStopTime = 0;    // Other variables that could cause issues if millis rolls over!
 
   if ((millisCopy - LastTime) >= 1000)
   {
-    LastTime += 1000;    
+    LastTime += 1000;
 
 
     // Check to see if any buttons are being held down:
       if (millisCopy  > endIdleTime)
       {
         if (IdleTimeEnded == 0){
-          EESaveSettings(); 
+          EESaveSettings();
           IdleTimeEnded = 1;
         }
       }
 
       // Note which buttons are being held down
 
-      if ((( PINB & buttonmask) == 10) ||(( PINB & buttonmask) == 6))   // Alarm-time set is down 
-      { // Alarm button is down, and so is EITHER + or -. 
+      if ((( PINB & buttonmask) == 10) ||(( PINB & buttonmask) == 6))   // Alarm-time set is down
+      { // Alarm button is down, and so is EITHER + or -.
 
-        HoldAlarmSet++;   
+        HoldAlarmSet++;
         HoldOption = 0;
         HoldTimeSet = 0;
         HoldLEDTest = 0;
       }
 
-      if ((( PINB & buttonmask) == 9) ||(( PINB & buttonmask) == 5)) //Time-set is pressed down. 
-      { // Time button is down, and so is EITHER + or -. 
+      if ((( PINB & buttonmask) == 9) ||(( PINB & buttonmask) == 5)) //Time-set is pressed down.
+      { // Time button is down, and so is EITHER + or -.
 
-        HoldTimeSet++;       
+        HoldTimeSet++;
         HoldOption = 0;
         HoldAlarmSet = 0;
         HoldLEDTest = 0;
         HoldTimeSet = 0;
         HoldAlarmSet = 0;
         HoldLEDTest = 0;
-      }     
+      }
 
       if (( PINB & buttonmask) == 12)  // "time" and "alarm" are down.
       {
         HoldOption = 0;
 
 
-      }     
+      }
 
-    } 
+    }
 
     if (HoldAlarmSet > 1)
-    { 
+    {
       MomentaryOverridePlus = 1;       // Override momentary-action of switches
       MomentaryOverrideMinus = 1;      // since we've detected a hold-down condition.
       //MomentaryOverrideSetAlarm = 1;
 
       /*  // TODO: Add factory restore option
        // Hold + and - for 3 s AT POWER ON to restore factory settings.
-       if ( FactoryResetDisable == 0){ 
+       if ( FactoryResetDisable == 0){
        ApplyDefaults();
        EESaveSettings();
        led_all_off();        // Blink LEDs off to indicate restoring data
        else
        {
        if (AlignMode) {
-       AlignMode = 0; 
-       } 
+       AlignMode = 0;
+       }
        else {
        }
        }
     }
 
     if (HoldLEDTest == 4)  //"time" and "alarm" Held down, together, for 4 seconds
-    {   
+    {
       if (LEDTestMode)
         LEDTestMode = 0;
       else{
 
 
     if (HoldOption == 2)  // + and - Held down, together, for 2 seconds
-    {   
+    {
 
       MomentaryOverridePlus = 1;
       MomentaryOverrideMinus = 1;      // since we've detected a hold-down condition.
 
       if (OptionMode) {
-        OptionMode = 0;  
-      } 
+        OptionMode = 0;
+      }
       else {
         OptionMode = 1;  // On *and* set position in menu.
       }
     }
 
     if (HoldTimeSet > 1)
-    { 
+    {
       MomentaryOverridePlus = 1;       // Override momentary-action of switches
       MomentaryOverrideMinus = 1;      // since we've detected a hold-down condition.
       // MomentaryOverrideSetTime = 1;
         HoldTimeSet = 5;
     }
 
-    // Note: this section could act funny if you hold the buttons for 256 or more seconds.  
+    // Note: this section could act funny if you hold the buttons for 256 or more seconds.
     // So... um... don't do that.  :P
 
     SecNow++;       // Advance the time!
     CalculateNewTime();
-  }   
+  }
 
 
   if (( PINB & buttonmask) == buttonmask) {
       if (( PINB & buttonmask) == 10)    // Alarm +
       {
         if (HoldAlarmSet > 1)
-        {      
-          AlarmTimeMin++;       // Advance the Alarm time!   
+        {
+          AlarmTimeMin++;       // Advance the Alarm time!
           CalculateNewAlarm();
         }
         NextAdvance = millisCopy + (501 - 100 * (unsigned long) HoldAlarmSet);
       if (( PINB & buttonmask) == 6)   // Alarm -
       {
         if (HoldAlarmSet > 1)
-        { 
-          if (AlarmTimeMin > 0) 
+        {
+          if (AlarmTimeMin > 0)
             AlarmTimeMin--;       // Decrement the alarm time!
-          else 
+          else
           {
             AlarmTimeMin = 59;
             if (AlarmTimeHr > 0)
 
           CalculateNewAlarm();
         }
-        NextAdvance = millisCopy + (501 - 100 * (unsigned long) HoldAlarmSet);          
+        NextAdvance = millisCopy + (501 - 100 * (unsigned long) HoldAlarmSet);
 
-      }      
-      if (( PINB & buttonmask) == 9)   // Time + 
+      }
+      if (( PINB & buttonmask) == 9)   // Time +
       {
 
         if (HoldTimeSet > 1)
-        { 
+        {
           MinNow++;       // Advance the time!
           SettingTime = 1;  // Flag that time is changing, so that we don't reset it by RTC.idl
           CalculateNewTime();
-        } 
-        NextAdvance = millisCopy + (501 - 100 * (unsigned long)  HoldTimeSet);    
+        }
+        NextAdvance = millisCopy + (501 - 100 * (unsigned long)  HoldTimeSet);
       }
       if (( PINB & buttonmask) == 5) // Time -
-      { 
+      {
         if (HoldTimeSet > 1)
-        { 
+        {
           SettingTime = 1;  // Flag that time is changing, so that we don't reset it by RTC.
-          if (MinNow > 0) 
+          if (MinNow > 0)
             MinNow--;       // Decrement the time!
-          else 
+          else
           {
             MinNow = 59;
             if (HrNow > 0)
           }
           CalculateNewTime();
         }
-        NextAdvance = millisCopy + (501 - 100 * (unsigned long) HoldTimeSet);       
+        NextAdvance = millisCopy + (501 - 100 * (unsigned long) HoldTimeSet);
       }
     }
   }
 
 
   if (AlarmNow )        // Visual display and sounds durign ALARM sequences
-  { 
+  {
     if (OptionMode == 0)
-    {    // If we are testing the alarm sound in option mode, 
+    {    // If we are testing the alarm sound in option mode,
       // do not change the display.
       if (SecNow & 1) {
 
         display_sequence("ALARM",100);
-        //DisplayWordMode = 1; 
+        //DisplayWordMode = 1;
       }
     }
 
           SoundSequence++;
         }
         else if (SoundSequence == 200)
-        { 
+        {
           tone(13, 20 + 5 * SoundSequence, 2000);
           NextAdvanceSound = millisCopy + 1500;
           SoundSequence++;
-        } 
+        }
         else {
         NextAdvanceSound = millisCopy + 1000;
          SoundSequence = 0;
           else
           {
             NextAdvanceSound = millisCopy + 200;
-            SoundSequence++;  
-            noTone(13);  
+            SoundSequence++;
+            noTone(13);
 	    speaker_off();
           }
         }
         else
-        { 
+        {
           NextAdvanceSound = millisCopy + 1000;
           SoundSequence = 0;
-          noTone(13);  
+          noTone(13);
 	  speaker_off();
         }
 
           else
           {
             NextAdvanceSound = millisCopy + 200;
-            SoundSequence++;  
-            noTone(13);  
+            SoundSequence++;
+            noTone(13);
 	    speaker_off();
           }
 
         }
         else
-        { 
+        {
           NextAdvanceSound = millisCopy + 1400;
           SoundSequence = 0;
           noTone(13);
       }
       else  if (AlarmTone == 0) // High Tone
       {
-        if (SoundSequence < 6) 
+        if (SoundSequence < 6)
         {
           if (SoundSequence & 1)
           {
           else
           {
             NextAdvanceSound = millisCopy + 200;
-            SoundSequence++;  
-            noTone(13);  
+            SoundSequence++;
+            noTone(13);
 	    speaker_off();
           }
 
         }
         else
-        { 
+        {
           NextAdvanceSound = millisCopy + 1000;
-          SoundSequence = 0; 
+          SoundSequence = 0;
           noTone(13);
 	  speaker_off();
         }
       // Load buffers
       j = 0;
       while (j < 5)
-      { 
+      {
         if (j == cpos)
         {
           bufl1[j] = atemp;
           bufh1[j] = btemp;
-          bufh2[j] = ctemp;   
+          bufh2[j] = ctemp;
         }
         else
         {
           bufl1[j] = 0;
           bufh1[j] = 0;
-          bufh2[j] = 0;   
+          bufh2[j] = 0;
         }
         j++;
-      } 
+      }
 
       WordStopTime = millisCopy + 1000; // 100 ms per segment
       DisplayWordMode = 1;
 
   else if (OptionMode){
 
-    if (OptionMode == 1) {  // AM-PM / 24 HR   
+    if (OptionMode == 1) {  // AM-PM / 24 HR
 
-      if (HourMode24) 
-        display_string("24 HR"); 
+      if (HourMode24)
+        display_string("24 HR");
       else
         display_string("AM/PM");
     }
     {
       if (OptionNameSequence == 0)
       {
-        display_sequence("NIGHT",700);      
+        display_sequence("NIGHT",700);
       }
       else if (OptionNameSequence == 1)
         display_sequence("     ",100);
     {
       if (OptionNameSequence == 0)
       {
-        display_sequence("ALARM",700); 
+        display_sequence("ALARM",700);
       }
       else if (OptionNameSequence == 1)
         display_sequence("     ",100);
         display_sequence("     ",100);
       else if (OptionNameSequence == 2)
       {
-        display_sequence("SOUND",700); 
+        display_sequence("SOUND",700);
       }
       else if (OptionNameSequence == 3)
         display_sequence("     ",100);
   else {
 
     if ((PINBcopy & 1) == 0){
-      // Display Alarm time whenever "Alarm/Snooze" button is pressed. 
+      // Display Alarm time whenever "Alarm/Snooze" button is pressed.
       display_char(4, AMPM24HdisplayAlrm);
       display_char(3, MinAlrmOnes + '0');
       display_char(2, MinAlrmTens + '0');
         display_char(0,' ');
     }
     else {
-      // "Normal" time display: 
+      // "Normal" time display:
       //display[0] = AMPM24HdisplayNow;
-      //display[0] = (SecNow % 8) + 1 + 8  + '`'; // chaser     
+      //display[0] = (SecNow % 8) + 1 + 8  + '`'; // chaser
       display_char(3, MinNowOnes + '0');
       display_char(2, MinNowTens + '0');
       display_char(1, HrNowOnes + '0');
   }
 
   // Time (or word) to display is now computed.
-  // Now is the place in the loop when we switch gears, and 
+  // Now is the place in the loop when we switch gears, and
   // actually light up the LEDs. :)
 
   display_refresh();
       HrNow = DateTime.Hour;
 
       //    if ( HrNow > 11)   // Convert 24-hour mode to 12-hour mode
-      //    HrNow -= 12;     
+      //    HrNow -= 12;
 
       // Print confirmation
       Serial.print("Alpha Clock Five: Clock sync at: ");
 
       if ( prevtime != DateTime.now() )
       {
-        if (ExtRTC) 
+        if (ExtRTC)
           RTCsetTime(HrNow,MinNow,SecNow);
 
         DateTime.available(); //refresh the Date and time properties
         digitalClockDisplay( );   // update digital clock
-        prevtime = DateTime.now(); 
+        prevtime = DateTime.now();
       }
     }
-  } 
+  }
 
 }