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
not working as expecteddoes not mean anything specific