There are plenty of ways to do it but the Arduino UNO it probably not a good place to do it unless your total data size is quite limited. Where is the data coming from? Where are the results going? How many data items do you need to support? How many categories? What's the average and maximum size of a category name?
the serial console set at 115200 bauds should print whatever is the real average for each item...
of course you need to listen to @robtillaart advice and ensure you have enough memory for what you want to achieve
As @johnwasser says, storing the data if this is just to compute the sum and average later on is not a good idea. Just sum them in a variable as they come (a bit like I do in the code above) and count how many you got for each bucket as they come in and then you don't have a memory constraint (make sure you use the right data type able to represent the sum of all the samples correctly)
Thanks for the replies johnwasser and robtillaart!
The data will come in from an external radio connected through the Serial Port, as well as a Pixhawk's altitude through an Analog Port. Unfortunately I'm stuck with the UNO for this project.
As for the data location, is it alright for me to leave it in SRAM? The UNO will be constantly powered by the Pixhawk drone's battery, but when it lands, UNO will be connected to a computer by USB to run a sketch for this program to find the averages.
Lastly, each Item are labelled as "bikeObike1" , 2, 3, so on.. I am expecting a max of 5 items with no more than 6 rows of data each. The data array will be in the 2 columns shown, and from what i just mentioned, max of 30 rows.
Yes J-M-L, I just tried it, it works well! You're pretty good at this!
Just to check, this code requires me to input the S/N of my items in the first line?
Also, if i have another sketch which writes its data to an array in SRAM, does this code read from the SRAM as well?
If you would like, I can elaborate more on how this project actually works.
Hi J-M-L, thank you for your help around 2 weeks ago on this forum,
I have updated the code but I am not sure if i am doing this correctly.
uint8_t numOfFloor = {12}; // Input number of floors
// Declare item1, item2, item3, etc. (Do Not Remove "maxItems")
enum {bikeObike1 , bikeObike2, bikeObike3, bikeObike4, maxItems};
String dataIn; // String to temporarily store data from UART
int cueToOutput = 8; // Pin 8 indicates drone has landed
int altInput = 4; // Pin 4 used for Altitude input from pixhawk
float altIn; // altIn stores current altitude of pixhawk
int sonarInput = 5; // Pin 5 activated when signal received from sonar
int rows = 1; // Variables for rows/columns of data array
float maxAlt; // maxAlt stores height of building
struct measure_t
{
String serialNum;
float alt;
};
struct measure_t measures[0][2];
void setup()
{
pinMode(cueToOutput, INPUT_PULLUP);
pinMode(altInput, INPUT);
pinMode(sonarInput, INPUT_PULLUP);
Serial.begin(57600);
Serial.println("Start");
}
void loop()
{
/* measure_t measures[] = {
{bike1, 10},
{bike2, 22},
{bike2, 22.5},
{bike3, 31},
{bike3, 33},
{bike2, 21},
{bike1, 11.2},
{bike1, 10.7},
};
*/
altIn = analogRead(altInput);
while (Serial.available())
{
char UART;
UART = Serial.read();
dataIn += UART;
//Process message only when new line character is received
if (UART == '\n')
{
// Serial.print(dataIn);
//struct measure_t
measures[rows][2];
rows ++;
measures[rows][1].serialNum = dataIn;
measures[rows][2].alt = altIn;
dataIn = ""; // Clear received buffer
}
}
if (digitalRead(sonarInput) == 0)
{
maxAlt = altIn;
}
if (digitalRead(cueToOutput) == 0)
{
const uint16_t nbMeasures = sizeof(measures) / sizeof(measures[0]);
float measureSum[maxItems] = {0L};
float measureCount[maxItems] = {0};
for (uint16_t i = 0; i < nbMeasures; i++)
{
measureSum[measures[i].serialNum] += measures[i].alt;
measureCount[measures[i].serialNum]++;
}
for (byte i = 0; i < maxItems; i++)
{
if (measureCount[i] != 0)
{
float avgAltPerItem = {measureSum[i] / measureCount[i]};
float estFloorPerItem = {(avgAltPerItem / maxAlt)*numOfFloor};
Serial.print(F("Average Alt for Item"));
Serial.print(i+1);
Serial.print(F(" = "));
Serial.println(avgAltPerItem);
Serial.print(F("Estimated floor for Item"));
Serial.print(i+1);
Serial.print(F(" is "));
Serial.println(estFloorPerItem);
Serial.println();
}
else
{
Serial.print(F("No measure exists for Item"));
Serial.println(i+1);
Serial.println();
}
}
Serial.print(F("Total height of building is "));
Serial.print(maxAlt);
Serial.println(F(" meters."));
while(1){}
}
}
I get the error message (request for member 'serialNum' in 'measures', which is of non-class type 'measure_t [2]') at line 82. - measureCount[measures*.serialNum]++; This is the kind of data my UNO is receiving: (Ignore the "Start", its just my indicator)
_UART rx data*_ Best Regards, Adrian
I have updated the code but I am not sure if i am doing this correctly.
You are not. You need to post your questions and your issues at the same place you post your code. If you want help on dropbox, move your questions there.
I get the error message (request for member 'serialNum' in 'measures', which is of non-class type 'measure_t [2]') at line 82. - measureCount[measures.serialNum]++;
uint8_t numOfFloor = {12}; // Input number of floors
// Declare item1, item2, item3, etc. (Do Not Remove "maxItems")
enum {bikeObike1 , bikeObike2, bikeObike3, bikeObike4, maxItems};
String dataIn; // String to temporarily store data from UART
int cueToOutput = 8; // Pin 8 indicates drone has landed
int altInput = 4; // Pin 4 used for Altitude input from pixhawk
float altIn; // altIn stores current altitude of pixhawk
int sonarInput = 5; // Pin 5 activated when signal received from sonar
int rows = 1; // Variables for rows/columns of data array
float maxAlt; // maxAlt stores height of building
struct measure_t
{
String serialNum;
float alt;
};
struct measure_t measures[0][2];
void setup()
{
pinMode(cueToOutput, INPUT_PULLUP);
pinMode(altInput, INPUT);
pinMode(sonarInput, INPUT_PULLUP);
Serial.begin(57600);
Serial.println("Start");
}
void loop()
{
/* measure_t measures[] = {
{bike1, 10},
{bike2, 22},
{bike2, 22.5},
{bike3, 31},
{bike3, 33},
{bike2, 21},
{bike1, 11.2},
{bike1, 10.7},
};
*/
altIn = analogRead(altInput);
while (Serial.available())
{
char UART;
UART = Serial.read();
dataIn += UART;
//Process message only when new line character is received
if (UART == '\n')
{
// Serial.print(dataIn);
//struct measure_t
measures[rows][2];
rows ++;
measures[rows][1].serialNum = dataIn;
measures[rows][2].alt = altIn;
dataIn = ""; // Clear received buffer
}
}
if (digitalRead(sonarInput) == 0)
{
maxAlt = altIn;
}
if (digitalRead(cueToOutput) == 0)
{
const uint16_t nbMeasures = sizeof(measures) / sizeof(measures[0]);
float measureSum[maxItems] = {0L};
float measureCount[maxItems] = {0};
for (uint16_t i = 0; i < nbMeasures; i++)
{
measureSum[measures[i].serialNum] += measures[i].alt;
measureCount[measures[i].serialNum]++;
}
for (byte i = 0; i < maxItems; i++)
{
if (measureCount[i] != 0)
{
float avgAltPerItem = {measureSum[i] / measureCount[i]};
float estFloorPerItem = {(avgAltPerItem / maxAlt)*numOfFloor};
Serial.print(F("Average Alt for Item"));
Serial.print(i+1);
Serial.print(F(" = "));
Serial.println(avgAltPerItem);
Serial.print(F("Estimated floor for Item"));
Serial.print(i+1);
Serial.print(F(" is "));
Serial.println(estFloorPerItem);
Serial.println();
}
else
{
Serial.print(F("No measure exists for Item"));
Serial.println(i+1);
Serial.println();
}
}
Serial.print(F("Total height of building is "));
Serial.print(maxAlt);
Serial.println(F(" meters."));
while(1){}
}
}