Light and Time Switcher on Arduino (ATMega168 and up) Version 0.94 The LTS controls a relay based on both time and light. The relay is switched on if both the time is later than the morning time stamp and the light level is above the on-light level. The relay is switched off if both the time is later than the evening time stamp or the light level is below the off-light level. There is currently no provision for the relay to be active during the night period, but renaming some variables and inverting the relay actions might do the trick. Two LEDs indicate the time and light level conditions are met. The 'time' LED is on when the current time is between the morning switch time and the evening switch time. The 'level' LED is on if the light level has been higher than the 'on level' but since then has not dropped below the 'off level'. If both LEDs are on, the relay is switched on. If both LEDs are off, the relay is switched off. Control of the application is currently via the virtual serial port of the Arduino board. Commands can be terminated a Carriage Return, LineFeed or dot. So you can use the Arduino Serial Monitor for this application. The interface recognizes these commands: D or Dyyyymmdd get or set date E or Ehhmmss get or set evening switch time H or ? short usage I or Innn get or set the update interval, or the frequency of measurements L or Lnnn get or set off LDR switch level M or Mhhmmss get or set morning switch time R copies the parameters from EEPROM to the RTC RAM (for transition from versions 0.92 and before). T or Thhmmss get or set time U or Unnn get or set on LDR switch level V prints the current version Very little checking is done on the arguments. If the size is right, it is accepted and processed. Garbage in is garbage stored. All 'hhmmss' arguments should be proper 24-hour time stamps, and the 'nnn' arguments should be numeric. Entering just the first character will echo the current setting. All these values are stored, time and date in the RTC, parameters in RTC RAM. To simplify calculations and save some storage space, the parameter values are stored in reduced accuracy. The light levels have only eight bit accuracy, loosing two bits. The time stamp have 16 bit accuracy, loosing one bit. Typical debug line: 2011-02-05 13:36:00 48960 25626 61200 842 200 188 20 on | | | | | | | | | | | | | | | | | | | current relay position | | | | | | | | updateInterval | | | | | | | off LDR switch level | | | | | | on LDR switch level | | | | | current LDR level | | | | evening switch time | | | morning switch time | | current time in seconds | current time current date The 'current date' is only for decoration, the timer does not use it. If your time is way off, you can deduce wether the RTC is too slow or too fast. The 'current time' and 'current time in seconds' are two representations of the same value. There is no provision for daylight saving time. The main users of the LTS, my tortoises, do not use it. Business rules: When the 'morning switch time' is in the past, one of the conditions for switching on the relay is met. This value is in seconds, the 'M' command returns the morning switch time in HH:MM:SS format. When the 'evening switch time' is in the past, one of the conditions for switching off the relay is met. This value is in seconds, the 'E' command returns the evening switch time in HH:MM:SS format. The 'current LDR level' is the light level measured by the LDR. When the 'on LDR switch level' is lower than the 'current LDR level', one of the conditions for switching on the relay is met. When the 'off LDR switch level' is higher than the 'current LDR level', one of the conditions for switching off the relay is met. The 'updateInterval' is the time between measurements. Short intervals can result in the clock running slow as each update results in several RTC accesses. The interval is derived from the RTC one second square wave by polling. Known problems: - If the DS1307 chip is not properly connected, the firmware hangs. Fix on progress.