Initial reading for Step Counter not started with zero, tried EEPROM, now the code is not working anymore

Hi, there. I would like to introduce myself as a beginner in programming language. At first I wanted to select language programming for the category. But since I did try EEPROM and looks like it has additional problem with memory, thus I choose this category.

Here is my code at the beginning:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>

Adafruit_MPU6050 mpu;

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

int16_t accelX, accelY, accelZ;

float vectorprevious;
float vector;
float totalvector;
float inbetween=54; //change it according to your distance between your legs in cm
float distance;

int Steps = 0;

void setup() {
  Wire.begin();
  Serial.begin(115200);

  mpu.begin();
  mpu.setAccelerometerRange(MPU6050_RANGE_2_G);
  mpu.setGyroRange(MPU6050_RANGE_250_DEG);
  mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);

  lcd.begin();
  lcd.backlight();
}

void loop() {
  getAccel();
  
  vector = sqrt((accelX * accelX) + (accelY * accelY) + (accelZ * accelZ));

  totalvector = vector - vectorprevious;

  if (totalvector > 0.9) {
    Steps++;
    distance = Steps*inbetween/100;
  }
  
  Serial.print("Totalvector: ");
  Serial.println(totalvector);
  
  Serial.print("Steps: ");
  Serial.println(Steps);

  Serial.print("Dist: ");
  Serial.println(distance);

  vectorprevious = vector;

  lcd.clear();// clearn previous values from screen
  lcd.setCursor(0,0);//Set the start of the character prints on 1st column and 1st line
  lcd.print("Steps:");//Print string
  lcd.print(Steps);//Print the declare made for number of steps detected
  lcd.setCursor(0,1);//Set the start of the character prints on 1st column and 1st line
  lcd.print("Dist :");
  lcd.print(distance);//Print calculated distance
  lcd.print(" cm ");

  delay(270);
}

void getAccel() {
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

  accelX = a.acceleration.x;
  accelY = a.acceleration.y;
  accelZ = a.acceleration.z;
}

The above code worked fine but, I notice in my LCD and serial monitor that the step counter showing initial reading at 1 after pressing reset button. So I used EEPROM to solve this problem and the coding is as follows:

#include <EEPROM.h>

//RECOMMENDED DELAY: 270/280
//TOTAL VECTOR THRESHOLD: >0.9
//WORKING COUNT STEPS AND CALC DISTANCE

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>

Adafruit_MPU6050 mpu;

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

int16_t accelX, accelY, accelZ;

float vectorprevious;
float vector;
float totalvector;
float inbetween=54; //change it according to your distance between your legs in cm
float distance;

byte Steps = 0; //Use byte data type

void setup() {
  Wire.begin();
  Serial.begin(115200);

  // Clear the EEPROM to ensure a fresh start for Steps
  for (int i = 0; i < sizeof(Steps); i++) {
    EEPROM.write(i, 0);
  }
  EEPROM.commit();

  mpu.begin();
  mpu.setAccelerometerRange(MPU6050_RANGE_2_G);
  mpu.setGyroRange(MPU6050_RANGE_250_DEG);
  mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);

  lcd.begin();
  lcd.backlight();
}

void loop() {

  getAccel();
  
  vector = sqrt((accelX * accelX) + (accelY * accelY) + (accelZ * accelZ));
  totalvector = vector - vectorprevious;

  if (totalvector > 0.9) {
    Steps++; // Add 1 step 
    distance = Steps * inbetween / 100;
    
    // Save the current step count in the EEPROM
    EEPROM.write(0, Steps);
    EEPROM.commit();
    }
    
  Serial.print("Totalvector: ");
  Serial.println(totalvector);
  
  Serial.print("Steps: ");
  Serial.println(Steps);

  Serial.print("Dist: ");
  Serial.println(distance);

  lcd.clear();// clearn previous values from screen
  lcd.setCursor(0,0);//Set the start of the character prints on 1st column and 1st line
  lcd.print("Steps:");//Print string
  lcd.print(Steps);//Print the declare made for number of steps detected
  lcd.setCursor(0,1);//Set the start of the character prints on 1st column and 1st line
  lcd.print("Dist :");
  lcd.print(distance);//Print calculated distance
  lcd.print(" cm ");

  vectorprevious = vector;
  
  delay(280);
}

void getAccel() {
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

  accelX = a.acceleration.x;
  accelY = a.acceleration.y;
  accelZ = a.acceleration.z;
}

The code makes the step counter only shows '0' for total vector, steps and distance.

So, I gave up with EEPROM and get back to previous codes. But the step counter that previously worked shows '0' reading now.

Any suggestion, on how to solve the problems that I have? Thank you in advance!!

EEPROM.write() writes a single byte (at least on AVR); you're trying to store an int which is at minimum 2 bytes (maybe 4 bytes on an ESP).

I'm not familiar with ESP boards; the commit() indicates that you're using an ESP. But try using EEPROM.put() and EEPROM.get().

First Code.
I never used this module MPU6050>
I don't know what values of X, Y and Z they provide when "resetting" the arduino.
But if the value of this algorithm is greater than 0.9, (which I think is very likely to happen every time).
Your first code will always print the initial value of steps as 1.
Why?
If " if (totalvector > 0.9) {" is satisfied then steps will be incremented to 1.
And you only print step after this increment.

Second code:
You only write to EEPROM, (and one byte only), not read from EEPROM.
What is the purpose of recording only?

Hye there!

Yes, you're correct. I'm using ESP8266.

I already tried the EEPROM.get() with my limited knowledge in programming, but I cannot produce a good result.

I will just modify my code without EEPROM I think, but I'm still figuring out why.

Thank you for your explanation on byte. Thank you.

I see, so I must add additional command for it to minus the step count with 1.

Your explanation helps me understand the concept, and will come back to share the outcomes.

Thank you.

There is an additional step required for ESP8266; you have to initialise the EEPROM object. See e.g. Using the EEPROM with the ESP8266 • AranaCorp .

Thank you so much! I am more clear with that now, but still slowly learning it.

Check this link out, the part does not have EEPROM but it is simulated. ESP_EEPROM is data persistent when flashing new rev or new program? - #2 by gilshultz

I don't think this is right.
Better figure out why you add one to step? What is the purpose of this construction in the code?

Or did you just copy it off somewhere?

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