user57437: I am reading an accelerometer using arduino. Basically i receive positive values and I need to get only first row of negative values.
E.g. I receive values > -0.5 , then I start to have values < -0.5 (which i put them into an array , and then in case I get again values greater than -0.5 I want to stop it.
Also if in 30 seconds it should stop anyway.
This is what I did till now but is not working like expected
void reading_mpu_brake(){
          unsigned long StartTime = millis();
     
      float acc[500];
      int read_time[500];
    
      byte arrayIndex = 0;
    
    while(1){
      unsigned long CurrentTime = millis();
      if (CurrentTime - StartTime < interval) {
        mpu.update();
        if (mpu.getAccX() < -0.05){
          total_acc = sqrt((pow((mpu.getAccX() - offsetx), 2) + pow((mpu.getAccY() - offsety), 2) + pow((mpu.getAccZ() - offsetz), 2) ));
          
          acc[arrayIndex] = total_acc; 
          read_time[arrayIndex] = millis();
          
          Serial.print(acc[arrayIndex]);
          Serial.print(" | ");
          Serial.print(mpu.getAccX());
          Serial.print(" | ");
          Serial.println(read_time[arrayIndex]);
          Serial.println(arrayIndex);
           arrayIndex++;
        }
      }  
      else if (CurrentTime - StartTime > interval)
      {
        Serial.println("Over time");
        break;
      
      
      }
    
    }

Now is working and get values less than -0.5 but if after several good values if I get positive values and then again negative it will put in array also the last one which I don t.
So basically I need to stop it after first bunch of good values...
Any help appreciated
Thank you!
L.E.
Seems I miss a good explanation so I will try again.
1 - I call the function and it start monitoring values for 30 seconds.
Case 1 :
I receive 0.6 , 0.7 , 0.4 , 0.2 , -0.2 , -0.6 , -0.7 , -0.8 , -0.4 , 0.3 , 0.5 ...etc
In this case I get into acc array values : -0.6 , -0.7 , -0.8
then after 30 sec gone the function exit
That is OK partially .
Case 2 :
I receive 0.6 , 0.7 , 0.4 , 0.2 , -0.2 , -0.6 , -0.7 , -0.8 , -0.4 , 0.3 , 0.5 , -0.6 , -0.7 , 0.6...etc
In this case I get into array values -0.6 , -0.7 , -0.8  and if the 30 secs are not gone I also get -0.6 , -0.7 .
What I want is function to exit after get first bunch of good values (-0.6 , -0.7 , -0.8) .
Hope now is more clear

user10794: float acc[500];
int read_time[500];

Right, so a float is 4 bytes on this platform, so that array takes 2000 bytes. And an int is 2 bytes, so that other array is another 1000 bytes.
The Uno has 2KB of RAM, so those two declarations alone take up more RAM than the device has.
I'm a little surprised it works at all, but certainly "not working as expected" is indeed an expected outcome here.

57437: <p>I am reading an accelerometer using arduino. Basically i receive positive values and I need to get only first row of negative values.
E.g. I receive values &gt; -0.5 , then I start to have values &lt; -0.5 (which i put them into an array , and then in case I get again values greater than -0.5 I want to stop it.</p>
<p>Also if in 30 seconds it should stop anyway.
This is what I did till now but is not working like expected</p>
<pre><code>void reading_mpu_brake(){
          unsigned long StartTime = millis();
     
      float acc[500];
      int read_time[500];
    
      byte arrayIndex = 0;
    
    while(1){
      unsigned long CurrentTime = millis();
      if (CurrentTime - StartTime &lt; interval) {
        mpu.update();
        if (mpu.getAccX() &lt; -0.05){
          total_acc = sqrt((pow((mpu.getAccX() - offsetx), 2) + pow((mpu.getAccY() - offsety), 2) + pow((mpu.getAccZ() - offsetz), 2) ));
          
          acc[arrayIndex] = total_acc; 
          read_time[arrayIndex] = millis();
          
          Serial.print(acc[arrayIndex]);
          Serial.print(&quot; | &quot;);
          Serial.print(mpu.getAccX());
          Serial.print(&quot; | &quot;);
          Serial.println(read_time[arrayIndex]);
          Serial.println(arrayIndex);
           arrayIndex++;
        }
      }  
      else if (CurrentTime - StartTime &gt; interval)
      {
        Serial.println(&quot;Over time&quot;);
        break;
      
      
      }
    
    }
</code></pre>
<p>Now is working and get values less than -0.5 but if after several good values if I get positive values and then again negative it will put in array also the last one which I don t.</p>
<p>So basically I need to stop it after first bunch of good values...</p>
<p>Any help appreciated</p>
<p>Thank you!</p>
<p>L.E.
Seems I miss a good explanation so I will try again.</p>
<p>1 - I call the function and it start monitoring values for 30 seconds.</p>
<p>Case 1 :
I receive 0.6 , 0.7 , 0.4 , 0.2 , -0.2 , -0.6 , -0.7 , -0.8 , -0.4 , 0.3 , 0.5 ...etc</p>
<p>In this case I get into acc array values : -0.6 , -0.7 , -0.8
then after 30 sec gone the function exit</p>
<p>That is OK partially .</p>
<p>Case 2 :
I receive 0.6 , 0.7 , 0.4 , 0.2 , -0.2 , -0.6 , -0.7 , -0.8 , -0.4 , 0.3 , 0.5 , -0.6 , -0.7 , 0.6...etc</p>
<p>In this case I get into array values -0.6 , -0.7 , -0.8  and if the 30 secs are not gone I also get -0.6 , -0.7 .</p>
<p>What I want is function to exit after get first bunch of good values (-0.6 , -0.7 , -0.8) .</p>
<p>Hope now is more clear</p>
 10794: <pre><code>float acc[500];
int read_time[500];
</code></pre>
<p>Right, so a float is 4 bytes on this platform, so that array takes 2000 bytes. And an int is 2 bytes, so that other array is another 1000 bytes.</p>
<p>The Uno has 2KB of RAM, so those two declarations alone take up more RAM than the device has.</p>
<p>I'm a little surprised it works at all, but certainly &quot;not working as expected&quot; is indeed an expected outcome here.</p>
