0

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

2
  • I edited and added more explanations. By "row of negative values" mean first bunch of values that met criteria to be < -0.5 Commented Sep 8, 2021 at 8:10
  • it is clear what you are trying to do ... it is unknown what happens when you run the code ... not working as expected does not mean anything specific Commented Sep 8, 2021 at 15:21

1 Answer 1

5
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.