Step 7: The code is the glue

Picture of The code is the glue
I have included the code which hopefully read with the following notes should make it self explanatory.

I used the latest Arduino IDE 1.0 which I must say has been brilliant especially as you can now programme using the ISP programmer and not have to worry about USB dongles or circuitry.  One thing I would say you still need to burn the boot loader onto your blank ATmega328 chip otherwise debugging becomes a bit of a challenge as the Arduino environment hasn't been set up properly!!!

The code I have compiled is 8,346 bytes in size so you can use the ATmega168 chip if you have a few kicking around.


Arduino.h - the standard and very large library
DS1302.h - Real time clock library
LCD4Bit_mod.h - Library supplied to support the LCD/Keyboard - very similar to the Standard library
OneWire.h - Provides communication to the temperature probe

Pinout Allocation and Global Variables:

This is where you need to set your own sunset and sunrise depending on your location.  We have summer savings here in the UK but I decided to keep my system on GMT which means when I look at the clock in the summer saving time period it is an hour behind so the sunset and sunrise times need to be set accordingly.

  1. Sets the pinouts for the motor
  2. Sets the display back light which is switched off on reset.  
  3. The current MotorState is set to STOP
  4. The top line of the display is initialised with the status headings.  
  5. At this point we check the temperature to initialise the min/min max settings and display it. 

In the code you will notice a commented out routine 'set_time()' to set the RTC.  This should be uncommented for the first time programming so the clock can be set.  Comment it out and reprogramme the ATmega328 again this way if the board is reset or further changes to the code are made the RTC always has the correct time.

You need to experiment  on setting the RTC in sync with the laptop time.  I discovered on a Mac environment you need to allow 40 seconds for compiling and uploading to make sure the times where exactly synced.

  1. Print the current time
  2. Pause for 0.5 second so the display can be read.
  3. Print the bedtime which is in total minutes with the '^' symbol in front of it.
  4. Print the minimum temperature with the 'v' symbol in front of it.
  5. Pause for 0.5 second so the display can be read.
  6. Print the wake time which is in total minutes with the 'v' symbol in front of it.
  7. Print the maximum temperature with the '^' symbol in front of it.
  8. Pause for 0.5 second so the display can be read.
  9. Print the current time
  10. Check the door activation button (display on/off, close door or open door manually)
  11. Change the Motor State according to activation button
  12. Check to see if it is wake time if so set Motor State to DoorUp
  13. Check to see if it is bed time if so set Motor State to DoorDown
  14. While the State Motor isn't 'Stop' call door_change to either close the door or open it and display status on screen
I would suggest you familiarise yourself with the code and hopefully you can understand it enough to make your own modifications, i.e. no temp probe but a LDR instead.

As you can see from the code I have created a state motor variable which ensures actions are completed before the main loop code goes back on itself.