Loop control
JOSHT
Posts: 8
Hello, I am new to the forum and Also new to spin language. I am having some problems with loops I am trying to create. I have had some luck with them. But I still cannot get this one to works. I am trying to make an LED turn on and then I want to press a button 10 and after the tenth press have the button turn off. Here is some of the code I have written. It doesn't work. I have tried a few different ways and no such luck. Any help would be greatly appreciated.
Thanks,
-Josh
CON
_CLKMODE=XTAL1 + PLL2X
_XINFREQ = 5_000_000
INPUT_PIN =23
INV_HIGH =0
INV_LOW =1
WAITPERIOD =5_000_000
LED0 =2
VAR
LONG X
PUB GO0
DIRA [LED0]~~
OUTA [LED0]~~
DIRA [INPUT_PIN]~
REPEAT
TURNON_LED0
WAIT
WHILE X=>10
TURNOFF_LED0
WAIT
IF INA[INPUT_PIN]==0
X++
PRI TURNON_LED0
OUTA[LED0] :=INV_HIGH
PRI TURNOFF_LED0
OUTA[LED0] :=INV_LOW
PRI WAIT
WAITCNT(WAITPERIOD + CNT)
Thanks,
-Josh
CON
_CLKMODE=XTAL1 + PLL2X
_XINFREQ = 5_000_000
INPUT_PIN =23
INV_HIGH =0
INV_LOW =1
WAITPERIOD =5_000_000
LED0 =2
VAR
LONG X
PUB GO0
DIRA [LED0]~~
OUTA [LED0]~~
DIRA [INPUT_PIN]~
REPEAT
TURNON_LED0
WAIT
WHILE X=>10
TURNOFF_LED0
WAIT
IF INA[INPUT_PIN]==0
X++
PRI TURNON_LED0
OUTA[LED0] :=INV_HIGH
PRI TURNOFF_LED0
OUTA[LED0] :=INV_LOW
PRI WAIT
WAITCNT(WAITPERIOD + CNT)
Comments
I haven't tested it but it should work. I'm not sure if the button [23] should be declared input or output.
Thanks,
-Josh
Your going to want to keep track of the previous button state so you can count how many times the old state was unpressed and the new state is pressed.
I think Larry's code will turn off the LED if the button if just held down.
I've got some work to do right now. If you haven't got this figured out by the time I'm done with my work, I'll try to help some more. (I may be tomorrow.)
BTW, welcome to the forum and the wonderful world of Propellers.
It will help others help you if you learn how to post code in the forum. Follow this link.
[HTML]
VAR byte index
PUB TnBtnPrsses
dira[2] := 1
dira[23] := 1
outa[2] := 0
repeat 10
repeat while ina[23]
index++
outa[2] := 1
outa[2] := 0[/HTML]
I modified the statement a bit. It compiles but it is still untested. I'm a student too.
EDIT: You posted while I was typing. I haven't tried your second code. My guess is it wouldn't work they why you're hoping. It would also turn off the LED too fast. (You also make the button an output instead of an input.)
2nd EDIT: Josh, is your button normally high? The code I posted below assumes so. If not, change the "== 0" to "== 1" and change "oldState := 1" to "oldState := 0".
I came up with some code but Josh, don't look at it yet.
Try to come up with some working code with these clues.
Keep track of the oldState of the button. Compare it with the new state to see if they are different (you've either pressed or released the button).
If it's different check to see it the button has just been pressed (and not just released).
If the button has just been pressed increment your counter (X).
Start your loop with repeat. At the bottom of the loop use:
Oh, you don't need the method "WAIT".
Good luck.
Not stop reading and start programming!
.
.
.
.
.
(Back of the book answer.)
I like this program. I only get 8 presses before the LED turns off. I think it's because: The first time the button is pressed you start the loop. Since it is likely the button will still be pressed when the loop starts the second "repeat while" statement doesn't delay the program. If you add another half second delay prior to the "repeat 9" statement, you'll have time to lift your finger off the button.
The button you're using is normally high? And a press grounds the button?
I think if you add the delay and change the repeat from 9 to 10, then it will work they way you'd like it to.
I personally don't like a Propeller program to completely end. I usually add a repeat at the end to keep the cog active. I think this is just because it's what I've seen other's on the forum do. Often a LED is supposed to be left on (I think I see more test programs that turn a LED on when a condition is met than programs that turn off a LED) and without a final repeat you wouldn't know the program had worked properly.
I agree with you. This is fun stuff.
Ah, yes. I used to have the problem. I finally ordered ten of these and made a little Propeller board with power supply that was easy to transfer from breadboard to breadboard.
Your program needs a button to break out of the "repeat while" loops. Since you're using a terminal program you could change:
to:
Oh good, HyperTerminal's rxcheck is public. The rxcheck method in Parallax Serial Terminal is private. (Why in the world did Parallax make it private?)
Just a second. I want to test this to make sure it works. Okay, it works. So now you should be able to try it without a button.
My many breadboards came in handy this last Monday. I taught nine Cub Scouts how to wire up a ciruit on the boards with a wire, a resistor and a LED. I had enough breadboards so each Cub Scout could use one. It was fun to see how excited they got when their LED was finally lit.
@Josh, Are you still with us? How goes the programming? Fun stuff, right? Keep the questions coming.
I'd like to, but there isn't enough time in the "Pack Activity" to do so. I'm not sure if all the boys are interested robots enough to want to learn to program.
I might have a mini robot school at my house on a couple of Saturdays each month. This way, the kids who are interested, could learn more if they want.