Sensing a key and controlling an LED is an important but not so simple task for an AVR beginner. The algorithm to do this is:
- Sense the key continuously.
- If the key is pressed, i/o pin is LOW. If the key is not pressed, the corresponding i/o pin is HIGH.
- If the pin is LOW, wait for 20 mS to neglect the bouncing of the key. This is known as de-bouncing delay.
- Toggle the LED state by XORing it with the LED bit.
- Wait till the key is released.
- Go to step number 1 again.
Here is the keyled file for download.