Hi community, I am running into some difficulties here.
This code is adapted from one shared by J-M-L,
uint8_t numOfFloor = {12}; // Input number of floors
enum dataType {bike1 , bike2, bike3, bike4, maxItems}; // Declare item1, item2, item3, etc.
float maxAlt = 36;
struct measure_t {
dataType type;
float value;
};
struct measure_t measures[] = {
{bike1, 10.0},
{bike2, 22.0},
{bike2, 22.5},
{bike3, 31.0},
{bike3, 33.0},
{bike2, 21.0},
{bike1, 11.2},
{bike1, 10.7},
};
const uint16_t nbMeasures = sizeof(measures) / sizeof(measures[0]);
float measureSum[maxItems] = {0L};
float measureCount[maxItems] = {0};
void setup() {
Serial.begin(57600);
for (uint16_t i = 0; i < nbMeasures; i++) {
measureSum[measures[i].type] += measures[i].value;
measureCount[measures[i].type]++;
}
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."));
}
void loop() {}
And I have tried to change it to become a array for my separate microcontroller to write into, which looks like this:
uint8_t numOfFloor = {12}; // Input number of floors
// Declare item1, item2, item3, etc. (Do Not Remove "maxItems")
enum dataType : char {bike1 , bike2, bike3, bike4, maxItems};
// Please fill in information for above
// Code under this line is automated
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 = 0; // Variables for rows/columns of data array
float maxAlt = 36; // maxAlt stores height of building
struct measure_t
{
dataType type;
int value;
};
measure_t measures[10][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);
rows ++;
measures[rows][1] = dataIn;
measures[rows][2] = 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][1].value] += measures[i].value;
measureCount[measures[i][1].value]++;
}
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 have another portion (not added here) which is able to get altitude data to input into this array.
However, I am not able to get any values like the first code.
The response I received is:
Average Alt for Item1 = 0.00
Estimated floor for Item1 is 0.00
No measure exists for Item2
No measure exists for Item3
Total height of building is 36 meters.
I think my array should be the problem, but I searched a lot and am really lost already.. Please help, thank you!


