How to store incoming data in a single std array C++

I am trying to understand how one could take two measurements say temperature and pressure and store the data as it comes in, say up to 100 measurements. My C++ is pretty weak, but I am hoping to use this array to find the min, max, average and maybe other stats.

DataArray [100] [2]

Does anyone know how to accomplish this?

if you need two different values use a structure

struct Weather{
   int temp;
  int pressure;
};

Weather weather[100];

add a writing index and get the size of your array / number of elements

size_t index = 0;
const size_t noOfWeather = sizeof(weather)/sizeof(weather[0]);

after each write increase the index

weather[index].temp = analogRead(A0);
weather[index].pressure= analogRead(A1);
index++;
if (index >= noOfWeather) index = 0;  // decide what to do when you reach the end...
1 Like

Simple two array declarations:

unsigned int myTemp[100] = {0};
unsigned int myPress[100] = {0};

//acquire/store signal at 200 ms interval

for(int i = 0; i<100; i++)
{
      myTemp[i] += analogRead(A0);  //analog type Temp sensor is connected with A0-pin
      delay(100);    //delay to avoid cross talk with Pressure channel
      myPress[i] += analogRead(A1); //ananlog type Pressure sensot is connected with A1-pin
      delay(100);
}

//do processing/display as needed

1 Like
Struct Weather{
   int temp;
  int pressure;
};

Weather weather[100];

Does not get compiled!

what does "incoming data" mean?

Do you have some kind of a test-code that shows the principle how one temperature and one pressure "comes in"

Does it "come in" through a function-call

temp = mySensorObject.getTemperature();

or does it "come in" from the serial interface?

or somehow different?

you are working on an informatic project and what is most needed in an informatic project is sufficient information

Be the change you want to see in the world
best regards Stefan

The 200ms interval is a good assumption for my project. The data is coming in through analog inputs, but being converted to float data types. I don't want to get too much into this as it is off topic. So let's just assume the variables temperature and pressure are changing values every 200ms.

The code you posted seems to be 2 distinct arrays, but I am hoping to have these two variables in a single array. I think even better would be a std::array right because it has the ability to work with predefined functions for processing data...

void fProcessAirPressure ( void *pvParemeters )
{
  int   cellCount = 58;
  int   Ticks     = 118;
  bool  Filled    = false;
  float *ptr      = CollectionPressure;
  const int ticksTrigger = 120; // triggered at 1 minute intervals
  for (;;)
  {
    //triggered by BME which is triggered by the 1 minute hardware timer.
    xEventGroupWaitBits (eg, evtStoreAirPressure, pdTRUE, pdTRUE, portMAX_DELAY );
    xSemaphoreTake( sema_CollectPressure, portMAX_DELAY );
    xSemaphoreTake ( sema_eData, portMAX_DELAY );
    if ( !Filled )
    {
      for ( int j = 0; j < BufferCount; j++ )
      {
        *( ptr + j ) = x_eData.oPressure;
      }
      Filled = true;
    } else {
      if ( Ticks == ticksTrigger )
      {
        //shift contents left and insert new value at the end
        for ( int i = 0; i <= BufferCount - 2; i++ )
        {
          *( ptr + i ) = *( ptr + (i+1) );
        }
      }
      *( ptr + (BufferCount - 1) ) = x_eData.oPressure;
    }
    if ( x_eData.oPressure > x_eData.PressureH )
    {
      x_eData.PressureH = x_eData.oPressure;
    }
    if ( x_eData.oPressure < x_eData.PressureL )
    {
      x_eData.PressureL = x_eData.oPressure;
    }
    Ticks++;
    if ( Ticks >= (ticksTrigger + 1) )
    {
      Ticks = 1;
      cellCount++;
    }
    if ( cellCount == (BufferCount - 1) )
    {
      cellCount = 0;
    }
    x_eData.cngPress = CalculatePressureFactors(  *( ptr + 57), *( ptr + 59) ); // going back 4 hours
    xSemaphoreGive( sema_eData );
    xSemaphoreGive( sema_CollectPressure );
    //
    //log_i( " high watermark % d",  uxTaskGetStackHighWaterMark( NULL ) );
  } //for (;;)
  vTaskDelete( NULL );

} //void fStoreAirPressure ( void *pvParemeters )

The above code may be useful or not.

There is an array called CollectPressure with 60 elements. *ptr is a pointer to the array.

The part that might interest the OP is the code to store the highest and lowest values in the 2 variables called PressureL and PressureH, ``` float PressureH = 0.0f; float PressureL = 10000.0f; ''' that hold the highest values and lowest values. I'm keeping the highest and lowest recorded no matter how many array shifts happen but the OP can reset High Low at every reset of cellCount.

When the first data point comes in all 60 elements are filled with the same data. After the initial fill new data enters the array from the end [59] and old data is pushed out of [0].

1 Like

Yea this is very interesting I would like to replicate something like this. It looks like your array is x_eData

x_eData is a structure used by the various tasks to share data. The array is defined

const int BufferCount = 60;
float CollectionPressure[BufferCount] = {0.0f};

I used a pointer to access the array because I felt it was time to refresh my knowledge on the use of pointers. The pointers can be removed and replaced with CollectionPressure[X].

I understand the freeRTOS stuff may complicate the code; just remove it, of course.

Any example?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.