Toggle Pushbutton Using Debounce

Look at the code in an editor. Compare button.c and toggle.c. Why is there a debounce mechanism in toggle.c but not in button.c? How does the debounce mechanism work?

What happens if you change the debounce threshold to 1? (hint - try toggling the LED 10 times)

What happens if you change the debounce threshold to 30000?

Can you change it to 100000? (hint - you need to change something else, too. DO NOT SPEND MORE THAN 5 MINUTES TRYING TO FIGURE THIS OUT!)

Optional Section: Instruction Counting and Timing

Really, we mean it, don't waste time on this unless it fascinates you

In the BUTTON project directory, create the file button.ss by typing make button.ss

Look at button.ss - these are the instructions that are actually written to the microprocessor.

Look in the two sections <checkButton> and <main>. Each line corresponds to one instruction of the microprocessor. What debounce threshold value would give approximately 1 second of delay between the button press and the LED lighting up? (Hint: you will need to look at the Atmel Instruction Set documentation.)

Why is using a very high debounce threshold a bad way to implement a 1 second delay? (There are at least three good reasons.)

The best way to implement a 1 second delay is with the timer library in avrlib. Put the debounce threshold back to a reasonable value for debouncing and reintroduce the 1 second delay properly:

Add #include "timer.h" at the top of toggle.c

Add timerInit(); to the beginning of your main() function but after your varaible declariations..

Add a call to the timerPause() function in the right place in your program.

In your makefile, add $(AVRLIB)/timer.c to the list of files in the definition of SRC

The new makefile line should look like this

SRC= $(TRG).c $(AVRLIB)/timer.c

Find the maximum delay time before you can notice a pause between the button press and the LED illuminating.

Four buttons

Write a program to control 4 LEDs with 4 buttons. It's up to you to do some "interaction design": think of your own way for actions with the four buttons to control the states of the LEDs. Feel free to write this program from scratch, but we recommend that you start with the FOURTOGGLE demo.

Beep

Load BUTTONBEEP as you did BUTTON and FLASH above.

Make sure the blue wire from your speaker is connected to pin D5 and the black wire from the speaker to ground.

You should hear a beep when you press SW4.

Try inserting a pause between turning on the LED and playing the beep. In case you didn't do part 3b, here are the step-by-step instructions for using the avrlib's timer facilities:

Add #include "timer.h" at the top of buttonbeep.c

Add timerInit(); to the beginning of your main() function.

Add a call to timerPause() in the right place in your program.

In your makefile, add $(AVRLIB)/timer.c to the list of files in the definition of SRC

At what point does the delay become noticeable? What if the beep comes before the light?

Try finding the largest unnoticeable delay time as in #3 using the beep as feedback. Is it different?