"ina" Questions
Blake Koch
Posts: 39
in Propeller 1
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.
However when I add a third "ina" the program does give me the desired result.
%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
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 lowFor 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
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.
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.
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?
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?
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
You always should use INA[hi..lo] to get normal (not reversed) values to compare.
Andy
Again thank for any input. The problem of mapping what pins Buttons equal what binary or DEC equivalent when pressed is still unanswered.
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
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?
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
Frustrating
Thanks guys for helping me figure this out. Just wish I checked the battery first.