Shop OBEX P1 Docs P2 Docs Learn Events
New to Propeller...needing help with simple functions — Parallax Forums

New to Propeller...needing help with simple functions

eagletalontimeagletalontim Posts: 1,399
edited 2009-10-31 04:10 in Propeller 1
First.....

I want to create a function or "PUB" that I can send a call when needed and send a variable to that will toggle LED's in a certain patten. When a user pushes a button, it will change a global variable. That variable will then be passed to a sub or function and will change which LED's are on. The problem I have now is outa[noparse][[/noparse]MyPin] := 1 and outa[noparse][[/noparse]MyPin] := 0 is not working like I thought it would. Once the pin state changes to output, I was hoping to have the LED stay on, then the program return back to the main function. It just flashes [noparse]:([/noparse]

And to ensure I am doing this correctly, here is an example of how my code is :


PUB Main
  npattern := 1
  dira[noparse][[/noparse]MyPin]~~
  repeat
    if ina[noparse][[/noparse]button1] == 1
      npattern ++
      npattern := toggleleds(npattern)
    if ina[noparse][[/noparse]button2] == 1
      npattern --
      npattern := toggleleds(npattern)   

PUB toggleleds(tmp)
  if tmp > 4
    tmp := 4
  if tmp < 1
    tmp := 1

  if tmp == 1
    outa[noparse][[/noparse]MyPin] := 1
    outa[noparse][[/noparse]MyPin] := 0
  if tmp == 2
    outa[noparse][[/noparse]MyPin] := 0
    outa[noparse][[/noparse]MyPin] := 1
  if tmp == 3
    outa[noparse][[/noparse]MyPin] := 0
    outa[noparse][[/noparse]MyPin] := 0
  
  return tmp





I am not even sure if i am calling the PUB or function correctly or if the variable is even being passed.

Comments

  • eagletalontimeagletalontim Posts: 1,399
    edited 2009-10-31 02:38
    ok, I am completely lost!!! Does anyone know how to make an LED come on and stay on without having to repeat that section of code to keep the LED on? If so, I need an example on how to do this.
  • Mike GreenMike Green Posts: 23,101
    edited 2009-10-31 02:39
    The way you've written it, the LED will flash pretty quickly. An "outa[noparse][[/noparse] MyPin ] := 0" will execute in about 1us.

    Why don't you start with a description of what you want the LEDs (and the program) to do.

    The basic logic for the program itself looks ok although the pattern number is going to change really quickly any time you push a button. Most of the time, what you want is to check for a button being released before you check again to see if it's pressed. Think about how you might do that. Hint: you need more than one REPEAT loop.
  • Mike GreenMike Green Posts: 23,101
    edited 2009-10-31 02:41
    Once you turn the LED on (by setting the corresponding bit in OUTA (and DIRA) to one, it will stay on until the program turns it off (or you reset the Propeller). Similarly, once you turn an LED off, it will stay off until the program turns it on.
  • eagletalontimeagletalontim Posts: 1,399
    edited 2009-10-31 02:44
    I messed up in my code above.... It should be like this :

    outa[noparse][[/noparse]MyPin1] :=1
    outa[noparse][[/noparse]MyPin2] :=0
    
    
  • eagletalontimeagletalontim Posts: 1,399
    edited 2009-10-31 02:46
    It looks like the toggleled(npattern) is not sending the variable correctly for some reason. It seems to cycle between 1 and 4, not 1, 2, 3, 4 and 4, 3, 2, 1 like it should when the user would press the corresponding button.
  • Mike GreenMike Green Posts: 23,101
    edited 2009-10-31 02:56
    You also need to have a DIRA for MyPin1 and MyPin2.

    You still need some way to make your program react only to a change from button off to button on. The way you've written it, npattern will get changed and toggleled will get called several times (maybe a couple of thousand times) when you push either button. Remember that the button contacts will stay closed for many milliseconds even if you tap it. There'll also be some contact "bounce" with most types of switches where the contacts will repeatedly close and open many times before they settle into one state or the other (closed or open). Your program has to deal with this. Start with the Wikipedia: en.wikipedia.org/wiki/Switch.

    Your program looks like it's sending its parameter and receiving the result from toggleled the way you expect. The problem is the basic logic of your program, particularly the handling of the switches.
  • eagletalontimeagletalontim Posts: 1,399
    edited 2009-10-31 03:01
    that was it! I added a waitcnt to the if ina[noparse][[/noparse]button1] and it fixed it [noparse]:)[/noparse] Thanks for pointing that out. This is day 2 of me tinkering with making it a total of 2 or 3 hours total time to learn this and it does not seem that bad [noparse]:)[/noparse]
  • eagletalontimeagletalontim Posts: 1,399
    edited 2009-10-31 03:07
    is there a way to use a while loop to loop while a button is pressed? I looked up the while loop in the Help file, but it does not explain a whole lot about each command. It just shows them in the code.
  • eagletalontimeagletalontim Posts: 1,399
    edited 2009-10-31 03:12
    never mind....i figured it out [noparse]:)[/noparse]

    repeat while ina[noparse][[/noparse]button1] == 1 or ina[noparse][[/noparse]button2] == 1
       waitcnt(1_000)
    
    
  • ElectricAyeElectricAye Posts: 4,561
    edited 2009-10-31 03:16
    eagletalontim said...
    is there a way to use a while loop to loop while a button is pressed? I looked up the while loop in the Help file, but it does not explain a whole lot about each command. It just shows them in the code.

    Look at the REPEAT command in the Prop manual, page 188. It talks about the use of REPEAT WHILE.
  • Mike GreenMike Green Posts: 23,101
    edited 2009-10-31 03:18
    I think you'll find that what you posted doesn't do what you want.

    One thing ... WAITCNT waits until the system clock (CNT) equals the number that you supply. It doesn't wait for the amount of time you supply.

    To wait 100ms (1/10 sec), you divide CLKFREQ by the fraction of a second you want to wait, then you add the current time value like:

    WAITCNT( CLKFREQ / 10 + CNT )
  • Mike GreenMike Green Posts: 23,101
    edited 2009-10-31 03:23
    For debouncing, you may want to do something like:
    if ina[noparse][[/noparse]button] == 1   ' The button is pushed
       ' do something when the button is pushed
       waitcnt(clkfreq/50 + cnt)   ' if "something" takes less than 20ms, allow for debouncing
       repeat while ina[noparse][[/noparse]button] == 1  ' Wait for the button to be released
          waitcnt(clkfreq/50 + cnt)   ' Allow 20ms for button debounce, then we're ready for more
    
  • eagletalontimeagletalontim Posts: 1,399
    edited 2009-10-31 04:10
    Thanks for pointing that out! I have changed my code. I will get this soon enough [noparse]:)[/noparse]
Sign In or Register to comment.