Advertisement
pleasedontcode

**Sensor Data** rev_06

Nov 28th, 2024
59
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: **Sensor Data**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-11-28 23:37:11
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The project must implement the LM35 sensor for */
  21.     /* continuous temperature monitoring, with readings */
  22.     /* taken from analog pin A0. The setup should include */
  23.     /* necessary error handling for sensor malfunctions. */
  24. /****** SYSTEM REQUIREMENT 2 *****/
  25.     /* read gyroscope and accelerometer and print on */
  26.     /* serial. */
  27. /****** END SYSTEM REQUIREMENTS *****/
  28.  
  29. /* START CODE */
  30.  
  31. /****** DEFINITION OF LIBRARIES *****/
  32. #include <Wire.h>
  33. #include <LM35.h>   //https://github.com/wilmouths/LM35
  34. #include <BMI160.h> //https://github.com/EmotiBit/EmotiBit_BMI160
  35.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. void setup(void);
  38. void loop(void);
  39.  
  40. /***** DEFINITION OF ANALOG INPUT PINS *****/
  41. const uint8_t temp_LM35_Vout_PIN_A0     = A0;
  42.  
  43. /***** DEFINITION OF I2C PINS *****/
  44. const uint8_t accelerometer_BMI160_I2C_PIN_SDA_A4       = A4;
  45. const uint8_t accelerometer_BMI160_I2C_PIN_SCL_A5       = A5;
  46. const uint8_t accelerometer_BMI160_I2C_SLAVE_ADDRESS        = 104;
  47.  
  48. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  49. LM35 lm35(temp_LM35_Vout_PIN_A0); // Instantiate LM35 object
  50. BMI160 bmi160; // Instantiate BMI160 object
  51.  
  52. void setup(void)
  53. {
  54.     // Initialize serial communication for debugging
  55.     Serial.begin(9600);
  56.    
  57.     // Set the LM35 pin as input
  58.     pinMode(temp_LM35_Vout_PIN_A0, INPUT);
  59.    
  60.     // Initialize BMI160 sensor
  61.     bmi160.initialize();
  62.     if (!bmi160.testConnection()) {
  63.         Serial.println("BMI160 connection failed!");
  64.         while (1); // Halt the program if the sensor is not connected
  65.     }
  66. }
  67.  
  68. void loop(void)
  69. {
  70.     // Read temperature from LM35
  71.     double temperature = lm35.getTemp(); // Get temperature in Celsius
  72.     Serial.print("Temperature: ");
  73.     Serial.print(temperature);
  74.     Serial.println(" °C");
  75.  
  76.     // Read gyroscope and accelerometer data from BMI160
  77.     int16_t ax, ay, az, gx, gy, gz;
  78.     bmi160.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); // Get motion data
  79.  
  80.     Serial.print("Accelerometer: ");
  81.     Serial.print("X: "); Serial.print(ax);
  82.     Serial.print(", Y: "); Serial.print(ay);
  83.     Serial.print(", Z: "); Serial.println(az);
  84.  
  85.     Serial.print("Gyroscope: ");
  86.     Serial.print("X: "); Serial.print(gx);
  87.     Serial.print(", Y: "); Serial.print(gy);
  88.     Serial.print(", Z: "); Serial.println(gz);
  89.  
  90.     // Delay for a second before the next reading
  91.     delay(1000);
  92. }
  93.  
  94. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement