PDA

View Full Version : high/low of 14 numbers



Christopher4187
- 14th December 2012, 21:27
I referenced this thread: http://www.picbasic.co.uk/forum/showthread.php?p=41702

I can't figure out how to get started. I can do it with a 2 or 3 numbers but then the code gets too large and I lose track. What I need to do is it take in 14 byte sized numbers. Then I need to average them, which I've done, but then I need to find the high and low numbers. The numbers a whole numbers and range from 18-25.

Can someone help me get started?

HenrikOlsson
- 14th December 2012, 22:24
Hi,
If all you need to do is "peak detect" (and not actually sort the values) then something like this might work

Values VAR BYTE[14] ' Array to store the 14 values, array is indexed 0-13.
HighValue VAR BYTE ' Highes value found
LowValue VAR BYTE ' Lowest value found
i VAR BYTE
Temp VAR BYTE

HighValue = 0
LowValue = 255

For i = 0 to 13
Temp = Values[i]
IF Temp > HighValue THEN HighValue = Temp ' Found new high value
IF Temp < LowValue THEN LowValue = Temp ' Found new low value
NEXT

LCDOUT $FE, 1 , "Low: ", #LowValue, " High: ", #HighValue

You can skip using the Temp variable and index the array directly if you want but it'll produce slightly larger code.

/Henrik.

Christopher4187
- 15th December 2012, 12:42
Thanks for the help Henrik. I don't understand your code so I have some questions/comments:

1. I am still using 2.50. Is the top line (Values VAR BYTE[14]) wanting to use arraywrite? I'm assuming your code, if written out, would be Values.1, Values.2......

2. I already store the 14 byte sized variables into D1, D2, D3....D14. Could this be a direct substitution?

3. Your code states "Values[i]." The "i" would be 14 in this case?

4. Last question. When you put a # before the lowvalue and highvalue, what exactly is that doing?

HenrikOlsson
- 15th December 2012, 13:34
Hi,

1. I am still using 2.50. Is the top line (Values VAR BYTE[14]) wanting to use arraywrite? I'm assuming your code, if written out, would be Values.1, Values.2......
No, it does declare an array but it does not use ARRAYWRTITE or ARRAYREAD so you should be fine. The individual values in the array are accessed like Values[0], Values[1] and so on.


2. I already store the 14 byte sized variables into D1, D2, D3....D14. Could this be a direct substitution?
Not directly, no. But if you don't want to re-write your code you can create aliases for each of your variables to actually use the declared array

Values VAR BYTE[14]
D1 VAR Values[0]
D2 VAR Values[1]
D3 VAR Values[2]
'...and so on.
This way, when you assign a value to D3 it'll actually be stored in the array.


3. Your code states "Values[i]." The "i" would be 14 in this case?
No, i is a variable which is used as the index in the FOR-NEXT loop. First time thru the loop i is 0 so it will access the first value in the array, next time thru the loop i is 1 so it will access the second value in the array and so on. Remember that arrays are indexed starting at 0 so Value[0] is the first value, Value[13] is the 14th value.


4. Last question. When you put a # before the lowvalue and highvalue, what exactly is that doing?
It converts the value to readable ASCII text, it's the same as using the DEC modifier.

/Henrik.

Christopher4187
- 16th December 2012, 01:21
Thanks for the help Henrik! It works very well! One question - Is there an advantage to using # instead of DEC?

HenrikOlsson
- 16th December 2012, 09:43
No, not that I know of. With DEC you can specify the number of digits, like DEC3 or print signed numbers with SDEC but as far as I know # is the same as DEC.

/Henrik.