I want to define an unknown size for an array. However, I know that this cannot be done on the arduino. So what other methods do you suggest? I tried using vectors by downloading the library ArduinoSTL, but it doesnt work. (It only prints Serial.println(Signal); once, referring to the code in the pastebin link below)
Here's my code with vectors:
#include <ArduinoSTL.h>
#define sensor1 A0
using namespace std;
int Signal;
int Threshold = 550;
vector<int> startTime;
vector<int> endTime;
unsigned long measureTime;
void setup() {
Serial.begin(9600);
}
void loop()
{
//Serial.println(analogRead(sensor1));
int i = 0;
measureTime = millis();
while ((millis() - measureTime) < 10000)
{
Signal = analogRead(sensor1);
Serial.println(Signal);
if (Signal < Threshold)
{
startTime.at(i) = millis();
Serial.println("1");
}
if (Signal > Threshold)
{
endTime.at(i) = millis();
Serial.println("2");
}
i++;
}
}
-
unknown size? the size of the MCU RAM is known and there are no concurrent programs, so declare a maximum possible array and you are good to goJuraj– Juraj ♦2018年04月03日 06:58:56 +00:00Commented Apr 3, 2018 at 6:58
1 Answer 1
The vector
is empty after its default constructor. And nothing was ever added into it in your code. So you can't use anything like idexing by vect[index]
or method vect.at(index)
. And as there are no exceptions available, who knows what it does after the out of range call.
You should consider ring buffer instead, using dynamic memory is tricky on such constrained platforms. Even using Arduino's String class is often source of memory fragmentation and out of memory type errors.