NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Responding to a Button with Polling

June 18, 2011
by wvtsa
wvtsa's Avatar

I read how to connect the button but I'm not sure how to tell the microcontroller where to look. I just started with this this week and I need to know for next week, so I e-mailed this as well.

I need to know how to poll a given microcontroller port other than port 23 on which I have the LED, preferably between 13 and 22 or 24 (yes, the right side), to see if it is 1 or 0. I need to know a) how to initialize, say, port 20 (or any other not used by the LED on 23 or by the LCD I'm also connecting, or the programmable header) b) how to check the digital voltage on that port

First attempt at writing this (may contain extra info, but a little bit more tired): I've got everything else down on it for my project, but I'm not sure how to poll the button. I know that the traffic light is supposed to be DIY, but I'm not doing that now, and anyways, doesn't that interrupt, not poll? Do you have to listen for interrupt or can you poll, and if you are polling, how do you set up / initialize the button (on somewhere other than where you put the LED for LED Blink since that is where mine is) and what code do you use to tell if the button is pressed at any given time?

Sorry to e-mail and then post.

June 18, 2011
by Rick_S
Rick_S's Avatar

1st, you need to read your guide and do the projects. If you do that, you'll understand much more of what you are doing ... including how to connect a switch. I don't know why you have the 1 week deadline, but if you put your mind to it, you can go through the guide in a couple of days with the sample projects.

2nd, the pin number of the IC, is NOT a port. The ATMEGA168, has 3 ports PORTB, PORTC, and PORTD. Each of those are located at different pins of the IC. For instance Pin 23 where you said your LED is located is actually PC0.

If you look at the second page of the datasheet, it will show you all the different port names and numbers and their positions.

Rick

June 18, 2011
by wvtsa
wvtsa's Avatar

Sorry. Tired. Wrong word. I knew but realized too late that ot was a typo. Ill look in the morning. Its for going above and beyond for a model for a technology competition. The model doesn't have to be functional aand it doesn't break the rules to ask here. I read the guide and did the led blink but not the temp sensor. I've been diggimg through he code from traffic light but I'm new at this. Thanks. I think that was just what I needed for one part but can you give me a boolean answer on whether polling is possible?

June 18, 2011
by Rick_S
Rick_S's Avatar

Absolutely, you are only limited by your skill in programming. Polling is a common way to detect an input from a button or other sensor. If you look through the forum, you'll find programs, (some pretty basic), that have simple button input. For example, if you look at the thread "TTL ULN2038 Question" you can find a program I wrote for spinning a stepper motor. In the second iteration of that program, I added a button to reverse he motor. It happens to be on the pin your LED is on... but if you look at the code, it would be easy to change to another pin.

Rick

June 19, 2011
by wvtsa
wvtsa's Avatar

OK. I think I can do this now. Thanks a lot.

June 19, 2011
by hevans
(NerdKits Staff)

hevans's Avatar

Do let us know how the competition goes too!

Humberto

June 19, 2011
by wvtsa
wvtsa's Avatar

Thanks for all the help, guys. I have successfully programmed my NerdKit and depart for competition way too early tomorrow.

Post a Reply

Please log in to post a reply.

Did you know that NerdKits make a great gift? Learn more...