Light and Time Switcher on Arduino Diecimila (ATMega168 and up)

Version 0.92

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 swiched off if either 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 CarriageReturn, LineFeed or dot. So you can use the Arduino Serial Monitor to control 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 ? prints DEHILMTU?
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 morming switch time
T or Thhmmss get or set time
U or Unnn get or set on LDR switch level

Very little checking is done on the arguments. If the argument 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. Note that the D and T command will print the time and date at the last update interval. Setting the date and time will reset the update interval, so the new values are printed.

All these values are stored, time and date in the RTC, others in the EEPROM. To simplify calculations and save some storage space, the EEPROM values are stored in reduced accuracy. The light levels have only eight bit accuracy, loosing two bits. The time stamps have 16 bit accuracy, loosing one bit.

At each update interval, a line of debug data is printed to the serial port. This line contains all information on which the program decides to switch the reley. A typical debug line would look like:

2011-02-05 13:36:00 48960 25626 61200 842 200 188 20
|          |        |     |     |     |   |   |   |
|          |        |     |     |     |   |   |   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. 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.

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 measurement and update. Short intervals can result in the clock running slow.


Last updated: 2011-02-06

e-mail