Shop OBEX P1 Docs P2 Docs Learn Events
Simple input reading — Parallax Forums

Simple input reading

francis_L_Hfrancis_L_H Posts: 10
edited 2010-10-22 12:50 in Propeller 1
I am having a small task that is turning out to be a bigger problem than needs to be and taking up too much time. I am simply trying to assign a certain value to a variable based on a given user input.

I have three possible user inputs, pins 21, 22, and 23. The program awaits for the user to make a choice between 1 of 3 push buttons assigned to the mentioned pins, based on which button is pushed determines what the value of the variable should be. The section of code at hand follows:

dira[23..21] := outa[23..21] := %000
LCD.Str(String("Select Length",13))
repeat until ina[21] or ina[22] or ina[23]
Temp := ina[23..21]
if Temp[0] := 1
th := 96
elseif Temp[1] := 1
th := 120
elseif Temp[2] := 1
th := 180
The code will wait for the user to push one of the buttons, but regardless of which is pushed, it will always use the first statement to assign the value of the variable. In this case th = 96. Why can I not look at each bit and assign the corresponding value if the corresponding button was pushed?

Comments

  • VernVern Posts: 75
    edited 2010-10-21 15:17
    My experience is almost 0 but shouldn't it be
    if Temp[0] == 1
    not
    if Temp[0] := 1

    := would set the variable to 1 no?

    Where as == would check to see if its equal to 1?
  • legoman132legoman132 Posts: 87
    edited 2010-10-21 16:44
    what size is "temp"? from what I know, you can't address an individual bit like that (I've tried), so what I did (probably not the most efficient way) was create a bitmask with the "|<" operator (or was it the ">|"?), AND the bitmask and the long containing the bit together, then comparing the result to 0 to see if a bit was set. There's probably a better way, If someone more experiance suggests it, but If you call the method (its a good idea to put it in a method, at least in my mind) only 2 or 3 times in a program, it can't be that bad (my ROV controller prototype handled a bunch of these with ease).
  • T ChapT Chap Posts: 4,223
    edited 2010-10-21 17:52
    Vern wrote: »
    My experience is almost 0 but shouldn't it be
    if Temp[0] == 1
    not
    if Temp[0] := 1

    := would set the variable to 1 no?

    Where as == would check to see if its equal to 1?

    Correct. You are assigning the value of 1 to each test, not checking to see what the value is.

    It would be best to work with individual pins instead of trying to read all 3 like that.

    Another thing to do when having these kinds of problems, is to see what you are really getting prior to assigning the inputs to variables:

    PUB testinputs
      Repeat
         gotoxy(0,0)
         LCD.dec(ina[21]
         LCD.str(string(" "))
         LCD.dec(ina[22]
         LCD.str(string(" "))
         LCD.dec(ina[23]
    
    

    Push each button and make sure the system is seeing what it should. Then assign the inputs to variables, print out the variables too and make sure they are what they should be.
  • francis_L_Hfrancis_L_H Posts: 10
    edited 2010-10-22 08:33
    Thanks everyone. The == operator was my first mistake. I ended doing just as you said T Chap. I printed the values I was getting from inputs and changed my code so I could differentiate between the pins. It probably isn't the best way to learn, but regardless I accomplished what I needed to accomplish for now.

    Thanks again.
  • T ChapT Chap Posts: 4,223
    edited 2010-10-22 11:17
    It probably isn't the best way to learn...


    I wish I could figure out the 'best way to learn' myself, things would then get a lot easier.
  • VernVern Posts: 75
    edited 2010-10-22 12:50
    Pain is the greatest teacher. Although how to incorporate pain stimuli into learning to code I'm not sure. hahaha.

    If you think about it when you are a child you only have to burn yourself once on the oven to know that you shouldn't touch it again. Your mother could tell you a hundred times but until you learn the lesson through pain it will never stick.
Sign In or Register to comment.