Counting Presses

A pretty useful technique you'll want to add to your collection of sketch-knowledge is how to keep track of button presses. Try this sketch:

Notice, we've added the ++ operator. Simply, the statement "buttonPresses++" increments (adds 1 to) the buttonPresses variable. This is a shortcut for "buttonPresses = buttonPresses + 1". We first saw this in the For Lesson from Chapter 5.

In this sketch there is no new code to explain. you should recognize several things from previous lessons that ave been put together in new ways. Now it's time for you to extend on some previous ideas.

Time to Play

    • Modify this sketch so that it increments the variable when you let go of the button rather than press it.
    • Change this sketch so that it counts down rather than up. When you get to zero you should reset the variable so you can start over.
    • Attach 5 LEDs to your Arduino. At the beginning of the program they should all be turned on. Each time I push the button, one should go out.

The text and code examples above were copied and slightly modified from Arduino tutorials by Limor Fried at under as such, this page is subject to the same license.