Need some help!!

Hey there. Fairly new to pbasic, but I need it for a project. I'm trying to make my counter increase 1 every time I press my pushbutton, but can't seem to get my head wrapped around the code. Can somebody lend me a fresh brain? Stamp bs2, pbasic 2.5

Comments

  • How is the button connected to the microcontroller?
  • silence00,

    Look at page 79 of "What's a Microcontroller?".
    https://www.parallax.com/sites/default/files/downloads/28123-Whats-a-Micro-v3.0.pdf
  • ' suppose button is on p0, with pullup resistor to Vdd
    ' state machine logic, can be part of a loop executing other tasks.
    oldstate VAR bit
    newstate VAR bit
    activestate VAR bit
    
    oldstate = in0    ' initialize state
    DO
       newstate = in0
       activestate = newstate ^ oldstate & newstate ' active when button is released to high level
       oldstate = newstate
       IF activestate THEN DEBUG "buttonup",CR
       ' do other tasks
    LOOP
    

    There are many ways to scan buttons depending on the goal. There is also the PBASIC BUTTON command, but that is total overkill unless you want the repeat function.

    A reference:
    http://emesystems.com/OLDSITE/BS2fsm.htm#counter
  • xpacesixxpacesix Posts: 1
    edited 2020-10-13 - 04:09:50
    Hi, i'm beginner , therefor i can't understand some simple thing like push button Counter. my problem: in need to write a simple program, which one, counting the push button,wenn pressed and save it in one register for later. for example: if button pressed one time, led1 on. led 2&3 off. if button pressed two time, just led 2 on. led1&3 off if button pressed 3 time, just led3 on, led1&2 off and wenn pressed a gain, repeat it same. i founded this code:
    int main() { int count=0; int old_pb=0; int new_pb; pb.mode(PullUp); wait(.001); while(1) { new_pb = pb; if ((new_pb==0) && (old_pb==1)) count++; myled4 = count & 0x01; myled3 = (count & 0x02)>>1; myled2 = (count & 0x04)>>2; myled = (count & 0x08)>>3; old_pb = new_pb;
    

  • Mike GreenMike Green Posts: 23,013
    edited 2020-10-13 - 04:10:31
    You need what's called a "Finite State Machine". There's a very thorough article in the Wikipedia on this (<https://en.wikipedia.org/wiki/Finite-state_machine>;). Each state becomes a loop with a specific label. The program does a GOTO another state when a button is pressed. You might have something like this:
    state000:
    led1 = 0 ' Set up LEDs for this "idle" state
    led2 = 0
    led3 = 0
    if button1 = 1 then goto state001 ' If button pushed, go to next state
    goto state000 ' otherwise, stay in this state
    state001:
    if button1 = 0 then goto state002 ' Make sure button is released
    goto state001 ' Stay in this state until button released
    state002:
    led1 = 1 ' Set up LEDs for this state's action
    led2 = 0
    led3 = 0
    if button1 = 1 then goto state003 ' If button pushed, go to next state
    goto state002
    state003:
    if button1 = 0 then goto state004 ' Make sure button is released
    goto state003
    state004:
    led1 = 0 ' Set up LEDs for this state's action
    led2 = 1
    led3 = 0
    if button1 = 1 then goto state005 ' If button pushed, go to next state
    goto state004
    

    You can continue this for as many states as you need. You could have a second button that takes you back to state000 or whatever you want.
  • Happened on this thread by accident, but a good book on this (if it is still in print) is "Digital Logic and State Machine Design" by David J. Comer. If you are a student or interested in this topic, it is an excellent resource. Lots of ways to get it, but don't bother with amazon new, we can put up with a lot of flaws in used $8.00USD vs. $200USD new. Also recommend the paperback if you can find it "Realtime Programming: Neglected Topics" by Caxton C. Foster. Both can be found on google books and other sources.
  • Happened on this thread by accident, but a good book on this (if it is still in print) is "Digital Logic and State Machine Design" by David J. Comer. If you are a student or interested in this topic, it is an excellent resource. Lots of ways to get it, but don't bother with amazon new, we can put up with a lot of flaws in used $8.00USD vs. $200USD new. Also recommend the paperback if you can find it "Realtime Programming: Neglected Topics" by Caxton C. Foster. Both can be found on google books and other sources.

    You can also digitally borrow many books for free from openlibrary.org. I've had luck there with older technical books.
Sign In or Register to comment.