News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

How to Convert Array Elements to an Integer?

Started by Craig, Aug 29, 2023, 07:03 PM

Previous topic - Next topic

Craig

Hello

I have been looking at a way of Loading Numerical Values of an Array into a Float or Word Variable.
I have a Temperature Sensor which gives say 35.8°C and I have set an alarm at 37.9°C
I have written the Alarm Numeric Numbers which are 37.9°C into individual Byte Variables Via the Keypad.
Each Numeric Value is Held in it's Own Variable as Below. I can Push these Individually into Each Element
of an Array and access them Individually.

BTemp1 = 3  BTemp2 = 7  Btemp3 = 9

This works Fine as I can Print each digit and it works Fine.
My problem is that I cannot get these values Pushed back together into a Single Float or Word Variable as they need to be 379 in that order.
If I put them in as below by adding them then they will give 19. How can I put them into a single Integer in that order.
I need to monitor the Temperature and if it reaches the Alarm Temperature then trigger an alarm.

I have tried numerous things but, cannot seem to find a way of Concatenating the Values.

'------------------------------------------------------------------------------------------ 
  FHold[0] = BTemp1 
  FHold[1] = BTemp2
  FHold[2] = BTemp3
 
  Print At 0,140,"Fhold:",Dec2 FHold[0],":",Dec2 FHold[1],":",Dec2 FHold[2]
 
  For J = 0 To 3
  BufferArray[J] = FHold[0] + FHold[1] + FHold[2]
  ATemp = BufferArray[J]
  TempAlarm =  ATemp
  Next J

  '---------------------------------------------------------------------------------------

 This Prints 19 which is wrong as it is just Adding 3+7+9 = 19, I need to find a way to get it to
 379 as a complete numeric number which I can use to do a Comparison of the Actual Temperature and the Alarm Temperature.

 Any Help is very much appreciated
 Regards
 Craig 












trastikata

Dim fTemp As Float

fTemp = (100*bTemp1 + 10*bTemp2 + bTemp3)/10

Craig

Thanks very much for your help Trastikata, a nice simple solution.

tumbleweed

Just be careful using floats. Because of the inaccuracy it can fail if you try to compare "exact values".
You might be better off using words and representing 37.9 as 379.

The code would certainly be shorter and faster.
 

John Lawton

Definitely, much better to scale your readings appropriately and use integer maths.

Craig

Thanks very much Tumbleweed and John, I much prefer using Words.
regards
Craig


top204

#6
If using the value for comparisons, as stated above, it is safer to use integer calculations, and a lot faster. Remember; Floating Point is a method of compacting and encoding a value and is not 100% accurate. Even large bit sized floating point is never 100% accurate, as some space craft have discovered when they miss a planet or plunge into a planet's surface when many calculations each create a slightly different value and they each use the slightly different values in the next calculation, leading to a very different calculation result after a few weeks or months or years in space. :-)

Below is a simple demonstration of how a procedure could be used for the conversion of an array's elements into an unsigned integer value with a maximum of 999:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes pParam1 PIC Tick!
'
' Demonstrate converting the values held sequentially in an array's elements into an unsigned integer
'
' Written for the Positron8 compiler by Les Johnson.
'
    Device = 18F25K20                               ' Tell the compiler what device to compile for
    Declare Xtal = 16                               ' Tell the compiler what frequency the device will be operating at (in MHz)
'
' Setup USART1
'
    Declare Hserial1_Baud = 9600                    ' Set the Baud rate to 9600
    Declare HRSOut1_Pin = PORTC.6                   ' Set the TX pin
'
' Create some variables for the demo
'
    Dim bArray[3] As Byte
    Dim MyWord    As Word
   
'-------------------------------------------------------------------------
' The main program starts here
' Convert the sequence of values held in a byte array into an integer value and display it on a serial terminal
'
Main:
    bArray = 9, 8, 7                                ' Load the array with some values to convert
   
    MyWord = ConvArrToInt(bArray)                   ' Convert the array's elements to an unsigned integer
    HRSOutLn Dec MyWord                             ' Display the integer on a serial terminal (it will display the value of 987)

'-------------------------------------------------------------------------
' Convert the values held in sequence in a byte array to an unsigned integer
' Input     : pArrAddr holds the address of the array containing the values. (maximum values are 9, 9, 9)
' Output    : Returns the converted unsigned integer value. Maximum value is 999
' Notes     : None
'
Proc ConvArrToInt(ByRef pArrAddr As Word), Word       
    Result = Ptr8(pArrAddr++) * 100                 ' Read the first element and multiply it by 100
    Result = Result + (Ptr8(pArrAddr++) * 10)       ' Read the second element and multiply it by 10, then add it to the Result
    Result = Result + Ptr8(pArrAddr++)              ' Read the third element and add it to the Result
EndProc

Craig

Thanks Very Much for your Help Les Very much appreciated
Regards
Craig