How to Add a Switch and Input Buttons to Your Arduino Clock Project - dummies

How to Add a Switch and Input Buttons to Your Arduino Clock Project

By Brock Craft

For your Arduino Clock Project, you first add one of the pushbutton switches and one of the resistors, which you use as an input to increment the alarm hours. The pushbutton works by allowing +5V to be applied to Pin 7 when it’s pressed.

When current flows through the switch, it encounters the resistor, and instead takes the easier pathway to ground through Pin 7 (there are connections to ground internally, within the ATmega328 microcontroller IC). Reading Pin 7 with the digitalRead() function returns a value of HIGH (+5V).


When it’s not pressed, PIN 7 is connected through the resistor to GND, via the ground rail. Reading PIN 7 with the digitalRead() function returns a value of LOW (0V).

Now add the second pushbutton switch and resistor, which increments the minutes. It should be connected to digital Pin 8.

Finally, add the slide switch, which has three positions, and two resistors. You can see that the electrical connections for the slide switch are very similar to the two pushbutton switches. But because it’s a slider, it stays in the position that you put it in.

In the left position, it keeps +5V on digital Pin 10. In the right position, it keeps +5V on digital Pin 10. In the center, it makes no connection, and both of the digital pins are connected only to the ground rail.

Reading these pins, you can determine whether the clock is Alarm Set Mode (Pin 6 is HIGH), Alarm Armed Mode (Pin 10 is HIGH), or simply in Display Time mode (no connection; Pin 6 and Pin 10 are LOW).


Now add the code necessary to read and respond to the hardware. You need a few variables to store the system state and keep track of when to trigger the alarm. In the variable declaration section, add the following code:

DateTime now;
boolean displayAlarmSet = false; // Whether we are in the show time mode or show alarm set mode
boolean alarm = false; // Whether the alarm is currently happening
boolean armed = false; // Whether the alarm is armed or not
int alarmHrs = 12; // You can set the alarm time in code, here
int alarmMins = 00;

The variable displayAlarmSet allows you to switch between showing the clock time or showing the alarm time, that you use in the updateDisplay() function, a bit later. You can use the slide switch to change the value of this variable.

The “alarm” variable keeps track of whether or not the alarm is currently happening, so that you can sound the piezoelectric sounder, if necessary.

You also need to keep track of when to trigger the alarm. This is done with two integers, alarmHrs and alarmMins. If you want to set the alarm time from within the software and not from the buttons, you can set that by changing the starting value of these variables.

Counting the two pushbuttons, and the two states that can be set by the slide switch, you have four inputs. So, you need four digital pins to read them. You use them for input, so they need to be enabled in the setup() part of your code. You’ll also use the piezo sounder for output, later, but it can be added now, so add the following code:

// User input to set alarm time
const int alarmSetPin=6;   // Used to change to alarm set mode
const int incrementAlarmHrsPin=7; // Used to increment the alarm hours in alarm  set mode
const int incrementAlarmMinsPin=8; // Used to increment the alarm minutes in alarm set mode
const int piezoPin = 9;   // Used for the piezoelectric sounder
const int alarmArmedPin=10;  // Use to enable the alarm to go off
 // Set several pins for input and output
 pinMode(alarmSetPin, INPUT);
 pinMode(incrementAlarmHrsPin, INPUT);
 pinMode(incrementAlarmMinsPin, INPUT);
 pinMode(alarmArmedPin, INPUT);
 pinMode(piezoPin, OUTPUT);

Note, the ellipses (…) indicate the code you added earlier — no need to repeat that here. There are five new integers, which handle the input pins. These don’t change throughout the program so they are integer constants.

You connected the slide switch to Pin 6 and Pin 10 to handle either setting the alarm or arming it to go off. You connected the pushbutton switches to Pin 7 and Pin 8. Each one separately controls incrementing the hours and minutes.

You can only increment upward. Otherwise, you either need additional buttons to decrement hours and minutes or a way to switch between incrementing and decrementing, which would be unnecessarily complicated. Finally, these pins are all used for input, so they have to be explicitly set to be used for INPUT with the pinMode() function.