PDA

View Full Version : Need to STAMP out problems with my program



AncientDragonMaster6984
03-24-2010, 11:15 AM
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 [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

W9GFO
03-24-2010, 01:23 PM
AncientDragonMaster6984 said...





' {$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
DEBUG "ouch",CR
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

starter: 'generates the starting number

DEBUG CLS
DEBUG "A new game has started that even [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

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



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. (http://www.gadgetgangster.com/206)

W9GFO
03-24-2010, 01:28 PM
IF NOT t=1 AND NOT t=0 THEN

LOOP UNTIL t=1 OR t=0



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. (http://www.gadgetgangster.com/206)

AncientDragonMaster6984
03-24-2010, 08:51 PM
That portion is for idiot proofing in case someone enters 5 for example.

AncientDragonMaster6984
03-24-2010, 08:54 PM
What would happen if someone enters a character other than 0 or 1 on the debug normally?

Franklin
03-25-2010, 12:44 AM
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


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

W9GFO
03-25-2010, 12:49 AM
You would need to change "t" to be a BYTE sized variable if you want it to contain a value greater than 1 (NIB would work too but the highest value would be limited to 15).



DEBUGIN BIN t



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. (http://www.gadgetgangster.com/206)

Post Edited (W9GFO) : 3/24/2010 6:05:01 PM GMT

W9GFO
03-25-2010, 01:09 AM
AncientDragonMaster6984 said...
What would happen if someone enters a character other than 0 or 1 on the debug normally?


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. (http://www.gadgetgangster.com/206)

AncientDragonMaster6984
03-25-2010, 04:05 AM
t was originally a bit to save on space. It turns out now that I have the space to store it as a word variable. Putting the main program first turned out to be part of the solution. 20<x<100 is still giving fits. Is there a problem with this way of saying x has to be greater than 20 and less than 100?

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.

Franklin
03-25-2010, 04:16 AM
I think you need IF x > 20 AND x < 100 THEN

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen

AncientDragonMaster6984
03-25-2010, 07:06 AM
I scrapped the random subroutine and added the minus subroutine. The starter subroutine is working now, but the minus is acting weird.
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 [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

Franklin
03-25-2010, 08:33 AM
You are probably experiencing 'switch bounce'

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen

AncientDragonMaster6984
03-25-2010, 09:05 PM
I have tried adding BUTTON commands to prevent switch bounce. I'm still not having success in making it subtract only once. It will even do so if one end of a wire is plugged into the pin socket on the breadboard with the other end hanging free. There shouldn't even be a path.

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 [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

Spiral_72
03-25-2010, 09:45 PM
Just a side note here...... make sure all relevant inputs are pulled low or high (depending on your circuit) with a high value resistor. 10k?

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"...... crap, there went another one.

W9GFO
03-25-2010, 09:51 PM
IF p1 = ison AND p2 = p3 = p4 = isoff AND x>0 THEN


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. (http://www.gadgetgangster.com/206)

AncientDragonMaster6984
03-26-2010, 09:39 PM
I can't get that past the syntax test. What I need is for the value to be subtracted from once whenever a certain button is pressed. It continually subtracts whenever there is a wire in the pin circuit regardless of whether or not the other end of the wire is connected to anything else.




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

kf4ixm
03-26-2010, 09:49 PM
AncientDragonMaster6984 said...
·What I need is for the value to be subtracted from once whenever a certain button is pressed. It continually subtracts whenever there is a wire in the pin circuit regardless of whether or not the other end of the wire is connected to anything else.
Do you have a pullup or pulldown resistor on the button circuit? it sounds like you don't. As Spiral_72 stated above,·whenever the pullup or pulldown resistor is omitted from a pushbutton circuit, the input acts as a recieving antenna of sorts, electrical noise will give you unpredictable signals on a floating pin (without a pullup or pulldown resistor). Refer to the help section in the basic stamp editor, look for button under index search·and see the example schematic for adding a pullup or pulldown resistor to a pushbutton.

Post Edited (kf4ixm) : 3/26/2010 2:56:47 PM GMT

W9GFO
03-26-2010, 11:20 PM
You still are not using the results of the BUTTON command to determine if a button is pushed. "p1" refers to the pin not the variable "ba".

Rich H

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Simple Servo Tester, a kit from Gadget Gangster. (http://www.gadgetgangster.com/206)

W9GFO
03-27-2010, 12:03 AM
Btn VAR NIB

SELECT Btn

CASE %0001
x = x-1 'subtracts 1 from number
GOSUB Display

CASE %0010
x = x-2 'subtracts 2 from number
GOSUB Display

CASE %0100
x = x-3 'subtracts 3 from number
GOSUB Display

CASE %1000
x = x-4 'subtracts 4 from number
GOSUB Display

CASE ELSE
GOSUB readButton

ENDSELECT

Display:

DEBUG CLS
DEBUG DEC x
PAUSE 1500
RETURN

readButton: 'Pseudo code to follow;

******************
DO

IF button 1 is pressed THEN Btn.BIT0 = 1
IF button 2 is pressed THEN Btn.BIT1 = 1
IF button 3 is pressed THEN Btn.BIT2 = 1
IF button 4 is pressed THEN Btn.BIT3 = 1

LOOPWHILE Btn = 0

RETURN
******************




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?" (http://www.parallax.com/Portals/0/Downloads/docs/prod/edu/28123-WAM-v3.0.pdf) manual;

http://forums.parallax.com/attachment.php?attachmentid=68929

Rich H

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Simple Servo Tester, a kit from Gadget Gangster. (http://www.gadgetgangster.com/206)

Post Edited (W9GFO) : 3/26/2010 8:00:10 PM GMT

W9GFO
03-27-2010, 04:43 AM
Better yet;



Btn VAR NIB

GOSUB readButton

x = x - Btn 'subtracts value from number

DEBUG CLS
DEBUG DEC x
PAUSE 1500

readButton: 'Pseudo code to follow;

******************
DO

IF button 1 is pressed THEN Btn = 1
IF button 2 is pressed THEN Btn = 2
IF button 3 is pressed THEN Btn = 3
IF button 4 is pressed THEN Btn = 4

LOOPWHILE Btn = 0

RETURN
******************


Rich H

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Simple Servo Tester, a kit from Gadget Gangster. (http://www.gadgetgangster.com/206)

AncientDragonMaster6984
03-27-2010, 08:52 PM
I see what you're getting at. I'll have to wait until Tuesday to test it, though.

AncientDragonMaster6984
03-31-2010, 07:12 AM
The problem has been solved. What was needed was to swap the NOTC switches with NCTO switches all running series with ground. I'd like to thank everyone for their help.