View previous topic :: View next topic |
Author |
Message |
QBoxer Guest
|
switch control |
Posted: Mon Jun 15, 2009 5:17 am |
|
|
Hello,
I have built a PIC circuit that is controlled by four switch inputs. I want to press one of the switches and increment a counter from 1 - 7 each time it is pressed regardless of how long it is held for (and then reset to 1 when it reaches 7). The counter will then control a display cursor on an lcd module.
At the moment I can press a switch and read whether it is in an on or off state but I just cant seem to get my head round the code that I need to achieve the counter function.
I know this sounds silly cause I am sure it is quite simple. I am pretty new to this whole thing and I am not asking for written code particularly just an explained approach or tips or advice? Any help would be much appreciated, cheers |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Jun 15, 2009 6:41 am |
|
|
What you need is the software construct called a "state machine". Google "state machine" and you should find more than you could possibly need to know ;-) _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Mon Jun 15, 2009 9:09 am |
|
|
This may not be the best way to do this but it works. We'll assume you're using A0 as your input and it is active LOW when you press the switch. Now, I've not added any debouncing to this.
Code: | int1 entered = 0;
int8 counter = 0;
if(input_low(PIN_A0) && !entered)
{
entered = 1;// change state so we don't re-enter until we've released
if(++counter > 7)//the button first
{
counter = 1;
}
}
if(entered && input_high(PIN_A0))
{
entered = 0;// reset flag so we can wait for the next button press
} |
This is only one way to accomplish this.
Ronald |
|
|
QBoxer Guest
|
|
Posted: Tue Jun 16, 2009 4:22 am |
|
|
Hello,
Thank you for that rnielsen, have adapted and got it more or less working perfectly for my circuit (it seems a little bit temperamental sometimes -don't know why this would be?)
Also thank you for the pointer to the state machine approach SherpaDoug, I would never have thought of that and even though some of it above my head, I can see it is very good approach, especially for the finally software and actually the program is a lot easier to think about in that way. Cheers |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Tue Jun 16, 2009 6:03 am |
|
|
Being "a little temperamental" sounds like you need switch debouncing. Any mechanical switch is likely to produce a series of short make and breaks as the metal contacts collide and microscopically bounce. If you read the switch fast enough you may see more that one electrical switch closure for a single mechanical activation.
Search on this forum for "debounce" for some ideas to deal with this. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
QBoxer Guest
|
|
Posted: Tue Jun 16, 2009 9:18 am |
|
|
Added debouncing code it is working perfectly. Thank you very much for your help! |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Tue Jun 16, 2009 9:37 am |
|
|
Quote: | if(input_low(PIN_A0) && !entered) |
Stupid me (what else is new?). Morphed input & output commands. Should have been
Code: | if(!input(PIN_A0) && !entered) |
But, hopefully you saw that little goof and got it to work correctly.
Ronald
PROCRASTINATORS UNITE!!!........ tomorrow |
|
|
|