increment or decrement led oscillation speed
ElectronsRfun
Posts: 5
Hello All,
I am new to micro-processors and had a question about the propeller in Spin. I am working through the PE Kit and wanted to try a combination of lessons into one project. I have 2 leds oscillating and am trying to set the oscillation between 1 second and 10 seconds. The code I wrote kinda works but when the push buttons are pressed the oscillation speed changes more than by 1 second and the code seems to have to wait for the current code to finish before the increment or decrement takes place. What I would like to have happen is the oscillation speed change by one second incremently or decremently by on second each button press. Also I would like for the pressed button to change the oscillation speed immediately. What I think I need to do is use cogstop then cogstart or cognew to initiate a new cog every time one of the push buttons are pressed and pass the parameters between the methods I write. Am I on the correct path?
Here is the code so far:
I am new to micro-processors and had a question about the propeller in Spin. I am working through the PE Kit and wanted to try a combination of lessons into one project. I have 2 leds oscillating and am trying to set the oscillation between 1 second and 10 seconds. The code I wrote kinda works but when the push buttons are pressed the oscillation speed changes more than by 1 second and the code seems to have to wait for the current code to finish before the increment or decrement takes place. What I would like to have happen is the oscillation speed change by one second incremently or decremently by on second each button press. Also I would like for the pressed button to change the oscillation speed immediately. What I think I need to do is use cogstop then cogstart or cognew to initiate a new cog every time one of the push buttons are pressed and pass the parameters between the methods I write. Am I on the correct path?
Here is the code so far:
CON
pin1 = 0
pin2 = 1
pb1 = 21
pb2 = 20
VAR
long stack[20]
long speed
PUB Main
cognew(Blink, @stack[0])
cognew(Button, @stack[10])
PUB Blink
dira[pin1..pin2]~~
outa[pin1..pin2]~
speed := 1
repeat
!outa[pin1]
waitcnt(clkfreq * speed + cnt)
!outa[pin1]
!outa[pin2]
waitcnt(clkfreq * speed + cnt)
!outa[pin2]
PUB Button
repeat
if ina[pb1] == 0
waitcnt(clkfreq/6 + cnt)
speed ++
speed <#= 10
elseif ina[pb2] == 0
waitcnt(clkfreq/6 + cnt)
speed --
speed #>= 1

Comments
Hard to tell from what you posted but it looks like you are not de-bouncing the push buttons in your code. That will cause the speed to be incremented more than once. Can you post all your code between {code} and {/code} lines but use the square [ ] brackets instead of the curly { } brackets?
Thanks for your reply. I am not sure how to correctly copy and paste the code I wrote into the Blog Post. I am also not sure what you meant by; "Can you post all your code between {code} and {/code} lines but use square [ ] brackets instead of the curly { } brackets."
What is {/code} is that short for "not code"?
Could you write a short example please?
I read in the PE manual the push buttons included with the PE kit don't have to be de-bounced. I am not sure if the author meant the simple programs written don't need to be de-bounced or it the push buttons themselves don't bounce.
I am not sure how to de-bounce a micro-controller circuit, (I usually use a 555 in mono-stable mode). I messed around with controlling switch bounce a little but I am not sure my approach was correct. Not sure weather to use REPEAT WHILE or REPEAT UNTIL in combination or some WAITCNT commands like WAITPEQ. Below is some of the code I wrote to use on my propeller demo board that worked with a case statement: The code is suppose to turn on the leds one at a time in secession without switch bounce. I tried to fix all the indentation on the code in the repeat portion to make it easier to read, I hope that helps until I learn the correct way to post code.
I was able to solve my original problem by taking a different approach. I would post my solution but it involves objects and I don't know how to post all the objects that were used to make the whole thing work. The solution I used only uses one pin (ina[pin]) instead of two (like in the increment and decrement code.) I would still like to solve the increment and decrement code though. I don't gain anything by not learning.
CODE:
CON led1 = 16 led2 = 17 led3 = 18 led4 = 19 led5 = 20 led6 = 21 led7 = 22 led8 = 23 pb1 = 0 pb2 = 1 VAR BYTE LED PUB main dira [16..23]~~ repeat IF ina [pb1] == 1 LED := LED + 1 IF LED == 9 LED := 0 waitcnt(clkfreq/3 +cnt) waitpeq(00, |< 0, 1) {{think what this says is pin 0 in binary goes to pwr then wait }} waitpeq(00, |< 0, 0) waitcnt(clkfreq/6 +cnt) CASE LED 0 : outa [led1..led8] := 0 1 : outa [led1] := 1 outa [led2..led8]~ 2 : outa [led1] := 1 outa [led2] := 1 outa [led3..led8]~ 3 : outa [led1..led3]~~ outa [led4..led8]~ 4 : outa [led1..led4]~~ outa [led5..led8]~ 5 : outa [led1..led5]~~ outa [led6..led8]~ 6 : outa [led1..led6]~~ outa [led7..led8]~ 7 : outa [led1..led7]~~ outa [led8]~ 8 : outa [led1..led8]~~