Shop OBEX P1 Docs P2 Docs Learn Events
Conditional statement question. — Parallax Forums

Conditional statement question.

JMLStamp2pJMLStamp2p Posts: 259
edited 2008-01-14 13:35 in Propeller 1
Hello all,
If I wanted to check multiple Pins on my Prop what is the most efficient way to Code ? PUB Check_Pins method is Highlighted below.
Any advise would be appreciated.

...............................................................................................

CON
·_CLKMODE = XTAL1 + PLL1X····· 'External crystal 5MHZ times "1"
·_CLKFREQ = 5_000_000·········· ·'5 Million cycles

OBJ
·DELAY: "Clock"
·DATA: "FullDuplexSerial"

VAR
·long Stack [noparse][[/noparse]9]
·byte Byte_Recieved
·long Input_2
·long Input_3

PUB MAIN
·dira[noparse][[/noparse]0]~~
·dira[noparse][[/noparse]1]~
·dira[noparse][[/noparse]2]~
·dira[noparse][[/noparse]3]~

·data.start(1,0,0,9600)
·Run_Program

PUB Run_Program
·cognew((Check_Pins), @Stack)

...............................
PUB Check_Pins

·Input_2 := INA[noparse][[/noparse]2]
· If Input_2 == 0
·· Increment_Display
· ELSE·······················

·· Check_Pins
...............................
··
PUB Increment_Display
· waitcnt(625_000 + cnt)
· data.tx($01)
· waitcnt(625_000 + cnt)
· data.tx($00)
· Check_Pins

PUB Decrement_Display
· waitcnt(625_000 + cnt)
· data.tx($02)
· Check_Pins
·
PUB Recieve
·data.start(21,20,%0000,9600)
· REPEAT
·· case data.rx
··· 1: Pixels
··· 2: TestPlay


·

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-01-11 21:32
    Before we get to your question, we need to deal with something first. The way you have coded your program is fundamentally flawed, you have Check_Pins doing two possible branches, Increment_Display or recursively calling Check_Pins again, and Increment_Display calls Check_Pins repeating the loop. Nowhere in this program flow is a return from a call ever taken. You will keep growing the stack until you run out of memory and your Propeller stops working.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-01-11 21:38
    You will run into several problems with this:
    1) Your Check_Pins routine calls itself and calls Increment_Display which calls Check_Pins. That will cause a stack overflow.

    2) Check_Pins runs in its own cog and calls the serial I/O routines. Depending on what your main program is doing, you
    may have several cogs calling the serial I/O routines which could cause problems. If this cog is the only one calling the
    transmit routines, you may be ok.

    I don't know what you're doing with Recieve. It doesn't seem to be called and it reinitializes the serial I/O object.

    Run_Program just exits after starting Check_Pins and, after returning to your main routine, "falls off the end" which causes the
    main cog to stop.

    How about starting with a brief description of what you want to accomplish rather than posting relatively long fragments of code that
    can't do anything useful? Then we can point you to possible examples of similar problems that you can start with and modify.
  • JMLStamp2pJMLStamp2p Posts: 259
    edited 2008-01-13 22:31
    Sorry Guys, I guess I should have explained. I am just learning Spin and was trying several code fragments just to see if I was coding right and if they would compile. My question· was in referance to the highlighted part of the code as follows:

    ....................................
    PUB Check_Pins

    · Input_2 := INA[noparse][[/noparse]2]

    · If Input_2 == 0
    ··· Increment_Display

    · ELSE·······················

    ·· ·Check_Pins
    ....................................

    I want to have·8 push buttons connected to 8 seperate IO lines on the Prop. I would like to have a dedicated processor check the status of these inputs constantly and jump to a specific sub-routine for that paticular button press. What would be the most efficient way for the dedicated processor to run in a loop checking the status of all 8 buttons and call methods to handle each one ?

    Thanks for the help,
    JMLStamp2p
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-01-13 23:26
    Ok ignoring the errors in the program flow of your code, the most efficient means for checking the states of multiple pins is waitpne, you establish the mask as the 8 pins you are checking and set the state to thier current value, so whenever the state of any pin deviates from from it's current value waitpne exits and you compare the current state with the old state to determine which pins have changed thier value.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-13 23:29
    JMLStamp:
    What Paul and Mike didn't say so openly is that your code is a total mess! Of course we will help, but it would help you much more if you were working yourself through the tutorials, especialy chapter 3 of the manual - from the beginning to the end!

    You want to read the status of an input pin; you do this by INA[noparse][[/noparse]nr_of_pin]; so the simplest way (there are three or four more possibilities) is to check them one ofter the other:
    REPEAT
       IF INA[noparse][[/noparse]pin1]
          pin1routine
       IF INA[noparse][[/noparse]pin2]
          pin2routine
    
  • JMLStamp2pJMLStamp2p Posts: 259
    edited 2008-01-14 13:35
    Thank You DeSilva,
    The code as a whole was never intended to run a specific task, my question in general was the highlighted code as you have answered.
    Thanks,
    JMLStamp2p

    PS: I will read chapter 3
Sign In or Register to comment.