View project on Hackaday View project on GitHub

Hack Clock

A hackable alarm clock, built for experimentation and learning

Lesson One: Change the Time

We've wired up the display and so far all we see is... "0:0." Exciting.

What we want to see is the current time. Open up the Hack Clock IDE again, and take a look at the code we loaded for Lesson One. For right now we can ignore the first few lines that say "import" at the top, and skip down to where we "Connect to the internal machine clock."

Notice that there are a few lines in this code that start with the "#" symbol. This means that the line is ignored by the computer, and is referred to as a "comment." Comments are for humans like you and I to read so we can better communicate what the code is doing, but the machines that read our code will completely ignore them. The comment says we are "connecting to the internal machine clock," but the actual action of connecting happens on the line directly underneath.

The line underneath says clock = Clock() and is the first machine-readable line of code we care about. This one line actually has two parts: a variable and a constructor. If we read this line from right to left, the "Clock()" phrase constructs a new object. Here we're telling our machine to "build" a new Clock object that will keep time for us and let us know when the seconds go by. The "=" sign assigns this newly constructed object to a variable. Our variable is named "clock," and it allows us to store and keep our newly created objects. If we were reading this out loud, we might say "the variable 'clock' is a newly constructed Clock object."

The following line does a similar thing, except instead of creating a new Clock we are creating a new Display object. This new object will be responsible for updating the digits on our bright LED display and letting people see what time it is.

Skip ahead and look at the rest of the code. Note that the rest of our file has lines that begin with "display.something()." When you see a line begin with "display." it means that we are using the display object to call a method. Methods are actions taken on an objects. When we type display.setColon(True) this is telling the machine "using the display object, set the colon lights to 'on.'" This should light up the colon indicator in the middle of the LED display.

Reading from top to bottom, we can see that we are telling the display to turn on the colon lights, turn off the evening indicator ("set(False)" means turn it off), set the hours digits to "0," set the minutes digits to "0" and set the brightness level to "1."

Now let's change the minutes and hours digits that are showing on the display. Change the hours to "12": display.setHours(12) and set the minutes to "33": display.setMinutes(33) After that is changed in your code, click on the save button in the top-right corner.

Once you save your changes, the clock should re-start itself. The display will blink, and you should see it now display "12:33." Congratulations! You can now make the display show whatever you wish!

Next up - try adjusting the brightness. Set it to the highest value of 15, and the lowest value of 0. Set it for a few values in between. What level of brightness do you like most?

Next up - Show the Time Like a Real Clock