-1

I am using a Wemos D1 Mini and an mpu 6050 accelerametor. I managed to get the acceleratmetor to work and have 1 value come out of movements. So when a peak it hit (aka, the threshold of 1.00 is hit) n gets incremetented with 1. So it work a bit like a pedometer. This value is pushed to my firebase DB.

However mY problem is that after pushing it once, so the value of n is 1. The program blocks for a second and either slows down bad or the accelerametor value drops to 0. this only happens when i my piece of firebase code in. Im not sure if this causes any delays..

here is my part of my code:

#include <Wire.h>

#include "ESP8266WiFi.h" 
#include "FirebaseArduino.h" 

#define FIREBASE_HOST "xxxxxxxxxx.firebaseio.com" //Without http:// or https:// schemes
#define FIREBASE_AUTH "XXXXXXXXXXXXXXXXXXXXX"
#define WIFI_SSID "Bankai"
#define WIFI_PASSWORD "xxxxxxxxxxxx"

const float HI_THRESHOLD = 1.00;
const float LO_THRESHOLD = 0.70;
bool count_flag = false;

int n= 0; //will be used to store the count
int oldValue = n;
String myString;

const uint8_t MPU_addr = 0x68; // I2C address of the MPU-6050

const float MPU_GYRO_250_SCALE = 131.0;
const float MPU_GYRO_500_SCALE = 65.5;
const float MPU_GYRO_1000_SCALE = 32.8;
const float MPU_GYRO_2000_SCALE = 16.4;
const float MPU_ACCL_2_SCALE = 16384.0;
const float MPU_ACCL_4_SCALE = 8192.0;
const float MPU_ACCL_8_SCALE = 4096.0;
const float MPU_ACCL_16_SCALE = 2048.0;



struct rawdata {
  int16_t AcX; //Acceleration
  int16_t AcY; //Acceleration
  int16_t AcZ; //Acceleration
  int16_t Tmp;
  int16_t GyX; //Gyroscope
  int16_t GyY; //Gyroscope
  int16_t GyZ; //Gyroscope
};

// Using int variables to convert later on to degrees and decimals
struct scaleddata {
  float AcX; //Acceleration
  float AcY; //Acceleration
  float AcZ; //Acceleration
  float Tmp;
  float GyX; //Gyroscope
  float GyY; //Gyroscope
  float GyZ; //Gyroscope
};

bool checkI2c(byte addr);
void mpu6050Begin(byte addr);
rawdata mpu6050Read(byte addr, bool Debug);
void setMPU6050scales(byte addr, uint8_t Gyro, uint8_t Accl);
void getMPU6050scales(byte addr, uint8_t &Gyro, uint8_t &Accl);
scaleddata convertRawToScaled(byte addr, rawdata data_in, bool Debug);

//start connection, serial, and connection to db
void setup() {
  Wire.begin();
  Serial.begin(115200);

  mpu6050Begin(MPU_addr);

    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(100);
  }
  Serial.println();
  Serial.print("Connected with IP: ");
  Serial.println(WiFi.localIP());
  Serial.println();
    Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);

}

//if the value now is different from the current value it gets pushed to the DB, otherwise a flood of numbers apperars.
void loop() {
  rawdata next_sample;
  setMPU6050scales(MPU_addr, 0b00000000, 0b00010000);
  next_sample = mpu6050Read(MPU_addr, false);
  convertRawToScaled(MPU_addr, next_sample, false);

I feel likeit is thei part which creates the bottleneck.

  //myString = String(n);
  if(oldValue != n){
   Firebase.setInt("Reps/Value", n);
  }
  delay(100); // Wait 5 seconds and scan again


}

 float filterConstant = 0.05;
  values.AcX = (float) data_in.AcX / scale_value;
  values.AcY = (float) data_in.AcY / scale_value;
  values.AcZ = (float) data_in.AcZ / scale_value;
  filteredAx = filteredAx * (1.0 - filterConstant) + values.AcX * filterConstant;
  filteredAy = filteredAy * (1.0 - filterConstant) + values.AcY * filterConstant;
  filteredAz = filteredAz * (1.0 - filterConstant) + values.AcZ * filterConstant;
  float result = sqrt((filteredAx * filteredAx) + (filteredAy * filteredAy ) + (filteredAz * filteredAx ));

//  Serial.print(values.AcX); Serial.print("\t");
//  Serial.print(filteredAx); Serial.print("\t");


  Serial.println(result);


  //    Serial.print(values.AcY); Serial.print("\t");
  //    Serial.print(values.AcZ);
//  Serial.println();

 if( result > HI_THRESHOLD && !count_flag) //if analog_in > Hi_THRESHOLD and count_flag == false
     {
           n++;
           count_flag = true;
     }

     if(result <= HI_THRESHOLD) //set count_flag to false only when below the threshold
           count_flag = false;

  Serial.println(n);



  return values;
}

Can you help me find the problem? Thank you!

1

Logging the data is indeed what slows your program down. You can't avoid that. There are several ways to deal with it depending on what you need.

Instead of logging data all the time, you could try to accumulate a bunch of data and then write it. This approach of buffering data will still fail to read while you log, but you will be able to get bursts of good data in between.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.