safwan092

Untitled

Jan 29th, 2025
55
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 1 0
  1. #include <Wire.h>
  2. #include <EEPROM.h>
  3. #include <WiFi.h>
  4. #include <RemoteXY.h>
  5. ////////////////////////////////////////////
  6. #define REMOTEXY_MODE__WIFI_POINT
  7. #define BUTTON_PIN 10
  8. #define REMOTEXY_WIFI_SSID "RemoteXY"
  9. #define REMOTEXY_WIFI_PASSWORD "12345678"
  10. #define REMOTEXY_SERVER_PORT 6377
  11. // RemoteXY GUI configuration
  12. #pragma pack(push, 1)
  13. uint8_t RemoteXY_CONF[] = // 48 bytes
  14. { 255,0,0,1,0,41,0,19,0,0,0,0,31,1,106,200,1,1,2,0,
  15. 67,30,92,48,36,93,2,26,129,19,63,70,12,192,2,83,116,101,112,32,
  16. 67,111,117,110,116,101,114,0 };
  17.  
  18. // this structure defines all the variables and events of your control interface
  19. struct {
  20.  
  21. // output variables
  22. int16_t counter_01; // -128 .. 127
  23.  
  24. // other variable
  25. uint8_t connect_flag; // =1 if wire connected, else =0
  26.  
  27. } RemoteXY;
  28. #pragma pack(pop)
  29. //////////////////////////////////////////
  30.  
  31.  
  32. const int MPU_ADDR = 0x68; // I2C address of MPU-6050
  33. int16_t accelX, accelY, accelZ;
  34. int stepCount = 0;
  35. float accMagnitudePrev = 0;
  36.  
  37. void resetEEPROM() {
  38. for (int i = 0 ; i < EEPROM.length() ; i++) {
  39. EEPROM.write(i, 0);
  40. }
  41. EEPROM.commit();
  42. stepCount = 0;
  43. Serial.println("EEPROM reset");
  44. }
  45.  
  46. void setup() {
  47. RemoteXY_Init ();
  48. Wire.begin();
  49. Serial.begin(9600);
  50. Wire.beginTransmission(MPU_ADDR);
  51. Wire.write(0x6B); // PWR_MGMT_1 register
  52. Wire.write(0); // set to zero (wakes up the MPU-6050)
  53. Wire.endTransmission(true);
  54. delay(2000);
  55. pinMode(BUTTON_PIN, INPUT_PULLUP);
  56. EEPROM.begin(sizeof(int));
  57. EEPROM.get(0, stepCount);
  58.  
  59. }
  60. void loop() {
  61. RemoteXY_Handler ();
  62.  
  63. if (digitalRead(BUTTON_PIN) == LOW) {
  64. resetEEPROM();
  65. delay(1000); // Debouncing the button press
  66. }
  67. readAccelerometerData();
  68. detectStep();
  69. displayStepCount();
  70. RemoteXY.stepCount
  71. delay(100);
  72. }
  73. void saveStepCount() {
  74. // Save stepCount to EEPROM
  75. EEPROM.put(0, stepCount);
  76. EEPROM.commit();
  77. }
  78.  
  79. void readAccelerometerData() {
  80. Wire.beginTransmission(MPU_ADDR);
  81. Wire.write(0x3B); // Starting register for accelerometer data
  82. Wire.endTransmission(false);
  83. Wire.requestFrom(MPU_ADDR, 6, true);
  84.  
  85. accelX = Wire.read() << 8 | Wire.read();
  86. accelY = Wire.read() << 8 | Wire.read();
  87. accelZ = Wire.read() << 8 | Wire.read();
  88. }
  89. void detectStep() {
  90. float accX = accelX / 16384.0;
  91. float accY = accelY / 16384.0;
  92. float accZ = accelZ / 16384.0;
  93.  
  94. // Calculate the magnitude of acceleration
  95. // accX * accX is equivalent to pow(accX, 2)
  96. float accMagnitude = sqrt(accX * accX + accY * accY + accZ * accZ);
  97. // 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.
  98.  
  99. // Peak detection
  100. if (accMagnitudePrev > accMagnitude + 0.1 && accMagnitudePrev > 1.5) {
  101. stepCount++;
  102. saveStepCount();
  103.  
  104. }
  105. accMagnitudePrev = accMagnitude;
  106. }
  107.  
  108. void displayStepCount() {
  109. Serial.print("Steps: ");
  110. Serial.println(stepCount);
  111. dtostrf(stepCount, 0, 1, RemoteXY.counter_01);
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment