Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. /** loop前後にstep計算式を追加 **/
  2.  
  3. float accel, interval, preInterval;
  4. int stepcount,state,laststate=0;
  5. float total,threshold,hysteresis=0;
  6.  
  7. void loop() {
  8. byte rc;
  9. float acc[3];
  10. rc = KX126.get_val(acc);
  11.  
  12. for ( int i=1;i<=100;i++ ) {
  13. if (rc == 0) {
  14. // XYZ 軸の合成
  15. accel = sqrt( sq(acc[0]) + sq(acc[1]) + sq(acc[2]) );
  16. // 歩数カウントスタート
  17. if (i != 100) {
  18. total += accel;
  19. } else {
  20. threshold = total/i;
  21. hysteresis = threshold / 10;
  22. total = 0;
  23. }
  24. // 歩数カウントしきい値判定
  25. if ( accel > (threshold + hysteresis) ) {
  26. state = true;
  27. } else if ( accel < (threshold - hysteresis) ) {
  28. state = false;
  29. }
  30. // 歩数カウント
  31. if (laststate == false && state == true) {
  32. stepcount++;
  33. laststate = state;
  34. } else if (laststate == true && state == false) {
  35. laststate = state;
  36. }
  37. }
  38. }
  39. Serial.print("STEP: ");
  40. Serial.println(stepcount);
  41. Serial.println();
  42. delay(50);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement