user43368: 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++;
  }
}


user23841: 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.

43368: <p>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)</p>
<p>Here's my code with vectors:</p>
<pre class="lang-cpp prettyprint-override"><code>#include &lt;ArduinoSTL.h&gt; 
#define sensor1 A0

using namespace std;

int Signal;
int Threshold = 550;

vector&lt;int&gt; startTime;
vector&lt;int&gt; endTime;
unsigned long measureTime;


void setup() {
  Serial.begin(9600);
}

void loop()
{
  //Serial.println(analogRead(sensor1));
  int i = 0;
  measureTime = millis();

  while ((millis() - measureTime) &lt; 10000)
  {
    Signal = analogRead(sensor1);
    Serial.println(Signal);
    if (Signal &lt; Threshold)
    {
      startTime.at(i) = millis();
      Serial.println(&quot;1&quot;);
    }
    if (Signal &gt; Threshold)
    {
      endTime.at(i) = millis();
      Serial.println(&quot;2&quot;);
    }
    i++;
  }
}
</code></pre>
 23841: <p>The <code>vector</code> 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 <code>vect[index]</code> or method <code>vect.at(index)</code>. And as there are no exceptions available, who knows what it does after the out of range call.</p>

<p>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.</p>
