New to Propeller...needing help with simple functions
eagletalontim
Posts: 1,399
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 :
I am not even sure if i am calling the PUB or function correctly or if the variable is even being passed.
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
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.
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.
Look at the REPEAT command in the Prop manual, page 188. It talks about the use of REPEAT WHILE.
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 )