Button Challenge!


Read the section titled More Advanced Decision Making. Try to get it to switch brightness reliably. Don't forget to add the delay to the end of the program.

The two button light switch in part 2 of this exercise worked much better than this 1 button dimmer. I can almost imagine someone inventing a lamp with a button for each brightness, like a blender has to select speeds. But you know from experience that a single button can be used to reliably switch between different modes. See if you can do it better. The following challenges explore some principles of better button reading.


Make a sketch that uses your buttons to do interesting things. Some examples:

Intermediate - Prevent Cycling

If you press and hold the button in the sample program, the brightness cycles. Neither HIGH not LOW is really the right time to turn the light on or off if you only want one change per push. Instead you want to pay attention to whether the button is changing state — being pressed or being released — and pick one of those to change the light. To do this remember the button's previous state as well as its current state. Then you can determine four different button events:

Refer to File | Examples | Digital | StateChangeDetection to see how this might be done to simply turn a light on and off.


6. References