Need help with mathematical equations for LCD output

jst3712

Member
Hi there. If I have 3 word variables, each containing an ADC value, how do I translate the following mathematical equation from Excel into Picaxe language? I want the final figure to appear correctly on my Serial OLED (LCD) as a whole number with a percentage sign after it; no decimals. e.g. 64%

Example:
  • ADC1 = 801
  • ADC2 = 750
  • ADC3 = 80

=SUM(ADC1-ADC2)/ADC3*100
=SUM(801-750)/80*100

or a broken down version...

=SUM(801-750)
=51
=51/80
=0.6375
=0.6375*100
=63.75
=64

It's probably really simple. I've had a go, but had no luck; Forgot what code it was now :( Appreciate any assistance. Thanks!
 

AllyCat

Senior Member
Hi,

PICaxe Basic only handles integer numbers so your "0.6375" just becomes zero. That particular calculation can be "fixed" by changing the calculation order, i.e. Result = ADC1 - ADC2 * 100 / ADC3 which should give a truncated answer of 63. That can be then rounded to the nearest integer by adding half of the divisor, before the division, e.g.:

Code:
symbol Result = w0
symbol ADC1 = w1
symbol ADC2 = w2
symbol ADC3 = w3
ADC1 = 801
ADC2 = 750
ADC3 = 80

Result = ADC3 / 2 
Result = ADC1 - ADC2 * 100 + Result / ADC3   ;Can be written on multiple lines if preferred
sertxd(#Result,"%")
BUT, you need to define (and test for) the exact range of values to be expected from the ADCs. The maximum numereical value handled by PICaxe WORDS is still only 65,535 so if ADC1 - ADC2 exceeds 655 then the variable will overflow. Of course a small value of ADC3 may also cause an overflow (of 100%) as well.

Finally, What is "SUM" supposed to do? Excel expects to find a sequence of variables, e.g. SUM(A1 ; A2) or SUM(A1 : A10)

Cheers, Alan.
 
Last edited:

jst3712

Member
Hi,

PICaxe Basic only handles integer numbers so your "0.6375" just becomes zero. That particular calculation can be "fixed" by changing the calculation order, i.e. Result = ADC1 - ADC2 * 100 / ADC3 which should give a truncated answer of 63. That can be then rounded to the nearest integer by adding half of the divisor, before the division, e.g.:

Code:
symbol Result = w0
symbol ADC1 = w1
symbol ADC2 = w2
symbol ADC3 = w3
ADC1 = 801
ADC2 = 750
ADC3 = 80

Result = ADC3 / 2 
Result = ADC1 - ADC2 * 100 + Result / ADC3   ;Can be written on multiple lines if preferred
sertxd(#Result,"%")
BUT, you need to define (and test for) the exact range of values to be expected from the ADCs. The maximum numereical value handled by PICaxe WORDS is still only 65,535 so if ADC1 - ADC2 exceeds 655 then the variable will overflow. Of course a small value of ADC3 may also cause an overflow (of 100%) as well.

Finally, What is "SUM" supposed to do? Excel expects to find a sequence of variables, e.g. SUM(A1 ; A2) or SUM(A1 : A10)

Cheers, Alan.
Thank you Alan. Appreciate your reply.
I will look into this in detail tomorrow when I get a chance :)

Regarding the Excel formula, it was a "fluke" formula that ended up giving me the results I was expecting. It probably wasn't the ideal formula, but I was never good at math!
Looks like your "change of calculation order" suggestion might just do the trick, but I won't know for sure until I actually try it.
However, I'm a bit confused with your 'rounding' suggestion ... would you mind writing that up into multiple lines so I understand it better? Sorry!
Thanks.
 

AllyCat

Senior Member
Hi,

PICaxe maths works entirely "from left to right" (with no priority of multiply or divide before add or subtract) and with no availability of brackets. So converting to multiple statements (lines) needs another variable to be used, unless you are prepared/able to "destroy" (overwrite) one of the AD values. For example :
Code:
;.....
Symbol Intermediatevalue = w4
Result = ADC1 - ADC2     ; Difference between A/D values (beware that a "negative" result might occur) 
Result = Result * 100     ; Convert to percentage
Intermediatevalue =  ADC3 / 2        ; "One half" for final (integer) result
Result= Result + Intermediatevalue   ;  Round up/down
Result = Result / ADC3         ; Proportion (percentage) of input difference
The method of "rounding up / down" to the nearest integer is to add "one half" to a calculated value. But PICaxe can't work in halves, so we need to do the "rounding" before the final division. Therefore we add one half of the divisor, or 40 / 80 using your test values.

Well, a "SUM" of one value is not impossible to calculate (just pointless, like adding zero or multiplying by one) so Excel doesn't report it as an "error". But it does give a clue that some "trial and error" has been used. ;)

Cheers, Alan.
 

Bill.b

Senior Member
Hi Alan

the following is an example of passing the value in a word variable to the OLED display, You have to convert the value to ASCII before passing it to the display.
this example is part of program for displaying temperature.

Code:
symbol AsciiData1 	= b17		'Ascii data for display on LCD
symbol AsciiData2 	= b16		'Ascii data for display on LCD
symbol AsciiData3 	= b20		'Ascii data for display on LCD
symbol AsciiData4 	= b19		'Ascii data for display on LCD
symbol AsciiData5 	= b23		'Ascii data for display on LCD
symbol AsciiData6 	= b22		'Ascii data for display on LCD
symbol AsciiData3 	= b20		'Ascii data for display on LCD
symbol AsciiData4 	= b19		'Ascii data for display on LCD
symbol AsciiData5 	= b23		'Ascii data for display on LCD
symbol AsciiData6 	= b22		'Ascii data for display on LCD
symbol temperature	= w18		'temperature variable
symbol temperature2  	= w19		'tempory store - temperature
symbol OLED			= b.7		'Serial output to display
symbol Baud			= N2400	'Set serial output baud rate
Displaytemp:

	readtemp12 a.1,temperature2				'Read Temperature sensor DS18B20
  	temperature = temperature2 * 10
  	temperature = temperature/16
  	temperature = temperature - 20			'format temp data for deg C 
	serout OLED,Baud,(254,192)			'set to display curser to line 2
	bintoascii temperature,AsciiData5,AsciiData4,AsciiData3,AsciiData2,AsciiData1
	serout OLED,Baud,("Temperature ",AsciiData3,AsciiData2,".",AsciiData1," ",210,"C")
	return
If you require to display the full value of the word variable (AsciiDate5 to 1) some logic will be reqired to suppress leading zeros.


Bill
 

jst3712

Member
Hi guys. I appreciate your replies, and sorry for the delay in responding.

"Result = ADC1 - ADC2 * 100 / ADC3" (change in calculation order) has actually solved my problem. With what I will be using it for, I don't mind the number not being rounded.
Also there was no need to convert anything to ASCII for it to display on my OLED... I just use the 'serout' command and the LCD Firmware does the rest.

Thanks again.

*** RESOLVED ***
 
Last edited:
Top