C code for push buttons??

Discussion in 'Programmer's Corner' started by harami, Feb 29, 2012.

  1. harami

    Thread Starter Member

    Jun 20, 2011
    66
    0
    how do you write a code in C language for push buttons. if im to use two push buttons as an input to pic18f452. push buttons will be pressed one after another. and when push button A is pressed followed by push button B, number will increment and if button B is pressed followed by button A number will decrement. any suggestions/help??
     
  2. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    Let me make it more complicated for you. What if the user only presses one button, does the processor wait forever for the other button? How about if the sequence is A-B-A; is that read as AB followed by BA, or is it AB and then the beginning of a second AB sequence that's never completed? In fact it looks as if alternating A's and B's will cause either increments or decrements, depending on how it began. That doesn't seem like a very robust control method.
     
  3. harami

    Thread Starter Member

    Jun 20, 2011
    66
    0
    i am trying to built a programme which can monitor vehicle IN and OUT. im going to place two sensors at the entrance, A and B. Therefore A folliwed by B will be IN and B followed by A will be OUT. the reason im using push buttons is just for a testing purpose. so how do we write a programme for increment and decrement with respect to the inputs.
     
  4. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    Is vehicle counting the only homework they give out these days? ;)

    if A pressed;
    1. wait up to 1 second for B to be pressed
    2. if B pressed in <1 second, then vehicle+1

    if B pressed;
    1. wait up to 1 second for A to be pressed
    2. if A pressed in <1 second, then vehicle-1
     
  5. RiJoRI

    Well-Known Member

    Aug 15, 2007
    536
    26
    Snort! Traffic controllers were too difficult?? :D

    Now if they'd combine traffic counting and control...

    --Rich
     
  6. harami

    Thread Starter Member

    Jun 20, 2011
    66
    0
    this is exactly what im trying to programme. i have only written simple programme and debugged it. but now im trying to built something using microcontroller, i just dont know how to start. i mean how do i write a code to input A and B??
     
  7. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    Which section of the manual have you looked in, and what did it say?
     
  8. harami

    Thread Starter Member

    Jun 20, 2011
    66
    0
    what manual are you talking about? sorry i didnt really get you.
     
  9. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    panic mode likes this.
  10. panic mode

    Senior Member

    Oct 10, 2011
    1,321
    304
    most micro-controllers have I/O pins that are configurable to one of several functions. in order to use one of pins as input, you need to configure it to act as input (digital input in this case). this may involve enabling internal pull-up resistors, tristating output etc. (read the datasheet).

    then you read the corresponding port and test the bit (this can be done in different ways , depending on instruction set of a given MCU).

    now that you have the bit value of interest, you use it in piece of code that was described earlier (simple debounce logic).
     
Loading...