Lab 2: Button Challenge!

Challenge (Bonus Mark)

Read the section titled More Advanced Decision Making, and try to get it work. 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 probably 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 hints explain some principles of better button reading.


Make a sketch that uses a button or buttons to do three or more different interesting things. Be aware that if your interesting things take time because they use the delay() command, the button may seem unresponsive while the thing is happening.

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