Hey, wonder if someone could help me with a programming question on performing a max function on a list of floating point numbers.
I'm receiving a list of 32 floating point numbers over serial and then parsing the list into its 32 components. Next I am grouping the lists on a log scale and would like to return the maximum number in each category (a category can contain up to 17 floating point numbers).
I've attached the code I'm working with. At the bottom commented out is the section of the code I'm trying to write to find the maximum floating point number in each of LRA categories 3 to 5.
The code gets much smaller when you use an array instead of 32 variables. Note: C++ arrays start with index 0 so everything is one off from your numbering that started at 1.
const byte numChars = 255;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
float floatsFromPC[32];
float LRA[5];
const int LRAStartRange[5] = {0, 1, 3, 7, 15};
const int LRAEndRange[5] = {0, 2, 6, 14, 31};
boolean newData = false;
//============
void setup()
{
Serial.begin(115200);
Serial.println("Arduino is Ready");
Serial.println();
}
//============
void loop()
{
recvWithStartEndMarkers();
if (newData == true)
{
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
}
//============
void recvWithStartEndMarkers()
{
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false)
{
rc = Serial.read();
if (recvInProgress == true)
{
if (rc != endMarker)
{
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars)
{
ndx = numChars - 1;
}
}
else
{
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker)
{
recvInProgress = true;
}
}
}
//============
void parseData() // split the data into its parts
{
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, " "); // get the first part - the string
floatsFromPC[0] = atof(strtokIndx); // convert this part to a float
for (int i = 1; i < 32; i++)
{
strtokIndx = strtok(NULL, " "); // this continues where the previous call left off
floatsFromPC[i] = atof(strtokIndx); // convert this part to a float
}
}
float maxInRange(int start, int end)
{
float max = floatsFromPC[start];
for (int i = start + 1; i <= end; i++)
{
if (floatsFromPC[i] > max)
max = floatsFromPC[i];
}
return max;
}
void showParsedData()
{
for (int i = 0; i < 32; i++)
{
// Serial.print("Bin ");
// Serial.print(i+1);
// Serial.print(": "
Serial.print(floatsFromPC[i]);
}
Serial.println();
for (int i = 0; i < 5; i++)
{
LRA[i] = maxInRange(LRAStartRange[i], LRAEndRange[i]);
Serial.print("LRA");
Serial.print(i + 1);
Serial.print(' ');
Serial.println(LRA[i]);
}
}
Using an object that already stores floats and can tell you the max of its list..
#include <runningAvg.h>
runningAvg floatList(17); // An object that can store 17 floats.
char readBuff[80]; // A buffer for our typings.
int index; // Where the enxt char goes.
void setup(void) {
Serial.begin(57600); // Fire up the serial stuff.
index = 0; // Init the index.
}
void loop(void) {
if (Serial.available()) { // We got a char coming in?
readBuff[index] = Serial.read(); // Stuff it into the buffer.
readBuff[index+1] = '\0'; // Stuff in an end of string marked after it.
if (readBuff[index]=='\n') { // The last char was a newline?
floatList.addData(atof(readBuff)); // Read the string we got as a float. Add it to the list
Serial.print("Max value : "); // Lets show the user-
Serial.println(floatList.getMax()); // the max value of the list so far.
index = 0; // And we reset the index to zero for the next string.
} else { // Oh, we DIDN"T see an end of line?
index++; // Bump up the index for the next char.
}
}
}
You will need to grab LC_baseTools from the IDE library manager to get this to compile.
great, thanks so much for your replies, really helpful. John I ran your code and it works perfectly and yes thanks all, much simpler to put the floats into an array