Shop Learn P1 Docs P2 Docs Events
Using Case — Parallax Forums

Using Case

IguanamanIguanaman Posts: 28
edited 2022-05-14 07:12 in Propeller 1

Hello. In the Propeller Manual v1.0 page 171 Spin language reference - CASE. I made this program.

CON
''''_clkmode = xtal1 + pll16x
''''_xinfreq = 5_000_000
''''X = 2
PUB start
''''dira[0]:=1
''''dira[1]:=1
''''dira[2]:=1
''''dira[3]:=1
''''case x
'''''''0: outa[0]:=1
''''''''''''waitcnt(clkfreq + cnt)
''''''1: outa[1]:= 1
'''''''''''waitcnt(clkfreq + cnt)
''''''2: outa[2]:=1
''''''''''waitcnt(clkfreq + cnt)
''''''3: outa[3]:=1
'''''''''''waitcnt(clkfreq + cnt)
This program will light a LED at the pin out listed to let me know what number X is. Can I use letters instead of numbers? Can this program be expanded to pick something typed in that closely resembles some encoded close to input? All the commands that the propeller knows I only know about 15 commands and I am trying to learn more. I love the IF...ELSEIF...ELSE commands. It is frustrating to know that my little brain will never be able to access the full capability's of the Propeller language. The possibility  are endless! Thank you for any information. 13 may 2022(fri)

ModEdit: Code tags added

Comments

  • Nice use of apostrophes to highlight your indenting! It's unnecessary, though. Precede and succeed your code with a line consisting of three backticks (```), and your indenting will display just as you've written it.

    -Phil

  • Can I use letters instead of numbers?

    Sure. You could take a key from the terminal and use it to control your LEDs like this:

      repeat
        c := term.rx
        case c
          "a", "A" : io.toggle(LED_A)
          "b", "B" : io.toggle(LED_B)
          "c", "C" : io.toggle(LED_C)
          "d", "D" : io.toggle(LED_D)
          other    : clear_leds
        time.pause(250)
    

    This will work with upper- and lowercase letters.

    You can make your program a bit easier to read by using a couple simple libraries. I've attached an archive to help yo get started.

  • Hello. Thank you very much for your response Phil Pilgrim (PhiPi) and JonnyMac. WoW so much I do not know. Thank you for the examples for the case spin code JonnyMac. Can not thank you enough! 16 may 2022(mon)

  • msrobotsmsrobots Posts: 3,632
    edited 2022-05-16 21:39

    And there are ranges too

    CASE x
       "1..4:" : do something if value is 1 , 2 ,3 or 4
       "6..10" : DO SOMETHING WITH VALUES FROM 6 TO 10
       other: handle the rest
    

    Mike

Sign In or Register to comment.