Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <EEPROM.h>
- #include <WiFi.h>
- #include <RemoteXY.h>
- ////////////////////////////////////////////
- #define REMOTEXY_MODE__WIFI_POINT
- #define BUTTON_PIN 10
- #define REMOTEXY_WIFI_SSID "RemoteXY"
- #define REMOTEXY_WIFI_PASSWORD "12345678"
- #define REMOTEXY_SERVER_PORT 6377
- // RemoteXY GUI configuration
- #pragma pack(push, 1)
- uint8_t RemoteXY_CONF[] = // 48 bytes
- { 255,0,0,1,0,41,0,19,0,0,0,0,31,1,106,200,1,1,2,0,
- 67,30,92,48,36,93,2,26,129,19,63,70,12,192,2,83,116,101,112,32,
- 67,111,117,110,116,101,114,0 };
- // this structure defines all the variables and events of your control interface
- struct {
- // output variables
- int16_t counter_01; // -128 .. 127
- // other variable
- uint8_t connect_flag; // =1 if wire connected, else =0
- } RemoteXY;
- #pragma pack(pop)
- //////////////////////////////////////////
- const int MPU_ADDR = 0x68; // I2C address of MPU-6050
- int16_t accelX, accelY, accelZ;
- int stepCount = 0;
- float accMagnitudePrev = 0;
- void resetEEPROM() {
- for (int i = 0 ; i < EEPROM.length() ; i++) {
- EEPROM.write(i, 0);
- }
- EEPROM.commit();
- stepCount = 0;
- Serial.println("EEPROM reset");
- }
- void setup() {
- RemoteXY_Init ();
- Wire.begin();
- Serial.begin(9600);
- Wire.beginTransmission(MPU_ADDR);
- Wire.write(0x6B); // PWR_MGMT_1 register
- Wire.write(0); // set to zero (wakes up the MPU-6050)
- Wire.endTransmission(true);
- delay(2000);
- pinMode(BUTTON_PIN, INPUT_PULLUP);
- EEPROM.begin(sizeof(int));
- EEPROM.get(0, stepCount);
- }
- void loop() {
- RemoteXY_Handler ();
- if (digitalRead(BUTTON_PIN) == LOW) {
- resetEEPROM();
- delay(1000); // Debouncing the button press
- }
- readAccelerometerData();
- detectStep();
- displayStepCount();
- RemoteXY.stepCount
- delay(100);
- }
- void saveStepCount() {
- // Save stepCount to EEPROM
- EEPROM.put(0, stepCount);
- EEPROM.commit();
- }
- void readAccelerometerData() {
- Wire.beginTransmission(MPU_ADDR);
- Wire.write(0x3B); // Starting register for accelerometer data
- Wire.endTransmission(false);
- Wire.requestFrom(MPU_ADDR, 6, true);
- accelX = Wire.read() << 8 | Wire.read();
- accelY = Wire.read() << 8 | Wire.read();
- accelZ = Wire.read() << 8 | Wire.read();
- }
- void detectStep() {
- float accX = accelX / 16384.0;
- float accY = accelY / 16384.0;
- float accZ = accelZ / 16384.0;
- // Calculate the magnitude of acceleration
- // accX * accX is equivalent to pow(accX, 2)
- float accMagnitude = sqrt(accX * accX + accY * accY + accZ * accZ);
- // float accMagnitude = sqrt(accX * accX + accY * accY + accZ * accZ);: This line calculates the magnitude of the acceleration vector using the calculated acceleration values for the X, Y, and Z axes. The sqrt() function is used to calculate the square root of the sum of the squared acceleration values.
- // Peak detection
- if (accMagnitudePrev > accMagnitude + 0.1 && accMagnitudePrev > 1.5) {
- stepCount++;
- saveStepCount();
- }
- accMagnitudePrev = accMagnitude;
- }
- void displayStepCount() {
- Serial.print("Steps: ");
- Serial.println(stepCount);
- dtostrf(stepCount, 0, 1, RemoteXY.counter_01);
- }
Advertisement
Add Comment
Please, Sign In to add comment