Shop OBEX P1 Docs P2 Docs Learn Events
Trying to learn spin — Parallax Forums

Trying to learn spin

I cant seem to figure out conditional branching. Using outa[9..4]  tied to leds, and ina[23..21] tied to pushbuttons, I am trying to make a 3 button calculator just for educational purposes.
PUB  PBCount
    dira[9..4]~~
    dira[23..21] := %000
    outa[9..4] := %000000
   
    repeat while flag == 0
       if ina[21] == 1
       outa[9..4] := outa[9..4] + 1
          waitcnt(clkfreq/2 + cnt)

     if ina[22] == 1
     flag := 1
     a := outa[9..4]
     outa[9..4] := %000000
    repeat     
             if ina[21] == 1
             outa[9..4] := outa[9..4] + 1                
             waitcnt(clkfreq/2 + cnt)
                if ina[23] == 1
                   b := a + outa[9..4]
                   outa[9..4] := b
     This code does not do what I want.
     What I want it to do is by pressing singly or holding the first button, it increments outa. This enters the first operand.
     After depressing the second button it saves outa in variable a, and clears outa. This is supposed to be like the "+" button.
     Then the first button is used to increment outa again. This enters the second operand.
     After the third button is presses it adds a to outa for the result. This is like the "=" button.
     I would like it to clear and start again by pressing the first button again, which will start entering the first operand again.
    I am aware that there will be an overflow problem with bigger numbers, I am just trying to learn the structure of the indents and conditions to make
    this possible in spin so that I can figure it out.


Please help
Thanks

Comments

  • try wrapping your code in html brackets (from the source code button at the right of the edit box) like so:
    <pre><code>PUB  PBCount    dira[9..4]~~</code></pre>

    Which will in turn will look like this
    PUB  PBCount
        dira[9..4]~~
    
  • I hope that you can read this flowchart. Im not really interested in the program itself, just the spin structure to get it done. Please help
    thanks
    2448 x 3264 - 1M
  • How are your buttons wire?
  • Please see code above. It uses the propeller education platform. I am just trying to perform all the decision blocks without the luxury of jump or goto.
  • This is the hardware, should be pretty typical.
    3264 x 2448 - 2M
  • This is the hardware, should be pretty typical.

    I don't see a pullup resistor on the button. 
    The side of the button which connects with the Propeller should have a 10k resistor connecting it to 3.3V. As it is now, the pin looks like it's floating which will give very unpredictable results.
    Your code doesn't have much of a chance of working with the present hardware. I'll compare your code with the flowchart to see if the Spin is correct.
  • It is wired almost as you say with a 10k pull down resistor. Logic 1 when pressed, logic 0 when depressed. This is copied straight from the propeller education kit lab fundamentals manual. I know that the buttons operate as I have been successful in incrementing the first data entry sequence. I just cant seem to figure out how to nest repeat loops to match the flowchart. Once I get this right, I will be able to understand this on my own. The manual only seems to have examples of sequential programs, not ones with what I call conditional branching which would be typically handled with jump or goto, which spin does not have.

  • Sorry, I missed the bottom section of the picture. I'll post about the code a bit later.
  • I finally figured it out on my own, thanks anyway.
Sign In or Register to comment.