Output is looping

#include <Arduino_LSM6DS3.h>
#include <LiquidCrystal.h>
#include <ArduinoBLE.h>



void setup() {
   Serial.begin(9600);  // initialize serial bus (Serial Monitor)
   while (!Serial);  // wait for serial initialization
   Serial.print("LSM6DS3 IMU initialization ");

   if (IMU.begin()) {  // initialize IMU
      Serial.println("completed successfully.");
   } else {
      Serial.println("FAILED.");
      IMU.end();
      while (1);
   }
   
   Serial.println();
}


void loop() {
   char buffer[8];    // string buffer for use with dtostrf() function
   float ax, ay, az;  // accelerometer values
   float gx, gy, gz;  // gyroscope values



   boolean fall = false; //stores if a fall has occurred
boolean trigger1=false; //stores if first trigger (lower threshold) has occurred
boolean trigger2=false; //stores if second trigger (upper threshold) has occurred
boolean trigger3=false; //stores if third trigger (orientation change) has occurred
byte trigger1count=0; //stores the counts past since trigger 1 was set true
byte trigger2count=0; //stores the counts past since trigger 2 was set true
byte trigger3count=0; //stores the counts past since trigger 3 was set true
int angleChange=0;

float Raw_Amp = pow(pow(ax,2)+pow(ay,2)+pow(az,2),0.5);
int  Amp = Raw_Amp * 10;  // Mulitiplied by 10 bcz values are between 0 to 1
 Serial.println(Amp);
 if (Amp<=2 && trigger2==false){ //if AM breaks lower threshold (0.4g)
   trigger1=true;
   Serial.println("TRIGGER 1 ACTIVATED");
   }
 if (trigger1==true){
   trigger1count++;
   Serial.print("Amp "); Serial.println(Amp); 
   if (Amp>=1){ //if AM breaks upper threshold (3g)
     trigger2=true;
     Serial.println("TRIGGER 2 ACTIVATED");
     trigger1=false; trigger1count=0;
     }
 }
 if (trigger2==true){
   trigger2count++;
   angleChange = pow(pow(gx,2)+pow(gy,2)+pow(gz,2),0.5); Serial.println(angleChange);
   if (angleChange>=30 && angleChange<=400){ //if orientation changes by between 80-100 degrees
     trigger3=true; trigger2=false; trigger2count=0;
     Serial.println(angleChange);
     fall=true; trigger3=false; trigger3count=0;
     Serial.println("TRIGGER 3 ACTIVATED");
       }
             else{ //user regained normal orientation
          trigger3=false; trigger3count=0;
          Serial.println("TRIGGER 3 DEACTIVATED");
       }
   }
 if (fall==true){ //in event of a fall detection
   Serial.println("FALL DETECTED");
   fall=false;
   return 1;
   }
 if (trigger2count>=6){ //allow 0.5s for orientation change
   trigger2=false; trigger2count=0;
   Serial.println("TRIGGER 2 DECACTIVATED");
   }
 if (trigger1count>=6){ //allow 0.5s for AM to break upper threshold
   trigger1=false; trigger1count=0;
   Serial.println("TRIGGER 1 DECACTIVATED");
   }

   // Retrieve and print IMU values
   if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable()
      && IMU.readAcceleration(ax, ay, az) && IMU.readGyroscope(gx, gy, gz)) {
      dtostrf(ax, 4, 1, buffer);
      dtostrf(ay, 4, 1, buffer);
      dtostrf(az, 4, 1, buffer);
      dtostrf(gx, 7, 1, buffer);
      dtostrf(gy, 7, 1, buffer);
      dtostrf(gz, 7, 1, buffer);
   }
   delay(100);  // wait one second between readings
}

so here is a code and when fall detection is true the output just looping you can see on a picture output, and instead of it I need to output message about fall and start monitoring again

Are those variables supposed to retain their values after loop() completes and starts all over? The way the are coded now, at the end of loop() the variables are destroyed and at the start of loop() those variables are recreated without retaining their previously held data.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.