LED PBASIC problem
hp16
Posts: 11
Hello, I'm new to this, I'm trying to learn PBASIC right now to do basic commands like: turn on LED, turn it off, etc.
I was working on a command on which, with the push of a button, will turn an LED on for 10 seconds, if I push the button again, while the LED is still on, it will blink for 10 seconds at a rate of one per second (on for .5 seconds, off for .5 seconds). But if the button is pressed again before the LED blinks 10 times it will stay on for 10 seconds. This is what I had so far but according to my professor, I'm wrong:
counter VAR Byte
For counter = 1 to 10
DEBUG ? counter
IF (IN3=1) THEN
HIGH 14
PAUSE 500
LOW 14
PAUSE 500
NEXT
ELSE
HIGH 14
PAUSE 1000
NEXT
DEBUG "All Done"
END
I was working on a command on which, with the push of a button, will turn an LED on for 10 seconds, if I push the button again, while the LED is still on, it will blink for 10 seconds at a rate of one per second (on for .5 seconds, off for .5 seconds). But if the button is pressed again before the LED blinks 10 times it will stay on for 10 seconds. This is what I had so far but according to my professor, I'm wrong:
counter VAR Byte
For counter = 1 to 10
DEBUG ? counter
IF (IN3=1) THEN
HIGH 14
PAUSE 500
LOW 14
PAUSE 500
NEXT
ELSE
HIGH 14
PAUSE 1000
NEXT
DEBUG "All Done"
END
Comments
It's hard to know how much help would be appropriate to offer on a school assignment.
I will suggest you should make sure the button has been released before you start looking to see if it has been pressed again. Otherwise you wont know if you are detecting a new button press or if the first press was just held down for awhile.
There's a link in post #3 of my index (see my signature) to a tutorial on how to post code to the forum. It will be easier to help you if you use the code tags mentioned in the tutorial.
Give the code another go and post what you have and what it does and I bet someone around here will offer another hint (if you still need one).
BTW, Welcome to the forums.
In a nutshell, you can't use pause statements. Instead, you must make some timed loops (either a half second or ten seconds) to continuously look at your pushbutton to decide what to do while the LED is doing its thing. In essence, you'll have three seperate loops within your program. First is LED off, looking for a button press. Second is blinking the LED for 10 secs while looking for a button press, and the third is LED on for ten seconds while looking for a button press.
Hopefully you have a Stamp you can experiment with "live", because until you achieve a certain amount of proficiency, writing code "blind" (without constant testing) is quite difficult.
Then how am I going to press the button again if there's only two choices for the button: button on, button off.
If anyone can guide me to a tutorial or something about LEDs and buttons I'll be very grateful. I have to turn this in on Tuesday and I have no idea what I'm doing.
DO
IF button pressed THEN
turn on LED
For counter 1 to 200 ms
wait 50 ms
IF button pressed
(blink)
ELSE button not pressed
wait 50 ms
END LOOP
Something to think about ... What happens when testPin is low for more than about 25 seconds? If that's not the behavior you want, how would you fix it?
Well, that kind of takes the fun out of it. Darn.
Learning this stuff is fun if you can see the effect on the output caused by small changes to the code. Trying to come up with code without being able to test small sections just isn't nearly as much fun.
Scour the Radio shacks near you to see if they have any of their clearance "Stamp Activity kits" . Just $10 or $15 for a NICE kit. Ridiculously cheap, includes a BS2 Homework Board.
http://parallax.com/product/90005
I've got this code but when I press the button it starts blinking first for 10 seconds and after blinking 10 seconds it stays on for 10 seconds. What am I doing wrong?
IF and ENDIF should align, FOR and NEXT should align, and your DO and LOOP are already aligned.
You now see everything that will run in the DO LOOP and first you have an IF that will turn on the light.
This first IF needs and ENDIF to stop it since you have more than one statement under it.
Then you have a FOR NEXT.
You need to understand that the FOR NEXT will complete before anything else can happen.
Is this what you want to happen?
Look at how Mike did it.
Also if you place a FOR NEXT inside of a FOR NEXT the inner FOR NEXT runs the number of times the outer FOR NEXT runs.
Is this what you want to happen?
You are getting there.
Do you notice something about the time the LED should stay on or blink?
Look again at Mike's program and what he does.
More importantly though is do you understand what your program needs to do?
Also, don't be afraid to ask questions if there is something that you don't understand.
Also the logic of your program is still not correct. Did you notice that LED will be on or blink for the same amount of time? Perhaps you should be using the same timing loop for both conditions.
And how does your program know whether the LED is blinking or steady? What if someone kept pressing the button over and over again?
Same as before, you don't want to use a FOR-NEXT unless you have a way to EXIT from it if conditions should change. Look at Mike's program and see how he makes does the same thing without a FOR-NEXT.
Have you tried making a Flow Chart or a diagram of how your program should function?