Hi there,
Is it possible to create an array that does not have a pre-defined size and is empty at the beginning?
for instance:
float forces[] = {} //no pre-existing entries in array
Where I can add sensor values from a strain gauge every 2 seconds into the array for varying period. In one session the array may gather 10 entries and on another occasion 16.
Is this possible using this tool? If not how else can I store values for later manipulation?
This is my first post so be kind haha
Duncan
There is no point in doing that using a microprocessor with such limited memory as most Arduinos.
Simply define an array that will be big enough to handle some reasonable number of points, and keep track of how many you have stored.
Also use an appropriate variable type. You don't need a four byte float to store most sensor readings -- a two byte integer is usually large enough.
thank you for your help! Ill just specify a large enough size for my purpose, and keep track of how many entries I have using a counter!
Kind regards,
Duncan
You must take great care to never write past the end of an array (write more elements that spaces in the array). Writing past rhe end of an array can cause hard to find bugs and even mysterious processor resets.
right okay! could I write code for instance:
if i = arraysize
then empty array
to stop the array from over flowing?
I know that isnt proper C++ but could I use that logic?
@OP
You can declare an array using a pointer variable such as:
float *myArray; //there is no explicit dimension as to how many float type data items it will hold
myArray[0] = 3.75;
//-------------------------------
myArray[9] = 1.25;
Serial.println(myArray[9], 2); //shows: 1.25
@golammostafa
In that example you still need to define a specific array size at the start though?
But yeah I know how to recall specific data inside an array!
thank for the reply!
In that example you still need to define a specific array size at the start though?
Yes, golammostafa forgot the most basic rule of this forum: post ALL the code.
The snippet is not only completely useless, it trashes memory.