For-Next loops
Dr. Resonance
Posts: 12
I've been a basic programmer for 20 years, and familiar with the standard For ... Next loop, but just starting out in PBasic, using a Stamp II for a room controller in a neighborhood haunted house project,
and seem to be "stuck" on the proper use of a FOR ..... Next loop in PBasic.
My code snippet to control a LED when switch 2 is pressed is:
Indicate2
····· DEBUG "switch 2 pressed"
····· FOR I=0 TO 240
····· HIGH 6······················ 'turns on blue LED connected to pin 6
····· PAUSE 250
····· LOW 6······················· 'turns off blue LED connected to pin 6
····· PAUSE 250
····· NEXT I
The problem I've encountered is I keep getting a high-lighted letter 'I" and error code "expected a variable" every time I try to run the code.
Also, I have 3 separate input pushbuttons and would like to know what's proper code for a "debounce" on the switch contacts.
I thought perhaps something like:
Main
····· DEBUG "Intialize Inputs"··································' ---- not sure if this section is required but I'm trying to intialize inputs and outputs prior to the main part of the program
····· IN0=1··········· 'sets pin o to a 5 volt condition
····· IN1=1··········· 'sets pin 1 to a 5 volt condition
····· IN2=1··········· 'sets pin 2 to a 5 volt condition
····· DEBUG "Intialize Outputs"
····· OUT3=0·········· 'sets pin3 = 0 volts
····· OUT4=0·········· 'sets pin4 = 0 volts
····· OUT5=0·········· 'sets pin5 = 0 volts
····· OUT6=0·········· 'sets pin6 = 0 volts
····· OUT7=0·········· 'sets pin7 = 0 volts
····· OUT8=0·········· 'sets pin8 = 0 volts
····· OUT9=0·········· 'sets pin9 = 0 volts
····· OUT10=0········· 'sets pin10 = 0 volts
····· OUT11=0········· 'sets pin11 = 0 volts
····· OUT12=0········· 'sets pin12 = 0 volts
····· OUT13=0········· 'sets pin13 = 0 volts
····· OUT14=0········· 'sets pin14 = 0 volts
····· OUT15=0········· 'sets pin15 = 0 volts
··· CheckSwitch1
····· IF IN0=0 THEN Indicate1····· 'checks to see if switch 1 is depressed
····· IF IN1=0 THEN Indicate2····· 'checks to see if switch 2 is depressed
····· IF IN2=0 THEN Indicate3····· 'checks to see if switch 3 is depressed
or, should I add a pause 300 line between section under Checkswitch1
Many thanks for the help.· Trying to help a neighbor 14 yr old kid with his haunted "garage" project and didn't expect a simple For ... Next loop to be so problematic.
D.C. Cox·· aka· Dr. Resonance
·
and seem to be "stuck" on the proper use of a FOR ..... Next loop in PBasic.
My code snippet to control a LED when switch 2 is pressed is:
Indicate2
····· DEBUG "switch 2 pressed"
····· FOR I=0 TO 240
····· HIGH 6······················ 'turns on blue LED connected to pin 6
····· PAUSE 250
····· LOW 6······················· 'turns off blue LED connected to pin 6
····· PAUSE 250
····· NEXT I
The problem I've encountered is I keep getting a high-lighted letter 'I" and error code "expected a variable" every time I try to run the code.
Also, I have 3 separate input pushbuttons and would like to know what's proper code for a "debounce" on the switch contacts.
I thought perhaps something like:
Main
····· DEBUG "Intialize Inputs"··································' ---- not sure if this section is required but I'm trying to intialize inputs and outputs prior to the main part of the program
····· IN0=1··········· 'sets pin o to a 5 volt condition
····· IN1=1··········· 'sets pin 1 to a 5 volt condition
····· IN2=1··········· 'sets pin 2 to a 5 volt condition
····· DEBUG "Intialize Outputs"
····· OUT3=0·········· 'sets pin3 = 0 volts
····· OUT4=0·········· 'sets pin4 = 0 volts
····· OUT5=0·········· 'sets pin5 = 0 volts
····· OUT6=0·········· 'sets pin6 = 0 volts
····· OUT7=0·········· 'sets pin7 = 0 volts
····· OUT8=0·········· 'sets pin8 = 0 volts
····· OUT9=0·········· 'sets pin9 = 0 volts
····· OUT10=0········· 'sets pin10 = 0 volts
····· OUT11=0········· 'sets pin11 = 0 volts
····· OUT12=0········· 'sets pin12 = 0 volts
····· OUT13=0········· 'sets pin13 = 0 volts
····· OUT14=0········· 'sets pin14 = 0 volts
····· OUT15=0········· 'sets pin15 = 0 volts
··· CheckSwitch1
····· IF IN0=0 THEN Indicate1····· 'checks to see if switch 1 is depressed
····· IF IN1=0 THEN Indicate2····· 'checks to see if switch 2 is depressed
····· IF IN2=0 THEN Indicate3····· 'checks to see if switch 3 is depressed
or, should I add a pause 300 line between section under Checkswitch1
Many thanks for the help.· Trying to help a neighbor 14 yr old kid with his haunted "garage" project and didn't expect a simple For ... Next loop to be so problematic.
D.C. Cox·· aka· Dr. Resonance
·
Comments
Post Edited (Mikerocontroller) : 10/26/2008 3:21:52 AM GMT
····· DEBUG "switch 2 pressed"
····· FOR I=0 TO 240
····· HIGH 6······················ 'turns on blue LED connected to pin 6
····· PAUSE 250
····· LOW 6······················· 'turns off blue LED connected to pin 6
····· PAUSE 250
····· NEXT
I switched it to just NEXT and it still locks up on the I in the 2nd line, "FOR I = 0 TO 240
So, now what should I try next??
Dr. Resonance
I VAR Byte
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Richard Vannoy
Programming and Electronics Instructor
www.PICRobots.com
www.SailingStrategy.info
www.TeachingTechnicalTraders.com
·
example:
· I······· VAR······ Byte
· If possible, could you post the entire code?
DEBUG "STAMP RESET"
That message should only appear once per iteration of the program. If you see it more than once, the Stamp is resetting.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When all else fails, try inserting a new battery.
Post Edited (Bruce Bates) : 10/26/2008 7:31:53 AM GMT
I forgot to post I VAR byte in my intialization process.
Also, wondering, do I need to declare my inputs and outputs like I did?
Another questions: If I do NOT use a GOSUB command (as in my code example), do I need the colon or will it run properly as shown?· After running thru the For .... NEXT loop I presume the
program would just drop down to the next line and keep running.
For inspection, entire code as follows:
' {$STAMP BS2}
··· Main
····· DEBUG "Intialize Inputs"
····· IN0=1··········· 'sets pin o to a 5 volt condition
····· IN1=1··········· 'sets pin 1 to a 5 volt condition
····· IN2=1··········· 'sets pin 2 to a 5 volt condition
····· DEBUG "Intialize Outputs"
····· OUT3=0·········· 'sets pin3 = 0 volts
····· OUT4=0·········· 'sets pin4 = 0 volts
····· OUT5=0·········· 'sets pin5 = 0 volts
····· OUT6=0·········· 'sets pin6 = 0 volts
····· OUT7=0·········· 'sets pin7 = 0 volts
····· OUT8=0·········· 'sets pin8 = 0 volts
····· OUT9=0·········· 'sets pin9 = 0 volts
····· OUT10=0········· 'sets pin10 = 0 volts
····· OUT11=0········· 'sets pin11 = 0 volts
····· OUT12=0········· 'sets pin12 = 0 volts
····· OUT13=0········· 'sets pin13 = 0 volts
····· OUT14=0········· 'sets pin14 = 0 volts
····· OUT15=0········· 'sets pin15 = 0 volts
····· I VAR Byte
··· CheckSwitch1
····· IF IN0=0 THEN Indicate1····· 'checks to see if switch 1 is depressed
····· IF IN1=0 THEN Indicate2····· 'checks to see if switch 2 is depressed
····· IF IN2=0 THEN Indicate3····· 'checks to see if switch 3 is depressed
····· GOTO CheckSwitch1
··· Indicate1
····· DEBUG "Switch 1 pressed"
····· HIGH 5······················ 'turns on red LED connected to pin 5
····· PAUSE 5000
····· LOW 5······················· 'turns off red LED connected to pin 5
····· GOTO CheckSwitch1
··· Indicate2
····· DEBUG "switch 2 pressed"
····· FOR I=0 TO 240
····· HIGH 6······················ 'turns on blue LED connected to pin 6
····· PAUSE 250
····· LOW 6······················· 'turns off blue LED connected to pin 6
····· PAUSE 250
····· NEXT························ 'returns to start of loop
··· GOTO CheckSwitch1············· 'returns TO start TO check FOR a switch presse
··· Indicate3
····· DEBUG "switch 3 pressed"
····· FOR I=1 TO 120
····· HIGH 4·························· 'turns on white LED connected to pin 4
····· PAUSE 250
····· LOW 4··························· 'turns off white LED connected to pin 4
····· PAUSE 250
····· NEXT···························· 'returns to start of loop
··· Indicate4
····· FOR I=1 TO 120
····· HIGH 5·························· 'turns on green LED connected to pin 5
····· PAUSE 250
····· LOW 5··························· 'turns off green LED connected to pin 5
····· PAUSE 250
····· NEXT···························· 'returns to start of loop
··· Indicate5
····· FOR I=1 TO 120
····· HIGH 6·························· 'turns on blue LED connected to pin 6
····· PAUSE 250
····· LOW 6··························· 'turns off blue LED connected to pin 6
····· PAUSE 250
····· NEXT···························· 'returns to start of loop
··· Indicate6
····· FOR I=1 TO 120
····· HIGH 5·························· 'turns on green LED connected to pin 5
····· PAUSE 250
····· LOW 5··························· 'turns off green LED connected to pin 5
····· PAUSE 250
····· HIGH 6·························· 'turns on blue LED connected to pin 6
····· PAUSE 250
····· LOW 6··························· 'turns off blue LED connected to pin 6
····· PAUSE 250
····· HIGH 4·························· 'turns on white LED connected to pin 4
····· PAUSE 250
····· LOW 4··························· 'turns off white LED connected to pin 4
····· PAUSE 250
····· NEXT···························· 'returns to start of loop
··· Indicate7
····· FOR I=1 TO 120
····· HIGH 4·························· 'turns on white LED connected to pin 4
····· HIGH 5·························· 'turns on green LED connected to pin 5
····· HIGH 6·························· 'turns on blue LED connected to pin 6
····· PAUSE 500
····· LOW 4··························· 'turns off white LED connected to pin 4
····· LOW 5··························· 'turns off green LED connected to pin 5
····· LOW 6··························· 'turns on blue LED connected to pin 6
····· PAUSE 500
····· NEXT···························· 'returns to start of loop
··· DEBUG "end of program"
··· END
Jim-
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Signature space for rent, only $1.
Send cash and signature to CannibalRobotics.
·· example:
·· ' {$STAMP BS2}
···' {$PBASIC 2.5}
I've attch. a lil. program that I think will be of some help.I also recomend "StampWorks" from Parallax, This is A really good read, I think a MUST when gettin started w./ the BS2!!!!!!!!!!:If You have some more ?? just drop a post reply:I added a few REM statments to the 2nd upload
____________$WMc%__________________
Post Edited ($WMc%) : 10/26/2008 8:43:49 PM GMT
I was wondering where everyone got the weird brackets as I could not find them on my keyboard.
Regards,
Dr. Resonance