Shop OBEX P1 Docs P2 Docs Learn Events
QS Board Programming the touchpads — Parallax Forums

QS Board Programming the touchpads

skylightskylight Posts: 1,915
edited 2011-11-28 06:18 in Propeller 1
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:
{{
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

  • MagIO2MagIO2 Posts: 2,243
    edited 2011-11-27 15:31
    It does not have to be in an editor tab, it has to be stored in the root of the propeller tool or in the same folder in which your program is stored.

    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.
  • skylightskylight Posts: 1,915
    edited 2011-11-27 15:41
    thank you kindly for the very fast response, I now understand about including all the files together in one folder, this now makes sense of the archive feauture in the prop tool.

    I will also try the repeat until as suggested and let you know how i got on
  • JonnyMacJonnyMac Posts: 9,198
    edited 2011-11-27 15:47
    Here's one solution (tested and works).
    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
    
  • skylightskylight Posts: 1,915
    edited 2011-11-27 16:26
    Ok I have adapted the code to this and it works brilliantly, any pad triggers one cycle of the chase or hold pad for continuos chasing
    {{
    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] :=0
    

    Now I need to work out how to get a specific pad to trigger the chase rather than any pad.
  • skylightskylight Posts: 1,915
    edited 2011-11-27 16:29
    @Jon thanks for your solution i'm going to try it, it is a little bit advanced for me to understand fully at the moment as i'm taking things step by step slowly but i will study it carefully and any bit I dont understand I will ask about.- cheers
  • bsnutbsnut Posts: 521
    edited 2011-11-27 23:26
    Here's is one way you can code it for what you want to do.
    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] := 0
    
    All 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.
  • skylightskylight Posts: 1,915
    edited 2011-11-28 04:22
    Thanks bsnut, I had tried using the INA command before but due to wrong syntax couldn't get it to work, so resorted to using the touch button demo, you have shown me the correct way and i am now able to select control from any pad I choose to use :smile:

    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
  • AribaAriba Posts: 2,690
    edited 2011-11-28 05:12
    On the Quickstart you can not just read the button states at the pins. First you need to write a High to a button, and then wait a short time. If the button is touched the pin goes low otherwise it stays high.
    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] := 0
    

    Andy
  • skylightskylight Posts: 1,915
    edited 2011-11-28 05:21
    Thanks Ariba for your reply, I tried your code but the chase runs continuously the buttons make no difference?
  • AribaAriba Posts: 2,690
    edited 2011-11-28 05:42
    OK, I should test the code before I post ;-)

    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 5ms
    

    You need to touch the most right button !

    Andy
  • skylightskylight Posts: 1,915
    edited 2011-11-28 05:53
    Thanks Ariba, that works perfectly without the anomaly as in my other post which I will post now as solved and link it to this post
  • bsnutbsnut Posts: 521
    edited 2011-11-28 06:18
    Thanks Ariba for for getting the code to work, which was a base line to work from.
Sign In or Register to comment.