Skip to main content

Subsection 2.2.4 Part 4: Button Exercises

In the rest of the hour we’ll put the button to work on the programs from Lab 1. These exercises are part of your Lab 2 work. You will not finish them in class, but they are a good head start.

Activity 2.2.6. Button Exercises.

(a)

Modify blinkyCNT.c so that pressing the button pauses the counter and releasing resumes it.

(b)

Homework (due Thursday): Write toggleLED.c so the on-board LED toggles each time the button is pressed and released: first press turns LED on, second press turns it off, and so on. You will likely observe erratic behavior (“glitches”). Bring your observations to class tomorrow.
The link below opens the simulator with button.c as you ran it in class, so you can start the toggle homework tonight whether or not your Nucleo is in front of you. Attach the button first: click + Button in the component bay, then click the Arduino header pin D5. Step walks the program one line at a time, and the register panel shows PUPDR and IDR, so you can watch the pin read 1 released and 0 pressed. One warning: the simulated button makes clean contact, so the glitches you are asked to observe are only partly reproduced here. You will see the LED flip on every pass of the loop while the button is held, which is the larger of the two effects, but contact bounce needs the real switch. The simulator does not save your code, so use the download button (the downward arrow) to keep a copy of your work.