QS Board Programming the touchpads
skylight
Posts: 1,915
I'm dabbling around with learning how to program with spin and have managed to get the leds chasing in several ways and happy with that I now wish to learn how to use the touchpads, this is where I have run into a little problem.
the program so far:
I'm not sure if i'm using the correct syntax as I havn't even got that far, as soon as i try to load I get a dialog saying it cannot find Touch Button.spin in any of the editor tabs yet i have it loaded on the next tab to the one that the code above is on.
the program so far:
{{
A simple chase of the quickstart board Leds
}}
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
VAR
byte time
OBJ
Buttons : "Touch Buttons"
PUB LedCounter
time:=4
Buttons.start(_CLKFREQ / 100)
dira[16..23] := %1111_1111
If Buttons.state
repeat
outa[16..23] :=1
waitcnt(clkfreq/time + cnt)
outa[16..23] :=2
waitcnt(clkfreq/time + cnt)
outa[16..23] :=4
waitcnt(clkfreq/time + cnt)
outa[16..23] :=8
waitcnt(clkfreq/time + cnt)
outa[16..23] :=16
waitcnt(clkfreq/time + cnt)
outa[16..23] :=32
waitcnt(clkfreq/time + cnt)
outa[16..23] :=64
waitcnt(clkfreq/time + cnt)
outa[16..23] :=128
waitcnt(clkfreq/time + cnt)
What I want to do is get the If statement bit to only run the chase if i'm touching one of the pads,I'm not sure if i'm using the correct syntax as I havn't even got that far, as soon as i try to load I get a dialog saying it cannot find Touch Button.spin in any of the editor tabs yet i have it loaded on the next tab to the one that the code above is on.

Comments
You have to check your indentation - either it's only wrong in the post or you have to fix it in the code. All instructions that shall be executed if the if-condition is true have to be intended relatively to the if. (the repeat has to be shifted and therefore all instructions in the repeat loop also need to be shifted)
The current code would look for the state only once. So if you don't push a button at program start, your program will immediately shut down the propeller.
So, maybe you should rather do something like
repeat until Buttons.state
and remove the if statement.
I will also try the repeat until as suggested and let you know how i got on
con _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 CLK_FREQ = ((_clkmode - xtal1) >> 6) * _xinfreq MS_001 = CLK_FREQ / 1_000 US_001 = CLK_FREQ / 1_000_000 obj buttons : "Touch Buttons" pub main | b, idx buttons.start(CLK_FREQ / 100) dira[23..16] := %1111_1111 idx := 0 repeat b := buttons.state if (b == %0000_0001) outa[23..16] := 1 << idx idx := ++idx & %111 waitcnt(cnt + (CLK_FREQ >> 4)) else outa[23..16] := %0000_0000 idx := 0{{ A simple chase of the quickstart board Leds }} CON _clkmode = xtal1 + pll16x _CLKFREQ = 80_000_000 VAR byte time OBJ Buttons : "Touch Buttons" PUB LedCounter time:=4 dira[16..23] := 11_1111 Buttons.start(_CLKFREQ / 100) repeat If Buttons.state outa[16..23] :=1 waitcnt(clkfreq/time + cnt) outa[16..23] :=2 waitcnt(clkfreq/time + cnt) outa[16..23] :=4 waitcnt(clkfreq/time + cnt) outa[16..23] :=8 waitcnt(clkfreq/time + cnt) outa[16..23] :=16 waitcnt(clkfreq/time + cnt) outa[16..23] :=32 waitcnt(clkfreq/time + cnt) outa[16..23] :=64 waitcnt(clkfreq/time + cnt) outa[16..23] :=128 waitcnt(clkfreq/time + cnt) outa[16..23] :=0Now I need to work out how to get a specific pad to trigger the chase rather than any pad.
CON _clkmode = xtal1 + pll16x _CLKFREQ = 80_000_000 VAR byte time PUB LedCounter time:=4 dira[16..23] := 11_1111 repeat If (ina[0] == 1) outa[16..23] :=1 waitcnt(clkfreq/time + cnt) outa[16..23] :=2 waitcnt(clkfreq/time + cnt) outa[16..23] :=4 waitcnt(clkfreq/time + cnt) outa[16..23] :=8 waitcnt(clkfreq/time + cnt) outa[16..23] :=16 waitcnt(clkfreq/time + cnt) outa[16..23] :=32 waitcnt(clkfreq/time + cnt) outa[16..23] :=64 waitcnt(clkfreq/time + cnt) outa[16..23] :=128 waitcnt(clkfreq/time + cnt) else outa[16..23] := 0All that Jon's code is doing, is shifting the bits of "outa" pins 16 - 23 to the left and he also used less lines of code by doing what he showed you.I did change a line in the code though designating the outputs as some Leds were not turning on, the change was as below:
Original line
dira[16..23] := 11_1111
to
dira[16..23] :=%11111111
this turns on all leds in sequence
must have made an error in posting as it was like that in the original post but ended up wrong afterwards.
also noticed I didn't need to add the underscore it still worked
So change the code above to:
CON _clkmode = xtal1 + pll16x _CLKFREQ = 80_000_000 VAR byte time PUB LedCounter time:=4 dira[16..23] := 11_1111 outa[0] := 1 repeat dira[0] := 1 'charge button pin capacitor dira[0] := 0 'switch back to input waitcnt(clkfreq/500 + cnt) 'wait 2ms If (ina[0] == 1) 'read the button state outa[16..23] :=1 waitcnt(clkfreq/time + cnt) outa[16..23] :=2 waitcnt(clkfreq/time + cnt) outa[16..23] :=4 waitcnt(clkfreq/time + cnt) outa[16..23] :=8 waitcnt(clkfreq/time + cnt) outa[16..23] :=16 waitcnt(clkfreq/time + cnt) outa[16..23] :=32 waitcnt(clkfreq/time + cnt) outa[16..23] :=64 waitcnt(clkfreq/time + cnt) outa[16..23] :=128 waitcnt(clkfreq/time + cnt) else outa[16..23] := 0Andy
This one works on my Quickstart:
CON _clkmode = xtal1 + pll16x _CLKFREQ = 80_000_000 VAR long time PUB LedCounter time:=10 dira[16..23] := $FF outa[0] := 1 repeat dira[0] := 1 'charge button pin capacitor dira[0] := 0 'switch back to input waitcnt(clkfreq/500 + cnt) 'wait 2ms If (ina[0] == 0) 'read the button state outa[16..23] :=1 waitcnt(clkfreq/time + cnt) outa[16..23] :=2 waitcnt(clkfreq/time + cnt) outa[16..23] :=4 waitcnt(clkfreq/time + cnt) outa[16..23] :=8 waitcnt(clkfreq/time + cnt) outa[16..23] :=16 waitcnt(clkfreq/time + cnt) outa[16..23] :=32 waitcnt(clkfreq/time + cnt) outa[16..23] :=64 waitcnt(clkfreq/time + cnt) outa[16..23] :=128 waitcnt(clkfreq/time + cnt) else outa[16..23] := 0 waitcnt(clkfreq/200 + cnt) 'wait 5msYou need to touch the most right button !
Andy