Shop OBEX P1 Docs P2 Docs Learn Events
"ina" Questions — Parallax Forums

"ina" Questions

I'm trying to get this program to notice when 2 buttons are pressed and the third is not to preform a task.

What I'm missing is the understanding as to how the chip is making this comparison can any one help me.

The first set of code works using the binary %11 as the comparison against ifina[21..22] == %11 pin 4 only goes high when the right two buttons are pressed.
'' Test Program

CON
  _xinfreq = 5_000_000
  _clkmode = xtal1 + pll16x

VAR
  long button1                                          ' Variable to store ina Register

PUB PushButtonTest

  dira[21..23]~                                         ' Redundant, sets pin 21 thru 23 to input                        
  dira[4..9]~~                                          ' Sets pin 4 thru 9 as output

  repeat                                                ' Endless loop
    button1 := ina[21..22]                              ' Assign button1 the input register????????? THIS IS WHAT I THINK IT WOULD DO. 
      if ina[21..22] == %11                             ' If ina Register matches buttons 1 and two pushed at the same 
        outa[4] := 1                                    ' Turn pin4 high
    
    outa[4] := 0                                        ' If buttons 1 and 2 are not pressed turn pin 4 low


However when I add a third "ina" the program does give me the desired result.
'' Test Program

CON
  _xinfreq = 5_000_000
  _clkmode = xtal1 + pll16x

VAR
  long button1                                          ' Variable to store ina Register

PUB PushButtonTest

  dira[21..23]~                                         ' Redundant, sets pin 21 thru 23 to input                        
  dira[4..9]~~                                          ' Sets pin 4 thru 9 as output

  repeat                                                ' Endless loop
    button1 := ina[21..23]                              ' Assign button1 the input register????????? THIS IS WHAT I THINK IT WOULD DO. 
      if ina[21..23] == %011                            ' If ina Register matches buttons 1 and two pushed at the same 
        outa[4] := 1                                    ' Turn pin4 high
    
    outa[4] := 0                                        ' If buttons 1 and 2 are not pressed turn pin 4 low

For example the wrong buttons make pin 4 turn high. If I add these binary %000 values to the if statement I get strange results.

%001 the two correct buttons pins 21 and 22 will turn pin 4 high
%011 no buttons turn pin 4 high
%010 no buttons turn pin 4 high
%110 only pin 23 turn pin 4 high
%100 the wrong buttons pins 22 and 23 turn pin 4 high

I've read the manual and I still don't have a good understand how the "ina" register is storing this value.

Thanks for the help
Blake

Comments

  • Tracy AllenTracy Allen Posts: 6,656
    edited 2017-05-01 20:39
    Not at all sure, but I suspect that there is a problem with the pullup or pulldown resistors on the input pins. It can help greatly if you simply make a program to pass through the input pin state to three leds on the output:
    main
      dira[4..6] := %111 'outputs to led/resistor from 3 pins to Vss
      repeat
        outa[4..6] := ina[21..23] .  ' leds should reflect inputs exactly.
    
    You said,
    Assign button1 the input register????????? THIS IS WHAT I THINK IT WOULD DO.

    Not exactly, it does put the current value of ina[21..23] into the variable button1, but it does not assign the name of the variable to the input register. I'm not sure what you are thinking there.

  • lardomlardom Posts: 1,659
    edited 2017-05-01 21:09
    I think what you wrote should work. The only problem I saw was that you didn't tell the program what to do with the value you stored in button1.
    The first thing I would do is double check to be sure your buttons were 'connected' to pins 21 and 22.
    At some point you will need to become familiar with debouncing so I wrote an untested second version of your method with two extra instructions.
    CON
      _xinfreq = 5_000_000
      _clkmode = xtal1 + pll16x
    
    VAR
      byte button1                                          ' Variable to store ina Register
    
    PUB PushButtonTest
    
      dira[21..23]~                                         ' Redundant, sets pin 21 thru 23 to input                        
      dira[4..9]~~                                          ' Sets pin 4 thru 9 as output
    
      repeat                                                ' Endless loop
        button1 := ina[21..22]                              ' Assign button1 the input register????????? THIS IS WHAT I THINK IT WOULD DO. 
          if button1 == %11                             ' If ina Register matches buttons 1 and two pushed at the same 
            outa[4] := 1                                    ' Turn pin4 high
          else
            outa[4] := 0                                        ' If buttons 1 and 2 are not pressed turn pin 4 low
    
    PUB PushButtonTest_Debounce
    
      dira[21..23]~                                         ' Redundant, sets pin 21 thru 23 to input                        
      dira[4..9]~~                                          ' Sets pin 4 thru 9 as output
    
      repeat                                                ' Endless loop
        button1 := ina[21..22]                              ' Assign button1 the input register????????? THIS IS WHAT I THINK IT WOULD DO. 
          if button1 == %11                             ' If ina Register matches buttons 1 and two pushed at the same
            waitcnt(clkfreq/1000 + cnt)     
            if button1 == %11    
              outa[4] := 1                                    ' Turn pin4 high
            else
              outa[4] := 0                                        ' If buttons 1 and 2 are not pressed turn pin 4 low
    
  • Thank you

    The first program with two Buttons does work. The problem I'm having is when I want to add a third or Forth button the binary condition's stops working right.

    Such as with three Buttons and I want pins 21 and 22 to execute the next line of code if this condition is meet.

    If I use
    %001 the two correct buttons/pins 21 and 22 will turn pin 4 high
    %011 no buttons turn pin 4 high
    %010 no buttons turn pin 4 high
    %110 only pin 23 turn pin 4 high
    %100 the wrong buttons pins 22 and 23 turn pin 4 high
    so I'm not sure what "ina's register would look like if I wanted buttons 1 and 3 to do one executable and pins 2 and 3 to do a different executable.

    does anyone know how and/ or if this is possible?
  • Blake KochBlake Koch Posts: 39
    edited 2017-05-02 00:47
    After a few more test I want to correct that the all pins do something with numbers up to DEC 7.

    also that the circuit is a pull up resister type so pins 21 .. 23 see a 3.3 volts at rest.

    Pin 4 goes high when

    0 = 21,22,23 are grounded
    1 = 21,22 are grounded
    2 = 21,23 are grounded
    3 = 21 only grounded
    4 = 22,23 are grounded
    5 = 22 only grounded
    6 = 23 only grounded
    7 = pin 4 high without any buttons pressed
    8 = nothing after this

    I think I just answered my own question.
    However this raises a new question how could I have predicted this outcome with out having to test and type each number in. Just in case I choose to add more buttons.

    Is there a table to explain this outcome?

  • Blake, if you run Tracy Allen's code with led's things will clear up quickly. INA is a read only register that returns the input state of a pin. For example, if you press a button connected to pin 21 then ina[21] == 1. As soon as you release the button then ina[21] == 0.
  • Larry and Tracy thank you. I get what Tracy Allen's program does and that works great if I only wanted to push one button at a time, However if I need to press a sequence of buttons at the same time to execute pin4 high this type of programing does not work or would require a lot of "if" Conditions.

    I found with my last test that each number corresponds with a sequence of buttons pressed in the "if" Condition. With 3 buttons/pins there are 8 possible outcomes {see my chart above} using numbers 0 though 7 in the "if" Condition.

    I now have a chart for three buttons/pins as I performed a manual test to get the answers. However this is only three buttons with a 3 bit register what if I had a total of 8 buttons that's 256 possible outcomes. Without a map I would have to test all outcomes and log them. I'm hopeful someone has already done this or can point me to a formula to calculate them.

    My new question is "Is there a table or formula that would give me a advanced knowledge of what numbers would correlate with which buttons/pins being pressed. This way I would know what numerical value would be returned to the button1 variable when different clusters of buttons/pins are being pressed. "?

    Thanks again

  • AribaAriba Posts: 2,682
    edited 2017-05-02 01:41
    I think your problem is that you use the bit numbers in the wrong order. If you specify a bit range with the lower pin first and the higher pin last then Spin reverses the bits internally.
    You always should use INA[hi..lo] to get normal (not reversed) values to compare.
    if ina[21..23] == %011
    
       %011
        |||
        ||23
        |22
        21
    
    
    if ina[23..21] == %011
    
       %011
        |||
        ||21
        |22
        23
    

    Andy
  • Blake KochBlake Koch Posts: 39
    edited 2017-05-02 13:14
    Andy thanks I tried reversing the high low pins in ina and found the chart just reverses.
    'the way I had it
    if ina[21..23] == %001
    
      %001= 
        ||
        22
         21
      'these pins triger pin 4
      %100=
       ||
       23
        22
      'these pins triger pin 4
      %011=
         |
         21
      'this pin trigers pin 4   
    reversed
    if ina[23..21] == %001
    
      %001
       ||
       23
        22
      'these pins triger pin 4
    
      %100
        ||
        22
         21
      'these pins triger pin 4
      
      %011
       |
       23
      'this pin trigers pin 4
    

    Again thank for any input. The problem of mapping what pins Buttons equal what binary or DEC equivalent when pressed is still unanswered.
  • twm47099twm47099 Posts: 867
    edited 2017-05-02 16:41
    If button1 := ina[23..21] ' active low
    and if
    x := !button1 ' bitwise not button1
    And then bitwise and it with 7
    doesn't x equal the value from 0 to 7?

    Then you could use the case statement to go where you want, or put the value x into an outa[] to turn on the specific 8 leds? (once you had st the directions to output.

    note, I am not conversant enough in spin to try to write an object.

    Tom
  • Don't overthink this. The tiny program I posted is a simple way to visualize how the buttons and the leds are representations of a 3-bit binary number. When you press two buttons, two leds should follow suit. A simple mapping--one button--one led. The way the buttons and leds are hooked up and listed will affect the order of the 7 states. This will also reveal wiring errors if you don't get 7 states as expected.

    If you add a serial object to the mix, you could also debug the state of the inputs by showing the binary state %xxx on your computer terminal.

    You said, "if I need to press a sequence of buttons at the same time to execute pin4 high this type of programing does not work ". It is true that when you try to press two buttons at the same time, you will really get a sequence, as you will inevitably press one of them a bit ahead of the other. Is that a concern?
  • You said, "if I need to press a sequence of buttons at the same time to execute pin4 high this type of programing does not work ". It is true that when you try to press two buttons at the same time, you will really get a sequence, as you will inevitably press one of them a bit ahead of the other. Is that a concern?

    Yes that was the concern.

    The idea of using the serial object might solve my problem of finding the binary value for what sequence of buttons are being pushed. I'll write a test program and check it.

    As for not overthinking it I CAN'T I was born this way. LOL
  • Problem found Low voltage on the battery. New battery and everything works the way Tracy Allen said it would.

    Frustrating

    Thanks guys for helping me figure this out. Just wish I checked the battery first.
Sign In or Register to comment.