Picaxe Calculator Project

dhulme

New Member
Hi all,

I am building a calculator using a PICAXE-18X chip for my A Level Systems and Control project.

I am currently trying to add two 1-digit numbers together. I am able to correctly take input from my keypad, display the number entered, and save the value of the key entered to a variable.

However when I try to add two variables, and output the result, it fails. My code for the main sub is below:

<code>
Symbol key_number = b0
Symbol value_1 = b1
Symbol value_2 = b2
Symbol answer = b3


main:
do
gosub check_for_key
loop until key_number !=0

gosub display_number


value_1 = key_number
key_number = 0

wait 2

do
gosub check_for_key
loop until key_number !=0

wait 2

gosub display_number
value_2 = key_number
key_number = 0

answer = value_1 + value_2

wait 2

key_number = answer
gosub display_number
</code>

Both the 'check_for_key' and 'display_number' subroutines work fine. Any ideas?

All help appreciated,
Thanks in advance,
David
 

BeanieBots

Moderator
You've not included the sub-routines.
Need those to check for any strange interaction such as variable assignment and/or missing returns, nesting etc.

What happens after "gosub display_number" at the end of your code?
Does it drop into the start of the subroutines?
 

westaust55

Moderator
Hi david,

first a couple of housekeeping type issues.

1. you need to use square brackets around the code markers to make it appear in its own sub-window. That is: [code] and [/code]

2. you should try to indent your code at the various nesting levels and labels. Here is your maincode with examples for the two subroutines as well:
Code:
Symbol key_number = b0
Symbol value_1       = b1
Symbol value_2       = b2
Symbol answer        = b3


main:
    do
      gosub check_for_key
    loop until key_number !=0

    gosub display_number

    value_1 = key_number
    key_number = 0

    wait 2

    do
      gosub check_for_key
    loop until key_number !=0

    wait 2

    gosub display_number

    value_2 = key_number
    key_number = 0

    answer = value_1 + value_2

    wait 2

    key_number = answer
    gosub display_number

    GOTO main   ; [B][COLOR="Red"]is this line missing from your program ????? - refer Beaniebots comments[/COLOR][/B]


check_for_key:
    [COLOR="red"];   some action here[/COLOR]
    return
     
         
display_number:
    [COLOR="red"];   some action here[/COLOR]
    return

3. Now to the technical question:

Are your 1-digit numbers straight binary or are they ASCII encoded.
For example is the value "1" represented in a variable as 1 or as $31 = decimal 59?

The addition function in your code (answer = value_1 + value_2
) is correct, BUT the numbers must be in straight binary.
 

lbenson

Senior Member
The following (your code with the simplest added input and output routines), works in the simulator. What part of your code doesn't give the answer you expect based on what input?
Code:
Symbol key_number = b0
Symbol value_1 = b1
Symbol value_2 = b2
Symbol answer = b3

main:
  do
    gosub check_for_key
  loop until key_number !=0

  gosub display_number


  value_1 = key_number
  key_number = 0

  wait 2

  do
    gosub check_for_key
  loop until key_number !=0

  wait 2

  gosub display_number
  value_2 = key_number
  key_number = 0

  answer = value_1 + value_2

  wait 2

  key_number = answer
  gosub display_number
  goto main
  
check_for_key:
  serin 1,T2400_4,key_number
  return

display_number:
  sertxd(#key_number,CR,LF)
  return
 

dhulme

New Member
Hi all, thanks for the responses. Having examined my problem more thoruhgly, I will try and explain what is going wrong.

Firstly, below is my complete code:

Code:
Symbol key_number = b0
Symbol value_1 = b1
Symbol value_2 = b2
Symbol answer = b3


main:
	do
		gosub check_for_key
	loop until key_number !=0
	
	gosub display_number
	
	
	value_1 = key_number
	key_number = 0
	
	wait 2
	
	do
		gosub check_for_key
	loop until key_number !=0
	
	wait 2 'This is a line that it skips
	
	gosub display_number
	value_2 = key_number
	key_number = 0
	
	answer = value_1 + value_2
	
	wait 2
	
	key_number = answer
	gosub display_number
	
	wait 5
	end



check_for_key:
	high 7		'on left collumn
	if pin6 = 1 then
		key_number = 7
	endif
	if pin7 = 1 then
		key_number = 4
	endif
	if pin0 = 1 then
		key_number = 1
	endif
	if pin1 = 1 then
		key_number = 0
	endif
	low 7
	
	high 6
	if pin6 = 1 then
		key_number = 8
	endif
	if pin7 = 1 then
		key_number = 5
	endif
	if pin0 = 1 then
		key_number = 2
	endif

	low 6
	
	high 4
	if pin6 = 1 then
		key_number = 9
	endif
	if pin7 = 1 then
		key_number = 6
	endif
	if pin0 = 1 then
		key_number = 3
	endif

	low 4
	return
	
display_number: 'key_number as input
	low 3
	low 2
	low 1
	low 0
	select b0 'key_number
	case 0
		let pins = %00001010
	case 1
		let pins = %00000001
	case 2
		let pins = %00001000
	case 3
		let pins = %00001001
	case 4
		let pins = %00000100
	case 5
		let pins = %00000101
	case 6
		let pins = %00001100
	case 7
		let pins = %00001101
	case 8
		let pins = %00000010
	case 9
		let pins = %00000011
	endselect
	return
I have tried the program in the Simulator and everything works fine. (i.e. when 4 and 4 are entered, answer becomes 8, and the correct outputs are on to send '8' to the 7-seg decoder). However, when I download my program to my circuit, it does not function correctly.

It accepts the first number, but then seems to completly ignore the 'wait 2' command (line 24, indexed with a comment) and skips to displaying the number on the screen. What can be going wrong?

(And as for ASCII or Binary, it is stored as a 1 not a '1' (charactor),and reads as binary in the simulator)

Thanks again in advance,
David
 

westaust55

Moderator
while not solving your problem, I do spy the following point that needs attention.

The subroutine check_for_key: is checking for a key value of zero (0).
The subroutine display_number: includes a case for the value of zero (0).
Yet the main program has the loops
do
gosub check_for_key
loop until key_number !=0

will not accept / pass on a value of zero. :eek:
 

westaust55

Moderator
Next problem:

you can enter two numbers in the range 0 to 9.
The answer can be in the range 0 to 18, but your subroutine display_number: is only set to display the values 0 to 9.

Therefore it will "fail" to display all possible answers.
 

dhulme

New Member
Hi westaust55, the reason the check_for_key sub has a case for 0, is due to this program being part of a larger program, which was designed to enter 2 digit numbers or more.

And while I am aware of the problem adding numbers such as 6 + 5, sums such as 2 + 3 do not output the result on the display.

This program is a test for the larger program, to ensure I can add one digiit numbers before I begin adding larger numbers.

Sorry I didn't orginially make this clear,
Regards
David
 

dhulme

New Member
I am still unsure why it wont output answers such as 5... can anoyone suggest any possible reason for this?

David
 

hippy

Ex-Staff (retired)
Remove all "gosub check_for_key" and replace with "key_number=2" ( you can vary the actual number ). When that runs on a real PICAXE does it display the correct results ?

If so the problem is in your check_for_key routine, if not it's in the rest of the code.

Also, are you sure all the "pins=%xxxxxxxx" are right in your display routine ? Have you tested all combinations in the simulator ?
 
Last edited:

westaust55

Moderator
Then you could also add a DEBUG statement into the program after each fetching of a key number and prior to each display call and with the programming cable still conencted, watch what values are displayed in the PC screen. You may need longer Wait/pause times so you can see what happens after the second number entry.
 

dhulme

New Member
Hi all,

Thanks hippy for your respsone. I have tried was you have suggested. By removing the cehck_for_key routine and creating a simple 1 + 2 program, the result does still not display on the screen, rather only 1 and 2 are shown.

I then made an even simpler program, trying to display a 1 then a 2 then a 3. However this doesnt work either (both work in the simulator though).

I have included the simplified code below:

Code:
Symbol key_number = b0
Symbol value_1 = b1
Symbol value_2 = b2
Symbol answer = b3


main:
Symbol key_number = b0
Symbol value_1 = b1
Symbol value_2 = b2
Symbol answer = b3


main:
	key_number = 1
	gosub display_number
	
	wait 2
	
	key_number = 2
	gosub display_number

	
	wait 2
	
	key_number = 3
	gosub display_number
	
	wait 5
	
	end

	
display_number: 'key_number as input
	low 3
	low 2
	low 1
	low 0
	select b0 'key_number
	case 0
		let pins = %00001010
	case 1
		let pins = %00000001
	case 2
		let pins = %00001000
	case 3
		let pins = %00001001
	case 4
		let pins = %00000100
	case 5
		let pins = %00000101
	case 6
		let pins = %00001100
	case 7
		let pins = %00001101
	case 8
		let pins = %00000010
	case 9
		let pins = %00000011
	endselect
	return
As for the display_number sub, I wrote a program i while back that took input from the user for different keys and that displayed the correct numbers, so they must be all coded correctly.

I have no idea whats going on!
Anyone got any ideas?

David
 
Last edited:

westaust55

Moderator
It is sounding more like a hardware problem

Please upload your circuit diagram for the output to the display.
You have mentioned 7 seg displays in an earlier thread on your project.
 

hippy

Ex-Staff (retired)
Agreed, it is either a hardware error or what you are setting your pins to does not suit the hardware you are using.
 
Top