Shop OBEX P1 Docs P2 Docs Learn Events
For-Next loops — Parallax Forums

For-Next loops

Dr. ResonanceDr. Resonance Posts: 12
edited 2008-10-27 22:14 in BASIC Stamp
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


·

Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2008-10-26 02:46
    It's not NEXT I, it should be NEXT.
  • MikerocontrollerMikerocontroller Posts: 310
    edited 2008-10-26 03:06
    · The IN(S,L,H,0,1,2,3,4,5,6,7,8.....etc.) variable is READ-ONLY.· It reflects the current state of the pin or pins being referenced.· The state of the pins (when in INPUT mode) is set by your external circuitry.

    Post Edited (Mikerocontroller) : 10/26/2008 3:21:52 AM GMT
  • Dr. ResonanceDr. Resonance Posts: 12
    edited 2008-10-26 05:57
    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 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
  • RoboticsProfessorRoboticsProfessor Posts: 54
    edited 2008-10-26 06:23
    How about declaring "I" at the top with...

    I VAR Byte

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Richard Vannoy

    Programming and Electronics Instructor
    www.PICRobots.com
    www.SailingStrategy.info
    www.TeachingTechnicalTraders.com
    ·
  • MikerocontrollerMikerocontroller Posts: 310
    edited 2008-10-26 06:25
    · Did you declare "I" as a Byte variable?· Variables in PBasic must be declared before use.·

    example:

    · I······· VAR······ Byte

    · If possible, could you post the entire code?
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2008-10-26 06:32
    I trust you have a series resistor between the Stamp pin port and the LEDs. Otherwise the Stamp may be browning out and resetting. I doubt it's "hanging" as there is virtually no way to have that happen in PBASIC. Place the following line at the very beginning of your program:

    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
  • HumanoidoHumanoido Posts: 5,770
    edited 2008-10-26 06:47
    Indicate2 is a subroutine label and needs to have this syntax:

    Indicate2:
    
  • Dr. ResonanceDr. Resonance Posts: 12
    edited 2008-10-26 19:07
    Thanks for all the tips guys.

    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
  • CannibalRoboticsCannibalRobotics Posts: 535
    edited 2008-10-26 19:24
    The pins power up as inputs and stay that way until otherwise instructed so there is no need to reinitialize them to input.
    Jim-

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Signature space for rent, only $1.
    Send cash and signature to CannibalRobotics.
  • MikerocontrollerMikerocontroller Posts: 310
    edited 2008-10-26 19:54
    · Don't forget to put in the PBASIC version in your compiler directives.·I beleive if you use version 2.5 you will be required to use a colon at the end of your address names.· You do not use a colon when a reference is made to an address, such as GOTO CkeckSwitch1.· The PBASIC version icons are next to the STAMP icons in the toolbar of your editor.·

    ·· example:

    ·· ' {$STAMP BS2}

    ···' {$PBASIC 2.5}
  • $WMc%$WMc% Posts: 1,884
    edited 2008-10-26 20:10
    Dr Rresonance

    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
  • Dr. ResonanceDr. Resonance Posts: 12
    edited 2008-10-27 22:14
    Thanks for the tip Mike.·

    I was wondering where everyone got the weird brackets as I could not find them on my keyboard.

    Regards,

    Dr. Resonance
Sign In or Register to comment.