Controlling a Pin
Newzed
Posts: 2,503
I have a little program that says:
If InA[noparse][[/noparse]1] == Low'High······················ 'Be sure to indent under the If
····· SetScreenToWhiteOnDarkBlue
····· VideoDisplay.str(String("INPUT AT PIN A1"))
····· OutA[noparse][[/noparse]16] := High
··· Else··································· 'Be sure to indent under the Else
····· SetScreenToWhiteOnRed
····· VideoDisplay.str(String("NO INPUT"))
····· OutA[noparse][[/noparse]16] := Low
As long as I hold my PB down, the LED on Pin 16 lights, but if I let go of the PB the LED goes off.· Is there a "Toggle" equivalent in Spin.· I would like to press the PB to turn it on, then press it again to turn it off.
Sid
'····
If InA[noparse][[/noparse]1] == Low'High······················ 'Be sure to indent under the If
····· SetScreenToWhiteOnDarkBlue
····· VideoDisplay.str(String("INPUT AT PIN A1"))
····· OutA[noparse][[/noparse]16] := High
··· Else··································· 'Be sure to indent under the Else
····· SetScreenToWhiteOnRed
····· VideoDisplay.str(String("NO INPUT"))
····· OutA[noparse][[/noparse]16] := Low
As long as I hold my PB down, the LED on Pin 16 lights, but if I let go of the PB the LED goes off.· Is there a "Toggle" equivalent in Spin.· I would like to press the PB to turn it on, then press it again to turn it off.
Sid
'····
Comments
!OutA[noparse][[/noparse]16]
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Who says you have to have knowledge to use it?
I've killed a fly with my bare mind.
Chapter 5, which is not out yet, is supposed to contain a list of operators.· Is this list available anywhere?
Sid
To make P0 a high output:
DIRA[noparse][[/noparse]0] := 1
OUTA[noparse][[/noparse]0] := 1
To make P1 a low output:
DIRA[noparse][[/noparse]1] := 1
OUTA[noparse][[/noparse]0] := 0
To make P7..P0 count upwards:
DIRA[noparse][[/noparse]7..0] := $FF
REPEAT
··· OUTA[noparse][[/noparse]7..0]++
To make P1 output the opposite of P0:
DIRA[noparse][[/noparse]0] := 0
DIRA[noparse][[/noparse]1] := 1
REPEAT
··· OUTA[noparse][[/noparse]1] := !INA[noparse][[/noparse]0]
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chip Gracey
Parallax, Inc.
Is DirA(7..0) = $FF the same as
DirA(7..0) = %11111111 which means Pins 7 thru 0 are set as outputs?
I'm not sure what OutA(7..0)++ means.· I can not find an expanation anywhere for the various operators.· I think it will be in Chapter 5, but Chapter 5 is not out yet.
The little program I posted works just like it should, but what I want to do is toggle Pin 16 each time the PB is pressed.· In PBasic I would write:
if IN1 = 0 then TOGGLE 16
How would I write this in Spin?
Thanks
Sid
a huge chunk of it is operators
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Who says you have to have knowledge to use it?
I've killed a fly with my bare mind.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Life is one giant teacup ride.
If I write:
DirA(7..0) = $FF
OutA(7..0)++
How could·I display OutA on my TV?
Thanks
Sid
I am not familiar with PBASIC commands, so I can't say that my following thoughts directly implement the toggle command as PBASIC does it. But here are my thoughts...
To do a toggle you need to detect the state change of the button and remember the last state the button was in. You can detect the rising edge (changing from a 0 to a 1) or a falling edge (changing from a 1 to a 0). I get to do a lot of PLC programming in my industry and we have a command called a one-shot that helps to detect the change of a button or switch. The one-shot means that when a state changes (high or low, depending on the one-shot command), a bit is on (or off) for one scan of the program, this is handy for doing latches and toggle buttons.
Here is psuedocode to help your thinking (I have not checked Spin syntax or tested yet). I need to check the operators, because it can probably be simplified with the cool tricks available in Spin.
You might want to also implement a debounce routine for the button; otherwise you might get an undetermined amount of toggles due to the noise in the button contacts and system when pressing the button.
edit: Looks like my indentation does not show up when typing in text
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Timothy D. Swieter
tdswieter.com
One little spark is all it takes for an idea to explode
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Life is one giant teacup ride.
Repeat 127
·· DirA[noparse][[/noparse]7..0]:= 0
·· OutA[noparse][[/noparse]7..0]++
·· term.hex(OutA[noparse][[/noparse]7..0],2)
·· term.str(string(" "))·
The TV displayed 01 thru 7F so I think that part works.· So what am I reading - is 01
the count on Pin 7, 02 the count on Pin 6, 03 the count on Pin 5, and so on, then 08
is the new count on Pin 7, and so on?
Sid
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Life is one giant teacup ride.
Data 0 0 0 0 0 0 0 0 (Hex: 00)
Data 0 0 0 0 0 0 0 1 (Hex: 01)
Data 0 0 0 0 0 0 1 0 (Hex: 02)
Data 0 0 0 0 0 0 1 1 (Hex: 03)
:
:
Data 0 0 0 0 1 1 1 1 (Hex: 0F)
:
:
:
Data 1 1 1 1 1 1 1 1 (Hex: FF)
I am not following you either Sid, does this table help explain what is happening? Each line of data represents an increment (++). Think of the pin 7 to pin 0 as holding an 8-bit value and the code is incrementing that value.
edited: Adjusted table to display better.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Timothy D. Swieter
tdswieter.com
One little spark is all it takes for an idea to explode
What you are reading back is the state of the eight pins in hex. "01" in hex is "00000001" binary, which means that only pin 0 is set. "02" hex = "00000010" binary, which means that only pin 1 is set. "03" hex = "00000011" binary, which means that pins 0 and 1 are set, etc.
Try this:
This will show you a binary representation of pins 7 through 0 so you can easily see what each pin is doing.
Also, note that you don't need to include dira[noparse][[/noparse]7..0] := 0 in the loop. Once the direction of the bits is set, it stays that way until you change it.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
OS-X: because making Unix user-friendly was easier than debugging Windows
links:
My band's website
Our album on the iTunes Music Store
This will show you the binary representation alongside the hex representation.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
OS-X: because making Unix user-friendly was easier than debugging Windows
links:
My band's website
Our album on the iTunes Music Store
Sid