Using the addition feature as a coin adder
v_2hbran
Posts: 4
I am a beginner with the basic stamp in an electronics course.· As part of a group project we are attempting to build a working arcade game to finish off microprocessors.· We have a working coin raceway alike to one from a payphone, and have it successfully recognizing coins put into the 3 slots through a normally open relay switch.· The code we used is:
' {$STAMP BS2}
' {$PBASIC 2.5}
coinCounter VAR Byte
DO
DEBUG HOME
DEBUG ? IN4
DEBUG ? IN9
DEBUG ? IN15
IF (IN4 = 1) THEN
·DEBUG "5 cents."
·HIGH 12
·PAUSE 500
·LOW 12
·ELSEIF(IN9 = 1) THEN
·DEBUG "10 cents."
·HIGH 12
·PAUSE 500
·LOW 12
·ELSEIF (IN15 = 1) THEN
·DEBUG "25 cents, start bowling."
·HIGH 12
·PAUSE 500
·LOW 12
ENDIF
LOOP
It is working the way we want it for recognition purposes, but we are having trouble figuring out how to get it to add the dimes and nickels up to 25 cents.· If anyone can point us in the right direction it would be greatly appreciated!·· thank you, team HAM
' {$STAMP BS2}
' {$PBASIC 2.5}
coinCounter VAR Byte
DO
DEBUG HOME
DEBUG ? IN4
DEBUG ? IN9
DEBUG ? IN15
IF (IN4 = 1) THEN
·DEBUG "5 cents."
·HIGH 12
·PAUSE 500
·LOW 12
·ELSEIF(IN9 = 1) THEN
·DEBUG "10 cents."
·HIGH 12
·PAUSE 500
·LOW 12
·ELSEIF (IN15 = 1) THEN
·DEBUG "25 cents, start bowling."
·HIGH 12
·PAUSE 500
·LOW 12
ENDIF
LOOP
It is working the way we want it for recognition purposes, but we are having trouble figuring out how to get it to add the dimes and nickels up to 25 cents.· If anyone can point us in the right direction it would be greatly appreciated!·· thank you, team HAM
Comments
Let me first say as a beginner you're doing rather well! What you're missing here is really nothing but a concept. That will come with time.
I've expanded your program just a bit, but here is one way to go about it. Refer to the PBASIC Manual for any commands you don't understand. The idea here is to keep a running total of all the coin values·inserted to date, in terms of CENTS.
I have tried to use meaningful names and methods that are self-documenting. You may also want to look at the Parallax document "Elements of PBASIC Style" which will help you to write your programs in a logical, and self-documenting manner. I'll let others provide a link to it, as I've not quite figured out the new Parallax web site - sorry.
{PBASIC 2.5}
Nickel_Inserted·· PIN IN4· ·'Pin port 4 detects nickels
Dime_Inserted··· PIN IN9·· ·'Pin port 9 detects dimes
Quarter_Inserted PIN IN15 ·'Pin port 15 detects quarters
Amount· VAR BYTE 'Total cents
Nickel·· CON 5· '5 cents
Dime··· CON 10 '10 cents
Quarter CON 25 '25 cents
Restart:···· 'Come here on a restart (if required), to zero out Amount
Amount = 0
Main_Loop:
IF (Nickel_Inserted = 1) THEN
·DEBUG "5 cents."
·Amount = Amount + Nickel
·HIGH 12
·PAUSE 500
·LOW 12
·GOTO Check_Total
ELSEIF (Dime_Inserted = 1) THEN
·DEBUG "10 cents."
·Amount = Amount + Dime
·HIGH 12
·PAUSE 500
·LOW 12
·GOTO Check_Total
ELSEIF (Quarter_Inserted = 1) THEN
·DEBUG "25 cents."
·Amount = Amount + Quarter
·HIGH 12
·PAUSE 500
·LOW 12
ELSE GOTO Main_Loop
Check_Total:
IF Amount < 25 THEN GOTO Main_Loop 'Wait for more coins if not 25 cents
Amount = 0
'If so, get on with it!
DEBUG "Start Bowling!"
'Exit to appropriate routine
---
Regards,
Bruce Bates
Team HAM