LED PBASIC problem
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
' {$STAMP BS2} ' {$PBASIC 2.5} timeSec10 var word ' time in 1/10ths of a second timeMin var word ' time in minutes timeCntr var byte ' temporary time counter testPin pin 1 ' I/O pin to be checked timeMin = 0 timeCntr = 0 do ' main loop timeSec10 = 0 ' start over counting 1/10s of a second do ' each loop takes around a 1/10th of a second if testPin = 0 then ' I/O pin is low? if timeCntr = 0 then ' I/O pin just turned low ' here you might turn something on endif if timeCntr = 10 then ' I/O pin has been low for a second ' here you would turn something off endif timeCntr = timeCntr + 1 ' increment counter else timeCntr = 0 ' I/O pin not low, reset counter ' you also need to turn off the something here endif pause 100 ' this is what sets most of the time "tick" timeSec10 = timeSec10 + 1 loop while timeSec10 < 600 timeMin = timeMin + 1 loopThe amount of time it takes to execute the code other than the pause can take several milliseconds, so you will have to calibrate the program. You'll have to use an accurate clock to measure the actual time it takes to count timeMin up to, say, 30 minutes and decrease the pause time enough to make the 30 minute time work out properly.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?
' {$STAMP BS2} ' {$PBASIC 2.5} counter VAR byte DO IF (IN3 = 1) THEN HIGH 14 FOR counter 1 to 200 PAUSE 50 {IF (IN3 = 1) THEN FOR 1 to 10 PAUSE 50 DEBUG ? counter HIGH 14 PAUSE 500 LOW 14 PAUSE 500 NEXT } ELSE (IN3=0) PAUSE 50 LOOP END LOOPWell, 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?
DO IF(IN3 = 1) THEN FOR counter = 1 TO 200 DEBUG ? counter HIGH 14 PAUSE 50 IF(IN3 = 1) THEN FOR counter = 1 TO 10 DEBUG ? counter HIGH 14 PAUSE 500 LOW 14 PAUSE 500 NEXT ENDIF NEXT ELSE PAUSE 50 ENDIF LOOPIF and ENDIF should align, FOR and NEXT should align, and your DO and LOOP are already aligned.
DO IF(IN3 = 1) THEN FOR counter = 1 TO 200 DEBUG ? counter HIGH 14 PAUSE 50 IF(IN3 = 1) THEN FOR counter = 1 TO 10 DEBUG ? counter HIGH 14 PAUSE 500 LOW 14 PAUSE 500 NEXT ENDIF NEXT ELSE PAUSE 50 ENDIF LOOPYou 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.
' {$STAMP BS2} ' {$PBASIC 2.5} counter VAR Byte 'declaring my variable DEBUG ? IN3 'check if button has been pressed DO IF(IN3 = 1) THEN 'if I press the button here it's supposed to stay on for 10 seconds stayon: HIGH 14 FOR counter = 1 TO 200 DEBUG ? counter PAUSE 50 'the reason I have pause 50 ms is because if I wait for a longer time and if I press the button while it's waiting my program is not going to work efficiently IF(IN3 = 1) THEN GOSUB blink 'this is my subroutine. it jumps to the code that is at the bottom ELSE END ENDIF NEXT ELSE LOW 14 PAUSE 50 ENDIF LOOP blink: 'this is the command where it's going to blink 10 times FOR counter = 1 TO 10 DEBUG ? counter LOW 14 FOR counter = 1 TO 10 DEBUG ? counter PAUSE 50 IF(IN3 = 1) THEN GOSUB stayon 'sends it back to stayon: if the button is pressed ELSE END ENDIF NEXT HIGH 14 FOR counter = 1 TO 10 DEBUG ? counter PAUSE 50 IF(IN3 = 1) THEN GOSUB stayon 'sends it back to stayon: if the button is pressed ELSE END ENDIF NEXT NEXTAlso 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?