Make array or string and update it with results from Loop

Hi!

I’ve searched on the internet before asking it in this forum but couldn’t find an answer or make the code on my own.

I’m currently working on a project that involves LoRa to get the results of a sensor in the cloud.
With my current program I’m able to send one result of the sensor at a time to the cloud.
Now I would like to make a packet of 13 results and send this packet by LoRa to the cloud instead of one result at a time.

I was thinking about making an array or a string and always add the value of the sensor to this string or array. And when the array or string contains 13 values I would send it as one message. Once the message is send the array or string has to be reset. I think I would also have to add an index to number the values.

I’m still not very familiar with programming and using Arduino.

For not making it more complicate I make a simple sketch of what I mean.

float FlowMeasurement;
float i; //this would be for the index


void DataPacket();
{
//In this section would come the array or string with the 13 different values from the sensor
Send.Datapacket; //This would send the data packet that is made 
Delay(5000)
}

void loop();
{

for (i=0; i<= 12; i++){

FlowMeasurement = SensorValue; //the sensor works with I²C 
}

I hope it’s clear what I mean!

Any help appreciated. Thanks.

Start simple

Declare a global array of 13 bytes

Write code where in the loop

  • it reads a digital pin value (high or low) and store it in the next available slot
  • When array is full (reaching 13 entries that is index 12) print out the 13 values, empty the array and start again.

what kind of data are you collecting with your sensor, its range of values?

what is unpacking the data at the other end? what if any delimiters would you want to send?

Thanks for answering both of you.

Now I already thought about making an array.

It's about a differential pressure sensor but I'm converting the data from the sensor into a flow measurement so the range is from 0.0 to 5.0. And the type is float.

It has to send the data to the All Things Talk platform, but in the feature it could be another platform as well.

BulldogLowell: what is unpacking the data at the other end? what if any delimiters would you want to send?

how do you wish to pack this up? what should it look like?

e.g.

0.9,4.7,3.3,... and so on

with commas separating them?

It's about a differential pressure sensor but I'm converting the data from the sensor into a flow measurement so the range is from 0.0 to 5.0. And the type is float.

Converting the int you get from analogRead() or digitalRead() to a float simply doubles the storage space needed.

Store the ints. Convert to float ONLY when you do the sending. Use half as much space.

BulldogLowell: how do you wish to pack this up? what should it look like?

e.g.

0.9,4.7,3.3,... and so on

with commas separating them?

Yes, this is what I thought it could look like: 0.9,4.7,3.3,... and so on.

Thanks for the tip Paul S, I didn't take notice of that.

MehdiPl:
Yes, this is what I thought it could look like: 0.9,4.7,3.3,… and so on.
Thanks for the tip Paul S, I didn’t take notice of that.

Something like this using an array of 13 floats called sensorValue

char txBuffer[sizeof(sensorValue)/sizeof(sensorValue[0]) * 4 + 1] = ""; // . create a buffer for the array values as x.x, plus one for null terminator
for(int i = 0; i < sizeof(sensorValue)/sizeof(sensorValue[0]); i++)
{
  char nextVal[5]  = "";
  sprintf(nextVal, "%d.%d,", int(sensorValue[i]), int(sensorValue[i]*10)%10);
  strcat(txBuffer, nextVal);
}
doSomethingWithTxBuffer();
  snprintf(nextVal, "%d.%d,", int(sensorValue[i]), int(sensorValue[i]*10)%10);

You are missing an argument - the n part of snprintf().

PaulS:   snprintf(nextVal, "%d.%d,", int(sensorValue[i]), int(sensorValue[i]*10)%10);

You are missing an argument - the n part of snprintf().

no, I fixed it before you commented

Before posting this I figured out what the different things as sprintf and strcat mean.
Could it work like this:

float sensorValue[13];        //the array to store the sensor results
float FlowMeasurement;     // this will represent the result from the sensor

void loop()
{
for (int j = 0; j<=12; j++)   //this is to fill the array with the sensor results
  {
    sensorValue[j]=FlowMeasurement;
  }

 // underneath is the part from BulldogLowell

char txBuffer[sizeof(sensorValue)/sizeof(sensorValue[0]) * 4 + 1] = ""; // . create a buffer for the array values as x.x, plus one for null terminator


        for(int i = 0; i < sizeof(sensorValue)/sizeof(sensorValue[0]); i++)
      {
            char nextVal[5]  = "";
            sprintf(nextVal, "%d.%d,", int(sensorValue[i]), int(sensorValue[i]*10)%10);
            strcat(txBuffer, nextVal);
      }
}

I was thinking to maybe just Serial.print() the result so it would be possible to check immediately in the Serial Monitor.

try it and see

Or you could use dtostrf