Page 105 - Programming the Raspberry Pi Getting Started with Python
P. 105
The first thing to notice is that because we need access to GPIO pin 17 to see whether the button is
pressed, we need to use the RPi.GPIO library. We used this as an example of installing a module back
in Chapter 5. Therefore, if you have not installed RPi.GPIO, refer back to Chapter 5 and do so now.
We set the switch pin to be an input using the following command:
This command also turns on an internal pull-up resistor that ensures the input is always at 3.3V
(high) unless the switch is pressed to override it and pull it low.
Most of what was in the loop has been separated into a function called display_time. Also, two
new functions have been added: display_seconds and display_date. These are fairly self-
explanatory.
One point of interest is that display_date displays the date in U.S. format. If you want to change
this to the international format, where the day of the month comes before the month, change the line
that starts with disp.print_int appropriately (refer to the comments in the code).
To keep track of which mode we are in, we have added some new variables in the following lines:
The first of these lines gives each of the three variables a different number. The second line sets the
disp_mode variable to the value of time_mode, which we use later in the main loop.
The main loop has been changed to determine whether the button is pressed. If it is, then 1 is added