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!!