Advertisement
pleasedontcode

**Accelerometer Data** rev_01

Dec 21st, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Accelerometer Data**
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2024-12-21 15:44:26
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* El objetivo del proyecto es crear un sistema */
  21.     /* Arduino que utilice bibliotecas específicas para */
  22.     /* la gestión de sensores, asegurando un */
  23.     /* procesamiento de datos eficiente y una interacción */
  24.     /* fluida del usuario a través de funciones bien */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <Wire.h>
  31. #include <Adafruit_Sensor.h>
  32. #include <Adafruit_ADXL343.h>
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37.  
  38. // Instantiate the Adafruit_ADXL343 object with a unique ID
  39. Adafruit_ADXL343 accel = Adafruit_ADXL343(12345);
  40.  
  41. void setup(void)
  42. {
  43.     Serial.begin(9600);
  44.     while (!Serial); // Wait for Serial Monitor to open
  45.     Serial.println("Accelerometer Test");
  46.     Serial.println("");
  47.  
  48.     /* Initialise the sensor */
  49.     if (!accel.begin())
  50.     {
  51.         /* There was a problem detecting the ADXL343 ... check your connections */
  52.         Serial.println("Ooops, no ADXL343 detected ... Check your wiring!");
  53.         while (1);
  54.     }
  55.  
  56.     /* Set the range to whatever is appropriate for your project */
  57.     accel.setRange(ADXL343_RANGE_16_G);
  58.  
  59.     /* Display some basic information on this sensor */
  60.     displaySensorDetails();
  61.     displayDataRate();
  62.     displayRange();
  63.     Serial.println("");
  64. }
  65.  
  66. void loop(void)
  67. {
  68.     /* Get a new sensor event */
  69.     sensors_event_t event;
  70.     accel.getEvent(&event);
  71.  
  72.     /* Display the results (acceleration is measured in m/s^2) */
  73.     Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print("  ");
  74.     Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print("  ");
  75.     Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.print("  "); Serial.println("m/s^2 ");
  76.     delay(500);
  77. }
  78.  
  79. /* Function to display sensor details */
  80. void displaySensorDetails(void)
  81. {
  82.     sensor_t sensor;
  83.     accel.getSensor(&sensor);
  84.     Serial.println("------------------------------------");
  85.     Serial.print("Sensor:       "); Serial.println(sensor.name);
  86.     Serial.print("Driver Ver:   "); Serial.println(sensor.version);
  87.     Serial.print("Unique ID:    "); Serial.println(sensor.sensor_id);
  88.     Serial.print("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" m/s^2");
  89.     Serial.print("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" m/s^2");
  90.     Serial.print("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" m/s^2");
  91.     Serial.println("------------------------------------");
  92.     Serial.println("");
  93.     delay(500);
  94. }
  95.  
  96. /* Function to display data rate */
  97. void displayDataRate(void)
  98. {
  99.     Serial.print("Data Rate:    ");
  100.     switch (accel.getDataRate())
  101.     {
  102.         case ADXL343_DATARATE_3200_HZ: Serial.print("3200 "); break;
  103.         case ADXL343_DATARATE_1600_HZ: Serial.print("1600 "); break;
  104.         case ADXL343_DATARATE_800_HZ: Serial.print("800 "); break;
  105.         case ADXL343_DATARATE_400_HZ: Serial.print("400 "); break;
  106.         case ADXL343_DATARATE_200_HZ: Serial.print("200 "); break;
  107.         case ADXL343_DATARATE_100_HZ: Serial.print("100 "); break;
  108.         case ADXL343_DATARATE_50_HZ: Serial.print("50 "); break;
  109.         case ADXL343_DATARATE_25_HZ: Serial.print("25 "); break;
  110.         case ADXL343_DATARATE_12_5_HZ: Serial.print("12.5 "); break;
  111.         case ADXL343_DATARATE_6_25HZ: Serial.print("6.25 "); break;
  112.         case ADXL343_DATARATE_3_13_HZ: Serial.print("3.13 "); break;
  113.         case ADXL343_DATARATE_1_56_HZ: Serial.print("1.56 "); break;
  114.         case ADXL343_DATARATE_0_78_HZ: Serial.print("0.78 "); break;
  115.         case ADXL343_DATARATE_0_39_HZ: Serial.print("0.39 "); break;
  116.         case ADXL343_DATARATE_0_20_HZ: Serial.print("0.20 "); break;
  117.         case ADXL343_DATARATE_0_10_HZ: Serial.print("0.10 "); break;
  118.         default: Serial.print("???? "); break;
  119.     }
  120.     Serial.println(" Hz");
  121. }
  122.  
  123. /* Function to display range */
  124. void displayRange(void)
  125. {
  126.     Serial.print("Range:         +/- ");
  127.     switch (accel.getRange())
  128.     {
  129.         case ADXL343_RANGE_16_G: Serial.print("16 "); break;
  130.         case ADXL343_RANGE_8_G: Serial.print("8 "); break;
  131.         case ADXL343_RANGE_4_G: Serial.print("4 "); break;
  132.         case ADXL343_RANGE_2_G: Serial.print("2 "); break;
  133.         default: Serial.print("?? "); break;
  134.     }
  135.     Serial.println(" g");
  136. }
  137.  
  138. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement