Debug Terminal Problem
bluejay
Posts: 131
in BASIC Stamp
Would somebody please explain how I can modify my program so that when any keys input from keyboard other than the umbers 0 through 9 are pressed the program will type "invalid entry" , "try again". Below is my program , Thanks.
' {$STAMP BS2}
' {$PBASIC 2.5}
'4 color leds
counter VAR Word
repeat VAR Nib
led VAR Nib
lines VAR Byte
blue CON 0
green CON 1
yellow CON 2
red CON 3
Main:
GOSUB Terminal
led=blue
DEBUG HOME,"blue led" 'pin 1 blue led
GOSUB timing
led=green 'pin 2 green led
DEBUG HOME,"green led"
GOSUB timing
led=yellow 'pin 3 yellow led
DEBUG HOME,"yellow led"
GOSUB timing
led=red 'pin 4 red led
DEBUG HOME,"red led"
GOSUB timing
GOTO main
timing:
FOR repeat = 1 TO 2
FOR counter = 0 TO 500 STEP 20
PULSOUT led, counter
PAUSE 10
NEXT
FOR counter = 500 TO 0 STEP 20
PULSOUT led, counter
PAUSE 10
NEXT
NEXT
Terminal:
DEBUG CLS
DEBUG "What color led's?", CR
DEBUG "1=Blue,2=Green,3=Yellow,4=Red", CR
DEBUGIN DEC1 lines
IF lines = 1 THEN
DEBUG CLS,CR, CR
GOSUB blueled
ENDIF
IF lines = 2 THEN
DEBUG CLS,CR, CR
GOSUB greenled
ENDIF
IF lines = 3 THEN
DEBUG CLS,CR, CR
GOSUB yellowled
ENDIF
IF lines = 4 THEN
DEBUG CLS,CR, CR
GOSUB redled
ELSE
ENDIF
IF (lines < 1) OR (lines > 4) THEN
GOSUB invalid
ENDIF
GOSUB invalid
PAUSE 2000
GOTO Terminal
blueled:
led=blue 'pin 1 blue led
DEBUG HOME,"blue led"
GOSUB timing
RETURN
greenled:
led=green 'pin 2 green led
DEBUG HOME,"green led"
GOSUB timing
RETURN
yellowled:
led=yellow 'pin 3 yellow led
DEBUG HOME,"yellow led"
GOSUB timing
RETURN
redled:
DEBUG CLS
led=red 'pin 4red led
DEBUG HOME,"red led"
GOSUB timing
RETURN
invalid:
DEBUG CLS, "Invalid entry!",CR
DEBUG "Try again"
RETURN
' {$STAMP BS2}
' {$PBASIC 2.5}
'4 color leds
counter VAR Word
repeat VAR Nib
led VAR Nib
lines VAR Byte
blue CON 0
green CON 1
yellow CON 2
red CON 3
Main:
GOSUB Terminal
led=blue
DEBUG HOME,"blue led" 'pin 1 blue led
GOSUB timing
led=green 'pin 2 green led
DEBUG HOME,"green led"
GOSUB timing
led=yellow 'pin 3 yellow led
DEBUG HOME,"yellow led"
GOSUB timing
led=red 'pin 4 red led
DEBUG HOME,"red led"
GOSUB timing
GOTO main
timing:
FOR repeat = 1 TO 2
FOR counter = 0 TO 500 STEP 20
PULSOUT led, counter
PAUSE 10
NEXT
FOR counter = 500 TO 0 STEP 20
PULSOUT led, counter
PAUSE 10
NEXT
NEXT
Terminal:
DEBUG CLS
DEBUG "What color led's?", CR
DEBUG "1=Blue,2=Green,3=Yellow,4=Red", CR
DEBUGIN DEC1 lines
IF lines = 1 THEN
DEBUG CLS,CR, CR
GOSUB blueled
ENDIF
IF lines = 2 THEN
DEBUG CLS,CR, CR
GOSUB greenled
ENDIF
IF lines = 3 THEN
DEBUG CLS,CR, CR
GOSUB yellowled
ENDIF
IF lines = 4 THEN
DEBUG CLS,CR, CR
GOSUB redled
ELSE
ENDIF
IF (lines < 1) OR (lines > 4) THEN
GOSUB invalid
ENDIF
GOSUB invalid
PAUSE 2000
GOTO Terminal
blueled:
led=blue 'pin 1 blue led
DEBUG HOME,"blue led"
GOSUB timing
RETURN
greenled:
led=green 'pin 2 green led
DEBUG HOME,"green led"
GOSUB timing
RETURN
yellowled:
led=yellow 'pin 3 yellow led
DEBUG HOME,"yellow led"
GOSUB timing
RETURN
redled:
DEBUG CLS
led=red 'pin 4red led
DEBUG HOME,"red led"
GOSUB timing
RETURN
invalid:
DEBUG CLS, "Invalid entry!",CR
DEBUG "Try again"
RETURN

Comments
DEBUGIN DEC1 lines
try
DEBUGIN lines
IF lines < "1" OR lines > "4" THEN GOSUB invalid
That is, treat the options as single ascii characters rather than as DECimal values.
Note on program structure: take a look at the SELECT:CASE instructions for more compact branching through the options.
' {$STAMP BS2}
' {$PBASIC 2.5}
' fireflyled4
counter VAR Word
repeat VAR Nib
led VAR Nib
lines VAR Word
blue CON 0
green CON 1
yellow CON 2
red CON 3
Main:
GOSUB Terminal
led=blue 'pin 0 blue led
DEBUG HOME,"blue led"
GOSUB timing
led=green 'pin 1 green led
DEBUG HOME,"green led"
GOSUB timing
led=yellow 'pin 2 yellow led
DEBUG HOME,"yellow led"
GOSUB timing
led=red 'pin 3 red led
DEBUG HOME,"red led"
GOSUB timing
GOTO main
timing:
FOR repeat = 1 TO 2
FOR counter = 0 TO 500 STEP 20
PULSOUT led, counter
PAUSE 10
NEXT
FOR counter = 500 TO 0 STEP 20
PULSOUT led, counter
PAUSE 10
NEXT
NEXT
Terminal:
DEBUG CLS
DEBUG HOME, "What program numbers? Blinking color led's twice",CR
DEBUG "1=Blue,2=Green,3=Yellow,4=Red",CR
DEBUG "5=servo control demo"
'DEBUGIN DEC1 lines
DEBUGIN lines
IF lines < "1" OR lines > "5" THEN
GOSUB invalid
'ENDIF
'ENDIF
'GOSUB invalid
'PAUSE 2000
'GOTO Terminal
IF lines = 1 THEN
DEBUG CLS,CR, CR
GOSUB blueled
ENDIF
IF lines = 2 THEN
DEBUG CLS,CR, CR
GOSUB greenled
ENDIF
IF lines = 3 THEN
DEBUG CLS,CR, CR
GOSUB yellowled
ENDIF
IF lines = 4 THEN
DEBUG CLS,CR, CR
GOSUB redled
ELSE
ENDIF
IF lines = 5 THEN
DEBUG CLS,CR, CR
GOSUB servo1
'ELSE
ENDIF
ENDIF
GOTO terminal
'IF (lines < 1) OR (lines > 4) THEN
'IF lines < "1" OR lines > "4" THEN
'GOSUB invalid
'ENDIF
'GOSUB invalid
'PAUSE 2000
'GOTO Terminal
blueled:
led=blue 'pin 0 blue led
DEBUG HOME,"blue led"
GOSUB timing
RETURN
greenled:
led=green 'pin 1 green led
DEBUG HOME,"green led"
GOSUB timing
RETURN
yellowled:
led=yellow 'pin 2 yellow led
DEBUG HOME,"yellow led"
GOSUB timing
RETURN
redled:
DEBUG CLS
led=red 'pin 3 red led
DEBUG HOME,"red led"
GOSUB timing
RETURN
invalid:
DEBUG CLS, "Invalid entry!",CR
DEBUG "Try again"
RETURN
servo1:
DEBUG "Pulse width increment by 8", CR 'rotate pointer ccw to 180deg, the rotate to 0 deg.
'FOR repeat = 1 TO 2
FOR counter = 1500 TO 100 STEP 8
HIGH red
IF counter = 100 THEN HIGH blue
LOW red
PULSOUT 14, counter
'ELSE HIGH blue
' ENDIF
PAUSE 7
DEBUG DEC5 counter, CR, CRSRUP
' ENDIF
NEXT
'ENDIF
'NEXT
RETURN
Also, this is a good time to read about select/case.
DEBUGIN lines SELECT lines CASE "1" DEBUG CLS,CR, CR GOSUB blueled CASE "2" DEBUG CLS, CR, CR GOSUB greenled and so-on for "3" and "4" CASE ELSE GOSUB invalid ENDSELECT' {$STAMP BS2} ' {$PBASIC 2.5} ' fireflyled4 [b][/b] counter VAR Word repeat VAR Nib led VAR Nib lines VAR Word blue CON 0 green CON 1 yellow CON 2 red CON 3 Main: GOSUB Terminal led=blue 'pin 0 blue led DEBUG HOME,"blue led" GOSUB timing led=green 'pin 1 green led DEBUG HOME,"green led" GOSUB timing led=yellow 'pin 2 yellow led DEBUG HOME,"yellow led" GOSUB timing led=red 'pin 3 red led DEBUG HOME,"red led" GOSUB timing GOTO main timing: FOR repeat = 1 TO 2 FOR counter = 0 TO 500 STEP 20 PULSOUT led, counter PAUSE 10 NEXT FOR counter = 500 TO 0 STEP 20 PULSOUT led, counter PAUSE 10 NEXT NEXT Terminal: DEBUG CLS DEBUG HOME, "What program numbers?",CR DEBUG "1=Blue,2=Green,3=Yellow,4=Red",CR DEBUG "5=servo control demo" DEBUGIN lines SELECT lines CASE "1" DEBUG CLS,CR,CR GOSUB blueled CASE "2" DEBUG CLS,CR, CR GOSUB greenled CASE "3" DEBUG CLS,CR, CR GOSUB yellowled CASE "4" DEBUG CLS,CR, CR GOSUB redled CASE "5" DEBUG CLS,CR, CR GOSUB servo1 CASE ELSE GOSUB invalid ENDSELECT IF lines < "1" OR lines > "5" THEN GOSUB invalid ENDIF GOTO terminal blueled: led=blue 'pin 0 blue led DEBUG HOME,"blue led" GOSUB timing RETURN greenled: led=green 'pin 1 green led DEBUG HOME,"green led" GOSUB timing RETURN yellowled: led=yellow 'pin 2 yellow led DEBUG HOME,"yellow led" GOSUB timing RETURN redled: DEBUG CLS led=red 'pin 3 red led DEBUG HOME,"red led" GOSUB timing RETURN invalid: DEBUG CLS DEBUG "Invalid entry!" DEBUG "Try again" PAUSE 1000 RETURN servo1: DEBUG "Pulse width increment by 8", CR 'rotate pointer ccw to 180deg, the rotate to 0 deg. FOR counter = 1500 TO 100 STEP 8 HIGH green IF counter = 100 THEN HIGH blue LOW red PULSOUT 14, counter PAUSE 7 DEBUG DEC5 counter, CR, CRSRUP NEXT PAUSE 500 LOW blue LOW green RETURN