Can someone hep me with my coding.

I am currently making a cricket scoring device to use at my club, however I have never coded before and keep on getting syntax errors. It would be very helpful if someone could either help me write it or point out why I have syntax errors.
 

wayne_weedon

New Member
Try this. do not really have time to retype it all from an image. You could copy and paste the text from your editor here.

Wayne..
Code:
'Symbols/Aliases

'Pins
symbol plus_1run = pinb.1
symbol plus_4runs = pinb.4
symbol plus_6runs = pinb.6
symbol minus_1run = pinb.7
symbol plus_1wicket = pinb.0
symbol plus_1ball = pinb.2
symbol minus_1ball = pinb.5

'vars
symbol score = b0
symbol balls = b1
symbol wickets = b2
symbol overs = b3

dirsB = 0

'Init variables
score = 0
balls = 0
wickets = 0
overs = 0

do

if plus_1run = 1 then
	inc score
	gosub debounce 'Debounce (may need tweaking)
	elseif plus_4runs = 1 then
		score = score + 4
		gosub debounce
		elseif plus_6runs = 1 then
			score = score + 6
			gosub debounce
			elseif plus_1wicket = 1 then
				inc wickets
				gosub debounce
				elseif plus_1ball = 1 then
					inc balls
					gosub debounce
					elseif minus_1ball = 1 then
						dec balls
						gosub debounce
						elseif minus_1run = 1 then
							dec score
							gosub debounce
endif

overs = balls / 6

if wickets = 10 then
	gosub last_innings
endif

loop

last_innings:

' do something here!

return

debounce:

	pause 30 'Debounce (may need tweaking)

return
 
Last edited:

wayne_weedon

New Member
You will probably need a word variable for score. such as w0, will need all the byte vars moved up one or just move balls from b1 to b4.

Also your overs counter will need revision as it would only increment overs for the 1st 6th ball. Would probably need a tally in another variable. Or a statement such as;

overs = balls / 6

instead of the statements

if balls = 6 then
inc overs
endif

UPDATE: Already altered the overs calculation in the code included above.


Wayne
 
Last edited:

AllyCat

Senior Member
Hi Alex,

here is the code at the moment. I then need to code an OLED but am not sure on how to.
Welcpme to the forum. As said above it's best to use the "copy for forum" in the Program Editor and paste directly into the forum thread. Then we can copy, simulate, edit and paste back any changes that we propose. Also put in plenty of "Comments" (after a ; or ' character) in each line to indicate what is the intended purpose of the instruction.

Yes, you will get lots of Syntax Errors because each instruction must use the keywords in exactly the correct format. Many are explained with examples in the On-Line Basic Commands Syntax at the top of the forum page.

You probably need to start with lots of SYMBOL commands to define what you want to do. For example B.1 is an Output pin, typically to allow the PICaxe to light a LED (I'm not sure that's what you want PLUS_1RUN to do). pinB.0 defines an input , so you are likely to use that (or its symbol) in an IF ... THEN construction.

Variables are also best symbolised, for example : SYMBOL Team_Total = w0 ; A "Word" variable because it could be a large number : SYMBOL Batsman_1_Runs = b2 ; A byte value, maximum 255: etc. Quotation marks " " are mainly used to send the specific enclosed Characters to an Alphanumeric Display (Like an OLED). AND and TO (after a FOR) have very specific mreanings.

So the skeleton of the program might look like:

Code:
#PICAXE  20M2   ;  The "target" PICAXE can be important information 

SYMBOL ONE_RUN_SCORED = pinB.0    ;  Pushbutton Input 
SYMBOL TEAM_RUNS = w0                 ;  Total number of runs scored so far
SYMBOL BATSMAN_1_RUNS = b2         ; Number of runs scored by batsman A
;  etc...
DO
   IF ONE_RUN_SCORED = 1 THEN           ; Pushbutton pressed = a "1" level
       TEAM_RUNS = TEAM_RUNS + 1
       BATSMAN_1_RUNS = BATSMAN_1_RUNS + 1
       WAIT 5                                       ; Wait for the button to be released
   ENDIF
;  etc...
LOOP
Generally, you will send data to an OLED display with SEROUT commands, but it's better to start to develop your program with simple SERTXD instructions (you may need to put a #TERMINAL 4800 command at the top of the program).

Cheers, Alan.
 

hippy

Technical Support
Staff member
It would be very helpful if someone could either help me write it or point out why I have syntax errors.
The bottom line is that you appear to have invented the programming language which you want to use rather than using the programming language which the PICAXE supports.

That's not a problem, is often the way coding is done using 'pseudo code' which indicates what should be doing if not exactly how it is done.

If you copy what you have for the forum and post it as suggested, members can go through that and more easily say how what you have needs to be for the PICAXE.
 
Try this. do not really have time to retype it all from an image. You could copy and paste the text from your editor here.

Wayne..
Code:
'Symbols/Aliases

'Pins
symbol plus_1run = pinb.1
symbol plus_4runs = pinb.4
symbol plus_6runs = pinb.6
symbol minus_1run = pinb.7
symbol plus_1wicket = pinb.0
symbol plus_1ball = pinb.2
symbol minus_1ball = pinb.5

'vars
symbol score = b0
symbol balls = b1
symbol wickets = b2
symbol overs = b3

dirsB = 0

'Init variables
score = 0
balls = 0
wickets = 0
overs = 0

do

if plus_1run = 1 then
	inc score
	gosub debounce 'Debounce (may need tweaking)
	elseif plus_4runs = 1 then
		score = score + 4
		gosub debounce
		elseif plus_6runs = 1 then
			score = score + 6
			gosub debounce
			elseif plus_1wicket = 1 then
				inc wickets
				gosub debounce
				elseif plus_1ball = 1 then
					inc balls
					gosub debounce
					elseif minus_1ball = 1 then
						dec balls
						gosub debounce
						elseif minus_1run = 1 then
							dec score
							gosub debounce
endif

overs = balls / 6

if wickets = 10 then
	gosub last_innings
endif

loop

last_innings:

' do something here!

return

debounce:

	pause 30 'Debounce (may need tweaking)

return
Is this correct?

scoremain:
If "SCORE" = 100/200/300/400/500
goto flash

flash:
high C.6 and C.7
pause 500
low C.6 and C.7
high C.6 and C.7
pause 500
low C.6 and C.7
high C.6 and C.7
pause 500
low C.6 and C.7
high C.6 and C.7
pause 500
low C.6 and C.7
goto main
 

pxgator

Senior Member
Hi Alex,

You're getting closer, but not quite yet. Please, please, please re-read AllyCat's post #7.
He gives a very clear explanation on what you need to do.
Cheers and welcome to the forum.
 

wayne_weedon

New Member
What exactly does the 'debounce' do?

I was assuming you intend to use switches to enter data into your program. In simple terms mechanical switches will require a debounce as a single press will often register as multiple switching cycles. A small delay will often suffice to allow the microcontroller to miss the contact bounce.
 

wayne_weedon

New Member
Is this correct?

scoremain:
If "SCORE" = 100/200/300/400/500
goto flash

flash:
high C.6 and C.7
pause 500
low C.6 and C.7
high C.6 and C.7
pause 500
low C.6 and C.7
high C.6 and C.7
pause 500
low C.6 and C.7
high C.6 and C.7
pause 500
low C.6 and C.7
goto main

Not Quite. Using similar logic your code might translate into

Code:
If score = 100 or score = 200 or score = 300 or score = 400 or score = 500 then 
	gosub flash
endif


flash:

high C.6,C.7
pause 500
low C.6,C.7
high C.6,C.7
pause 500
low C.6,C.7
high C.6,C.7
pause 500
low C.6,C.7
high C.6,C.7
pause 500
low C.6,C.7

return
However there is a problem with this even though it compiles without error you would not register a new century if say the batsman was at 98 and then scored a four, this would be 102 obviously but the code is expecting exactly 100. Without giving you a solution perhaps think about it and see if you can work out another way. There would be a few solutions to this.

I am happy to help further but its always good to try yourself first. I am trying to stay as close to your original pseudo code as possible.


Wayne...
 

wayne_weedon

New Member
Alex

Not sure how far you have been able to get with this but I had 10 minutes spare to tweak the code a little to limit the issues with the century detection.

Code:
'Symbols/Aliases

'Pins
symbol plus_1run = pinb.1
symbol plus_4runs = pinb.4
symbol plus_6runs = pinb.6
symbol minus_1run = pinb.7
symbol plus_1wicket = pinb.0
symbol plus_1ball = pinb.2
symbol minus_1ball = pinb.5

'vars
symbol score = w0
symbol balls = b2
symbol wickets = b3
symbol overs = b4
symbol centuries = b5

dirsB = 0

'Init variables
score = 0
balls = 0
wickets = 0
overs = 0
centuries = 0

do 'Main loop

	if plus_1run = 1 then
		inc score
		gosub debounce
		elseif plus_4runs = 1 then
			score = score + 4
			gosub debounce
			elseif plus_6runs = 1 then
				score = score + 6
				gosub debounce
				elseif plus_1wicket = 1 then
					inc wickets
					gosub debounce
					elseif plus_1ball = 1 then
						inc balls
						gosub debounce
						elseif minus_1ball = 1 then
							dec balls
							gosub debounce
							elseif minus_1run = 1 then
								dec score
								gosub debounce
	endif

	overs = balls / 6 'Calculate number of Overs

	if wickets = 10 then
		gosub last_innings
	endif

	'Check for new centuries scored up to 5 - Only execute each once
	If score >= 100 and centuries = 0 then
		inc centuries
		gosub flash
		elseif score >= 200 and centuries = 1 then
			inc centuries
			gosub flash
			elseif score >= 300 and centuries = 2 then
				inc centuries
				gosub flash
				elseif score >= 400 and centuries = 3 then
					inc centuries
					gosub flash
					elseif score >= 500 and centuries = 4 then
						inc centuries
						gosub flash

	
	endif

	

loop 'Repeat Main Loop

last_innings:

	' do something here!

return

debounce: 'Try to ensure only one button press registered

	gosub log_updates
	pause 30 'Debounce (value may need tweaking)

return

flash:

	gosub log_updates
	high C.6,C.7
	pause 500
	low C.6,C.7
	high C.6,C.7
	pause 500
	low C.6,C.7
	high C.6,C.7
	pause 500
	low C.6,C.7
	high C.6,C.7
	pause 500
	low C.6,C.7

return

log_updates:

	sertxd ("Balls: ",#balls," Overs: ",#overs," Wickets: ",#wickets,cr,lf)
	sertxd ("Score: ",#score," Centuries: ",#centuries,cr,lf)
	
return
I have added an extra variable (centuries) to keep a tally of how many have occurred to ensure each is registered only once. Also a routine to help you monitor the score in the terminal window (Could be edited to suit OLED display). Try simulating it.


Wayne..
 
Last edited:

wayne_weedon

New Member
Update 25th Feb : Thought this was worth a little more work. I might set this problem or something similar as a task for some of my students so thanks for the inspiration!

Here is another version which has been rearranged to be more structured and also the calculation of new centuries is more sensible, just don't like too many nested if/elseif statements! As well as a more streamlined flash subroutine. It now uses about 95 bytes less.

Code:
'Symbols/Aliases

'Pins
symbol plus_1run = pinb.1
symbol plus_4runs = pinb.4
symbol plus_6runs = pinb.6
symbol minus_1run = pinb.7
symbol plus_1wicket = pinb.0
symbol plus_1ball = pinb.2
symbol minus_1ball = pinb.5

'vars
symbol score = w0
symbol balls = b2
symbol wickets = b3
symbol overs = b4
symbol centuries = b5
symbol next_century = b6
symbol tmp_var = b7

dirsB = 0

'Init variables
score = 0
balls = 0
wickets = 0
overs = 0
centuries = 0
next_century = 1

do 'Main loop

	if plus_1run = 1 then
		inc score
		gosub debounce
		elseif plus_4runs = 1 then
			score = score + 4
			gosub debounce
			elseif plus_6runs = 1 then
				score = score + 6
				gosub debounce
				elseif plus_1wicket = 1 then
					inc wickets
					gosub debounce
					elseif plus_1ball = 1 then
						inc balls
						gosub debounce
						elseif minus_1ball = 1 then
							dec balls
							gosub debounce
							elseif minus_1run = 1 then
								dec score
								gosub debounce
	endif	

	if wickets = 10 then
		gosub last_innings
	endif	

loop 'Repeat Main Loop

last_innings:

	' do something here!

return

debounce: 'Try to ensure only one button press registered

	gosub log_updates 'something has changed so update display
	pause 10 'Debounce (value may need tweaking)

return

flash:

	for tmp_var = 0 to 3
		high C.6,C.7
		pause 500
		low C.6,C.7
		pause 500
	next tmp_var

return

log_updates: 'Calculate derived stats and send all to display

	gosub calc_centuries
	gosub calc_overs
	sertxd ("Balls: ",#balls," Overs: ",#overs," Wickets: ",#wickets,cr,lf)
	sertxd ("Score: ",#score," Centuries: ",#centuries,cr,lf)
	
return

calc_overs:

	overs = balls / 6 'Calculate number of Overs

return

calc_centuries:

'Check for new centuries scored - Only execute each once
	tmp_var = score / 100
	If tmp_var = next_century then
		inc centuries
		inc next_century
		gosub log_updates
		gosub flash
		
	endif

return
 

AllyCat

Senior Member
Hi,

just don't like too many nested if/elseif statements!
Agreed, but IMHO the indenting is "wrong", the ELSE{IF}s should be indented by the same as the initial IF. It's then also rather more obvious that the "gosub debounce"s can be simplified:

Code:
	if plus_1run = 1 then
		inc score
;		gosub debounce		; "Commented Out" line
	elseif plus_4runs = 1 then
		score = score + 4
;		gosub debounce
	elseif plus_6runs = 1 then
		score = score + 6
;		gosub debounce
	elseif plus_1wicket = 1 then
		inc wickets
;		gosub debounce
	elseif plus_1ball = 1 then
		inc balls
;		gosub debounce
	elseif minus_1ball = 1 then
		dec balls
;		gosub debounce
	elseif minus_1run = 1 then
		dec score
;		gosub debounce
	endif
		gosub debounce			; Combined from above
"debounce" is probably the wrong name now (and a 10ms delay is unlikely to do anything useful) so something like "update_display" might be more meaningful. Contact bounce is (now) unlikely to be a problem, but "multiple detection" of a button pressed for too long might be: A typical solution is to test that the button has been released before repeating the main loop.

How are you planning to enter (say) two runs, pressing the "plus_1run" button twice? Note that the "flash" routine will "lock out" the reading of any pushbutton for over 4 seconds. Presumably the "minus_1ball" is to handle a "No Ball" ?

Cheers, Alan.
 

hippy

Technical Support
Staff member
I would approach the task slightly differently, have a single routine 'GetButton' which scanned and waited for a new button push, debounced the buttons, discarded multiple pushes, then assigned a constant identifier to a 'buttonPushed' variable, and use a SELECT-CASE to handle which button was pushed. There would be a final routine which puts the latest data on the scoreboard ...

Code:
Do
  Gosub GetButton
  Select Case buttonPushed
    Case BTN_PLUS_1_RUN  : runs = runs + 1
    Case BTN_PLUS_4_RUNS : runs = runs + 4
    :
  End Select
  Gosub UpdateScoreboard
Loop
That way you can develop, debug and test the button push code entirely separately from the score handling and display functionality. The scoring code can be nice, clean, uncluttered and fairly simple.

Get something basic working first, just counting runs. Then improve that to add in centuries counting and other stuff.
 

wayne_weedon

New Member
Always good to see others ideas. I started by trying to keep as close to the OPs original non functioning code and just worked from there.
 

wayne_weedon

New Member
"debounce" is probably the wrong name now (and a 10ms delay is unlikely to do anything useful) so something like "update_display" might be more meaningful. Contact bounce is (now) unlikely to be a problem, but "multiple detection" of a button pressed for too long might be: A typical solution is to test that the button has been released before repeating the main loop.

How are you planning to enter (say) two runs, pressing the "plus_1run" button twice? Note that the "flash" routine will "lock out" the reading of any pushbutton for over 4 seconds. Presumably the "minus_1ball" is to handle a "No Ball" ?
I had considered that too, but left it there so there were not too many changes. As that routine included some calculations perhaps a debounce would not even be necessary, but depends on how long those would take. I originally put about 30 ms delay in there.

The OP could handle the flash routine easily without any delays if he is using a M2 chip. Generally I forbid my own students to use any inbuilt pauses for this very reason!
 
Top