View previous topic :: View next topic |
Author |
Message |
mgmidget
Joined: 29 Apr 2012 Posts: 2 Location: uk
|
decoding button sequence |
Posted: Tue May 01, 2012 11:24 am |
|
|
I am very new to C programming and reading lots and asking lots of questions that most of you probably already know the answers to.
Question: I would like to read the input from 2 push button switches and decode the input and decide what action to take. This is a form of door lock.
Looking at using the PIC 16F627A with CCS software.
The input sequence is as follows.
buttons A , Button B
0,0
0,1
1,1
1,0
0,0
If this sequence happens, another function will be initiated.
If the reverse of this sequence happens another function is initiated.
What is the best way to implement this with C ?
Could both buttons be read at one time and the second line decide which function is going to start, or is it better to read all 5 lines before a decision is made.
Is there a very simple way of doing this?
I can read single button inputs and know what to do from there but feel there is a smarter way.
Thank you all for any time you can spare to help a newbie. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed May 02, 2012 2:31 am |
|
|
Try a state-machine approach.
There are loads of examples on this forum.
Mike |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed May 02, 2012 9:44 am |
|
|
If you treat buttons A and B as having values 2 and 1 your sequences then become 0, 1, 3, 2, 0 and 1, 2, 3, 1, 0.
Then look for these sequences as though you were using a parser.
The method can be extended to more characters or longer sequences.
Mike |
|
|
mgmidget
Joined: 29 Apr 2012 Posts: 2 Location: uk
|
|
Posted: Thu May 03, 2012 8:27 am |
|
|
Thank you all for your input.
I did search but didn't find the state machine route
I will have a play and get back if I get stuck.
Is there any one book you guys would recommend for Embedded C, or is trial and pain the best learning curve.
Thank you all again.
Best regards |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Thu May 03, 2012 9:23 am |
|
|
Quote: | or is trial and pain the best learning curve |
there is no book that teaches the logical steps you must take for a particular action...you can learn syntax but the logic is all yours.
there are several ways you can read buttons (or any other particular action for that matter)
Poll pins individually with If/else
Poll the port as a whole.
use tables
you can do state machines as suggested.
etc...
you can read a book that teaches you to make a state machine, but you wont find a book that teaches you a statemachine specifically for what you need.
G _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Thu May 03, 2012 11:29 am |
|
|
You've been given hints and links to a useful thread.
Have a go. Try several approaches.
When you get stuck, come back to the forum and someone will probably be able to help, if we can see that you've made an effort.
Many of the guys on the forum have decades of experience and could each dream up dozens of ways to achieve your goal.
You can't aquire that level of expertise overnight.
Mike |
|
|
|