Advertisement
angellin

MAPPING ACCELERATION PATTERNS TO MAXIMIZE GOLF SWING SPEED

Mar 29th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. //Define two integer variables for accelerometer and sensor values
  2. //Note: We are measuring only the z-axis acceleration
  3. int z_axisPin = A2;
  4. int z_sensorValue;
  5. int z_highValue = 0;
  6. int z_lowValue = 1023;
  7. int blueLED = 13;
  8. int redLED = 2;
  9.  
  10. //Introduce a new variable type called FLOAT -- a variable that store decimal numbers
  11. //Define the voltage source to the board. This can be the output of the battery or USB connection
  12. //Note: Incorrectly defining the voltage will generate incorrect voltage scale
  13. float supply_voltage = 3.38;
  14.  
  15. //Variables to store conversions, type FLOAT
  16. float z_voltage;
  17.  
  18.  
  19.  
  20. //Setup run once
  21. void setup() {
  22.  
  23. Serial.begin(9600);
  24. pinMode(z_axisPin,INPUT);
  25.  
  26. //wait 1 sec to stabilize the sensor
  27. delay(100);
  28.  
  29. }
  30.  
  31. void loop() {
  32.  
  33. //Read the sensor and store the integer value 0-1023
  34. z_sensorValue = analogRead(z_axisPin);
  35.  
  36. //keep track of which is the highest value
  37. if (z_sensorValue > z_highValue) {
  38. z_highValue = z_sensorValue;
  39. }
  40.  
  41. //keep track of which is the lowest value
  42. if (z_sensorValue < z_lowValue) {
  43. z_lowValue = z_sensorValue;
  44.  
  45. }
  46.  
  47. float swing_g = map (z_sensorValue, 505 , 1023, 1 ,16);
  48.  
  49. if (z_sensorValue > 750) {
  50. digitalWrite(blueLED, HIGH);
  51. delay(5000);
  52. digitalWrite(blueLED,LOW);
  53.  
  54. }
  55.  
  56. if (z_sensorValue < 750 && z_sensorValue > 600) {
  57. digitalWrite(redLED, HIGH);
  58. delay(5000);
  59. digitalWrite(redLED, LOW);
  60.  
  61.  
  62. }
  63. //Print out the sensor number and print a tab space - \t
  64. Serial.print("Z-axis reading: ");
  65. Serial.print(z_sensorValue);
  66. Serial.print("\t");
  67.  
  68. Serial.print("Z-axis high: ");
  69. Serial.print(z_highValue);
  70. Serial.print("\t");
  71.  
  72. Serial.print("Z-axis low: ");
  73. Serial.print(z_lowValue);
  74. Serial.print("\t");
  75.  
  76. //Convert to voltage and print result
  77. z_voltage = z_sensorValue * (supply_voltage/1024);
  78.  
  79. Serial.print("\tZ-axis voltage: ");
  80. Serial.println(z_voltage);
  81.  
  82. //Numbers to G force
  83.  
  84. Serial.print("gforce: ");
  85. Serial.println(swing_g);
  86.  
  87. //short delay before next reading. Sensor needs ~20ms between responses
  88. delay(10);
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement