Advertisement
makispaiktis

ADXL 335 - My Version

Mar 31st, 2021 (edited)
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const int xPin = A0;
  2. const int yPin = A1;
  3. const int zPin = A2;
  4.  
  5. // initialize minimum and maximum Raw Ranges for each axis
  6. int RawMin = 0;
  7. int RawMax = 1023;
  8.  
  9. // Take multiple samples to reduce noise
  10. const int sampleSize = 10;
  11.  
  12. void setup()
  13. {
  14.   Serial.begin(9600);
  15.   pinMode(A0, INPUT);
  16.   pinMode(A1, INPUT);
  17.   pinMode(A2, INPUT);
  18. }
  19.  
  20. void loop()
  21. {
  22.   //Read raw values
  23.   int xRaw = analogRead(xPin);
  24.   int yRaw = analogRead(yPin);
  25.   int zRaw = analogRead(zPin);
  26.  
  27.   // Convert raw values to 'milli-Gs"
  28.   long xScaled = map(xRaw, RawMin, RawMax, -3000, 3000);
  29.   long yScaled = map(yRaw, RawMin, RawMax, -3000, 3000);
  30.   long zScaled = map(zRaw, RawMin, RawMax, -3000, 3000);
  31.  
  32.   // re-scale to fractional Gs
  33.   float xAccel = xScaled / 1000.0;
  34.   float yAccel = yScaled / 1000.0;
  35.   float zAccel = zScaled / 1000.0;
  36.  
  37.   Serial.print("X, Y, Z  :: ");
  38.   Serial.print(xRaw - 336);
  39.   Serial.print(", ");
  40.   Serial.print(yRaw - 339);
  41.   Serial.print(", ");
  42.   Serial.print(zRaw - 407);
  43.   Serial.print(" :: ");
  44.   Serial.print(xAccel,0);
  45.   Serial.print("G, ");
  46.   Serial.print(yAccel,0);
  47.   Serial.print("G, ");
  48.   Serial.print(zAccel,0);
  49.   Serial.println("G");
  50.  
  51.   delay(200);
  52. }
  53.  
  54. // Take samples and return the average
  55. int ReadAxis(int axisPin)
  56. {
  57.   long reading = 0;
  58.   analogRead(axisPin);
  59.   delay(1);
  60.   for (int i = 0; i < sampleSize; i++)
  61.   {
  62.   reading += analogRead(axisPin);
  63.   }
  64.   return reading/sampleSize;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement