Need to STAMP out problems with my program
AncientDragonMaster6984
Posts: 9
I'm trying to create a simple game for a project for school (including writing a program from scratch). For some reason, it doesn't work and even my teacher doesn't know why. He suggested starting a topic on this forum. The hardware is a 1 kohm resistor connected to Vdd in series with 4 NOTC switches in parallel connected to the relevant pins.
The program (below) as is will only display the first debug line 3 times (don't know why). Removing the first subroutine and relevant programming will allow the second subroutine to start, but it also has problems. Inputted values >20 result in the option meant for errors (I want a number less than 100, but greater than 20), while values less than 20 will not be considered errors and the program will go to the end of the subroutine before resetting prematurely.
The program is pasted below (it wouldn't let me download it). If it comes down to it, I can scrap one subroutine as long as the other works. I would lik both to work if possible, though. Any ideas?
' {$STAMP BS2}
' {$PBASIC 2.5}
'Written by AncientDragonMaster6984
'This is a program for a digital version of a game called i(expletive)ori. Starting with either a random or picked number,
'2 players take turns subtracting anywhere from 1 to 4 from the number. The object is to not be the person to
'subtract the last number. Any rules not enforced by the program will rely on an honor system.
t VAR Bit 'used to denote whether a random or nonrandom number is used to start
x VAR Byte 'represents the number used in the game
r VAR Byte 'used in the random subroutine
p1 PIN 9 'represents the 4 input pins
p2 PIN 6
p3 PIN 3
p4 PIN 1
ison CON 1 'used in Boolean statements with inputs
isoff CON 0
INPUT p1 'defines the pins as input
INPUT p2
INPUT p3
INPUT p4
DEBUG "testing...",CR
randomness: 'the semi-RANDOM number generator
RANDOM x
DO
RANDOM r
x= x + r
RANDOM r
x=x-r
LOOP UNTIL 20<x<100
RETURN
DEBUG "ouch",CR
starter: 'generates the starting number
DEBUG CLS
DEBUG "A new game has started that even [noparse][[/noparse]teacher's name] can understand.", CR
DEBUG "Enter 0 for a random number or 1 to create your own and press ENTER", CR
DEBUGIN BIN t
IF NOT t=1 AND NOT t=0 THEN
DO
DEBUG "I may be smarter than a politician, but not by much.", CR 'error checking
DEBUG "Enter 0 for a random number or 1 to create your own and press ENTER", CR
DEBUGIN BIN t
LOOP UNTIL t=1 OR t=0
ELSEIF t=0 THEN
GOSUB randomness
ELSEIF t=1 THEN
DO
DEBUG "Please enter a number greater than 20 and less than 100 and press ENTER",CR
DEBUGIN DEC x
IF NOT 20<x<100 THEN
DEBUG "I may be smarter than a politician, but not by much.", CR 'error checking
ENDIF
LOOP UNTIL 20<x<100
ENDIF
DEBUG "Let's begin digital i(expletive)ori", CR 'user instructions
DEBUG "The order of the buttons is:",CR
DEBUG "1",CR
DEBUG "2",CR
DEBUG "3",CR
DEBUG "4",CR
PAUSE 10000
RETURN
DEBUG "banana"
DO
GOSUB starter
DEBUG DEC x
DO
IF p1 = ison AND p2 = p3 = p4 = isoff AND x>0 THEN
x = x-1 'subtracts 1 from number
DEBUG CLS
DEBUG DEC x
PAUSE 1500
ELSEIF p2 = ison AND p1 = p3 = p4 = isoff AND x>1 THEN
x = x-2 'subtracts 2 from number
DEBUG CLS
DEBUG DEC x
PAUSE 1500
ELSEIF p3 = ison AND p1 = p2 = p4 = isoff AND x>2 THEN
x = x-3 'subtracts 3 from number
DEBUG CLS
DEBUG DEC x
PAUSE 1500
ELSEIF p4 = ison AND p1 = p2 = p3 = isoff AND x>3 THEN
x = x-4
DEBUG "bob" 'subtracts 4 from number
DEBUG CLS
DEBUG DEC x
PAUSE 1500
ENDIF
LOOP UNTIL x=0
DEBUG CR
DEBUG "Game over" 'the game is over, automatic restart in 5 seconds
PAUSE 5000
LOOP
The program (below) as is will only display the first debug line 3 times (don't know why). Removing the first subroutine and relevant programming will allow the second subroutine to start, but it also has problems. Inputted values >20 result in the option meant for errors (I want a number less than 100, but greater than 20), while values less than 20 will not be considered errors and the program will go to the end of the subroutine before resetting prematurely.
The program is pasted below (it wouldn't let me download it). If it comes down to it, I can scrap one subroutine as long as the other works. I would lik both to work if possible, though. Any ideas?
' {$STAMP BS2}
' {$PBASIC 2.5}
'Written by AncientDragonMaster6984
'This is a program for a digital version of a game called i(expletive)ori. Starting with either a random or picked number,
'2 players take turns subtracting anywhere from 1 to 4 from the number. The object is to not be the person to
'subtract the last number. Any rules not enforced by the program will rely on an honor system.
t VAR Bit 'used to denote whether a random or nonrandom number is used to start
x VAR Byte 'represents the number used in the game
r VAR Byte 'used in the random subroutine
p1 PIN 9 'represents the 4 input pins
p2 PIN 6
p3 PIN 3
p4 PIN 1
ison CON 1 'used in Boolean statements with inputs
isoff CON 0
INPUT p1 'defines the pins as input
INPUT p2
INPUT p3
INPUT p4
DEBUG "testing...",CR
randomness: 'the semi-RANDOM number generator
RANDOM x
DO
RANDOM r
x= x + r
RANDOM r
x=x-r
LOOP UNTIL 20<x<100
RETURN
DEBUG "ouch",CR
starter: 'generates the starting number
DEBUG CLS
DEBUG "A new game has started that even [noparse][[/noparse]teacher's name] can understand.", CR
DEBUG "Enter 0 for a random number or 1 to create your own and press ENTER", CR
DEBUGIN BIN t
IF NOT t=1 AND NOT t=0 THEN
DO
DEBUG "I may be smarter than a politician, but not by much.", CR 'error checking
DEBUG "Enter 0 for a random number or 1 to create your own and press ENTER", CR
DEBUGIN BIN t
LOOP UNTIL t=1 OR t=0
ELSEIF t=0 THEN
GOSUB randomness
ELSEIF t=1 THEN
DO
DEBUG "Please enter a number greater than 20 and less than 100 and press ENTER",CR
DEBUGIN DEC x
IF NOT 20<x<100 THEN
DEBUG "I may be smarter than a politician, but not by much.", CR 'error checking
ENDIF
LOOP UNTIL 20<x<100
ENDIF
DEBUG "Let's begin digital i(expletive)ori", CR 'user instructions
DEBUG "The order of the buttons is:",CR
DEBUG "1",CR
DEBUG "2",CR
DEBUG "3",CR
DEBUG "4",CR
PAUSE 10000
RETURN
DEBUG "banana"
DO
GOSUB starter
DEBUG DEC x
DO
IF p1 = ison AND p2 = p3 = p4 = isoff AND x>0 THEN
x = x-1 'subtracts 1 from number
DEBUG CLS
DEBUG DEC x
PAUSE 1500
ELSEIF p2 = ison AND p1 = p3 = p4 = isoff AND x>1 THEN
x = x-2 'subtracts 2 from number
DEBUG CLS
DEBUG DEC x
PAUSE 1500
ELSEIF p3 = ison AND p1 = p2 = p4 = isoff AND x>2 THEN
x = x-3 'subtracts 3 from number
DEBUG CLS
DEBUG DEC x
PAUSE 1500
ELSEIF p4 = ison AND p1 = p2 = p3 = isoff AND x>3 THEN
x = x-4
DEBUG "bob" 'subtracts 4 from number
DEBUG CLS
DEBUG DEC x
PAUSE 1500
ENDIF
LOOP UNTIL x=0
DEBUG CR
DEBUG "Game over" 'the game is over, automatic restart in 5 seconds
PAUSE 5000
LOOP
Comments
I took the liberty of rearranging your code so that I could follow it more easily...
Rich H
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Simple Servo Tester, a kit from Gadget Gangster.
Since t is a bit sized variable it can only be either a 0 or 1. In other words, t will always have a value of either zero or one.
Rich H
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Simple Servo Tester, a kit from Gadget Gangster.
This is the start of your code and you have a return that was not called by a gosub (the code starts at debug and continues through the code below.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
If someone enters 5 (binary 101) the value stored in "t" will be a 1. If they enter 4 (binary 100) it would be a zero. Since you have made the variable "t" only one BIT in length, there is only room for the last BIT to fit inside the variable.
It might actually work to your advantage to make "t" a NIB (or BYTE) sized variable and forget about the part of the code that limits it to 20 > t < 100. They could input any number but only the last four (or eight) BITS will be used. Just test for zero to determine if you need to enter the randomness routine. For instance, they could enter 794 (binary 11_0001_1010) but the value stored within the BYTE sized variable would be 26 and the NIB sized would be 10.
You'll also want to change the "BIN" to "DEC" since you are having the user enter decimal values.
Rich H
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Simple Servo Tester, a kit from Gadget Gangster.
Post Edited (W9GFO) : 3/24/2010 6:05:01 PM GMT
You should write a simple program to test this. Just be sure to use a BYTE or WORD sized variable to hold the data.
Rich H
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Simple Servo Tester, a kit from Gadget Gangster.
It also completely skips my string of boolean statements designed to check for input and doesn't pause for nonrandom input. I'm going to need to take another look.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
whenever I put a wire end into a relevant pin (regardless of whether or not the other end is connected to anything), automatic subtraction from x occurs. It should only subtract once and only when a button is pressed.
The new code is as follows. Any ideas?
' {$STAMP BS2}
' {$PBASIC 2.5}
'ELTC 206 Final Project
'Written by AncientDragonMaster6984
'This is a program for a digital version of a game called i(expletive)ori. Starting with either a random or picked number,
'2 players take turns subtracting anywhere from 1 to 4 from the number. The object is to not be the person to
'subtract the last number. Any rules not enforced by the program will rely on an honor system.
x VAR Byte 'represents the number used in the game
r VAR Byte 'used in the random subroutine and in subracting
p1 PIN 9 'represents the 4 input pins
p2 PIN 6
p3 PIN 3
p4 PIN 1
ison CON 1 'used in Boolean statements with inputs
isoff CON 0
INPUT p1 'defines the pins as input
INPUT p2
INPUT p3
INPUT p4
x=0
r=0
DO
GOSUB starter
DEBUG DEC x
GOSUB minus
DEBUG CR
DEBUG "Game over" 'the game is over, automatic restart in 5 seconds
PAUSE 5000
LOOP
minus:
PAUSE 1000
DO
PAUSE 5
'BUTTON p1, ison, 255, 0, ba, 0, np
'BUTTON p2, ison, 255, 0, ba, 1, np
'BUTTON p3, ison, 255, 0, ba, 1, np
'BUTTON p4, ison, 255, 0, ba, 1, np
IF p1 = ison AND p2 = p3 = p4 = isoff AND x>0 THEN
x = x-1 'subtracts 1 from number
DEBUG CLS
DEBUG DEC x
PAUSE 1500
p1 = isoff
ELSEIF p2 = ison AND p1 = p3 = p4 = isoff AND x>1 THEN
x = x-2 'subtracts 2 from number
DEBUG CLS
DEBUG DEC x
PAUSE 1500
ELSEIF p3 = ison AND p1 = p2 = p4 = isoff AND x>2 THEN
x = x-3 'subtracts 3 from number
DEBUG CLS
DEBUG DEC x
PAUSE 1500
p3 = isoff
ELSEIF p4 = ison AND p1 = p2 = p3 = isoff AND x>3 THEN
x = x-4 'subtracts 4 from number
DEBUG CLS
DEBUG DEC x
PAUSE 1500
p4 = isoff
ELSE
GOTO minus:
ENDIF
LOOP UNTIL x=0
RETURN
starter: 'generates the starting number
DEBUG CLS
DEBUG "A new game has started that even [noparse][[/noparse]teacher's name] can understand.", CR
DO
DEBUG "Please enter a number greater than 20 and less than 100 and press ENTER",CR
DEBUGIN DEC x
IF NOT (x>20 AND x<100) THEN
DEBUG "I may be smarter than a politician, but not by much.", CR 'error checking
ENDIF
LOOP UNTIL x>20 AND x<100
DEBUG "Let's begin digital i(expletive)ori", CR 'user instructions
DEBUG "The order of the buttons is:",CR
DEBUG "1",CR
DEBUG "2",CR
DEBUG "3",CR
DEBUG "4",CR
PAUSE 1000
RETURN
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
This is the program as is. Any ideas?
' {$STAMP BS2}
' {$PBASIC 2.5}
'ELTC 206 Final Project
'Written by AncientDragonMaster6984
'This is a program for a digital version of a game called i(expletive)ori. Starting with either a random or picked number,
'2 players take turns subtracting anywhere from 1 to 4 from the number. The object is to not be the person to
'subtract the last number. Any rules not enforced by the program will rely on an honor system.
x VAR Byte 'represents the number used in the game
r VAR Byte 'used in the random subroutine and in subracting
ba VAR Byte
bb VAR Byte
bc VAR Byte
bd VAR Byte
p1 PIN 9 'represents the 4 input pins
p2 PIN 6
p3 PIN 3
p4 PIN 1
ison CON 1 'used in Boolean statements with inputs
isoff CON 0
INPUT p1 'defines the pins as input
INPUT p2
INPUT p3
INPUT p4
x=0
r=0
ba=0
bb=0
bc=0
bd=0
DO
GOSUB starter
DEBUG DEC x
GOSUB minus
DEBUG CR
DEBUG "Game over" 'the game is over, automatic restart in 5 seconds
PAUSE 5000
LOOP
minus:
PAUSE 1000
DO
PAUSE 5
BUTTON p1, ison, 255, 0, ba, 1, np
BUTTON p2, ison, 255, 0, bb, 1, np
BUTTON p3, ison, 255, 0, bc, 1, np
BUTTON p4, ison, 255, 0, bd, 1, np
IF p1 = ison AND p2 = p3 = p4 = isoff AND x>0 THEN
x = x-1 'subtracts 1 from number
DEBUG CLS
DEBUG DEC x
PAUSE 1500
p1 = isoff
ELSEIF p2 = ison AND p1 = p3 = p4 = isoff AND x>1 THEN
x = x-2 'subtracts 2 from number
DEBUG CLS
DEBUG DEC x
PAUSE 1500
ELSEIF p3 = ison AND p1 = p2 = p4 = isoff AND x>2 THEN
x = x-3 'subtracts 3 from number
DEBUG CLS
DEBUG DEC x
PAUSE 1500
p3 = isoff
ELSEIF p4 = ison AND p1 = p2 = p3 = isoff AND x>3 THEN
x = x-4 'subtracts 4 from number
DEBUG CLS
DEBUG DEC x
PAUSE 1500
p4 = isoff
ELSE
GOTO minus:
ENDIF
LOOP UNTIL x=0
RETURN
np:
GOTO minus
starter: 'generates the starting number
DEBUG CLS
DEBUG "A new game has started that even [noparse][[/noparse]teacher's name] can understand.", CR
DO
DEBUG "Please enter a number greater than 20 and less than 100 and press ENTER",CR
DEBUGIN DEC x
IF NOT (x>20 AND x<100) THEN
DEBUG "I may be smarter than a politician, but not by much.", CR 'error checking
ENDIF
LOOP UNTIL x>20 AND x<100
DEBUG "Let's begin digital i(expletive)ori", CR 'user instructions
DEBUG "The order of the buttons is:",CR
DEBUG "1",CR
DEBUG "2",CR
DEBUG "3",CR
DEBUG "4",CR
PAUSE 1000
RETURN
What I've noticed is an input that is OPEN will float from a low and high state seemingly random. Obviously this causes all kinds of problems if your monitoring the input. I believe this "condition" is in the stamp manual as well.
So, if the switch pulls the input HIGH, tie the input to ground with a 10k resistor, then the switch to +5.... This holds the switch at 0V until the switch pulls it to 5V (and wastes <1mA, no big deal usually)
The opposite is true, if the switch pulls the input LOW, use a 10k resistor to 5V
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"puff"...... Smile, there went another one.
Don't you want to use the result of the BUTTON commands to test which buttons have been pushed rather than the pin states?
You might want to look into using SELCT..CASE rather than several IF statements.
Rich H
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Simple Servo Tester, a kit from Gadget Gangster.
SELECT x
CASE p1 = ison AND p2 = p3 = p4 = isoff AND x>0
x = x-1 'subtracts 1 from number
DEBUG CLS
DEBUG DEC x
PAUSE 1500
CASE p2 = ison AND p1 = p3 = p4 = isoff AND x>1
x = x-2 'subtracts 2 from number
DEBUG CLS
DEBUG DEC x
PAUSE 1500
CASE p3 = ison AND p1 = p2 = p4 = isoff AND x>2
x = x-3 'subtracts 3 from number
DEBUG CLS
DEBUG DEC x
PAUSE 1500
CASE p4 = ison AND p1 = p2 = p3 = isoff AND x>3
x = x-4 'subtracts 4 from number
DEBUG CLS
DEBUG DEC x
PAUSE 1500
ENDSELECT
Post Edited (kf4ixm) : 3/26/2010 2:56:47 PM GMT
Rich H
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Simple Servo Tester, a kit from Gadget Gangster.
Do make sure that the buttons are pulled low when they are not pushed. If the pins are not connected to anything you can not be sure what level they are at and they may read as high.
From page 66 of the "What's a Microcontroller?" manual;
Rich H
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Simple Servo Tester, a kit from Gadget Gangster.
Post Edited (W9GFO) : 3/26/2010 8:00:10 PM GMT
Rich H
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Simple Servo Tester, a kit from Gadget Gangster.