Advertisement
pleasedontcode

**Sensor Control** rev_01

Feb 17th, 2025
205
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 Control**
  13.     - Source Code NOT compiled for: ESP8266 NodeMCU V1.0
  14.     - Source Code created on: 2025-02-17 21:07:50
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Control two servomotors using PWM signals while */
  21.     /* reading data from an MPU6050 sensor for motion */
  22.     /* detection. Ensure smooth operation and */
  23.     /* responsiveness in real-time applications. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Wire.h>
  30. #include <MPU6050.h>    //https://github.com/electroniccats/mpu6050
  31. #include <Deneyap_Servo.h>  //https://github.com/deneyapkart/deneyap-servo-arduino-library
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void updateOutputs();
  37. void readSensorData();
  38.  
  39. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  40. const uint8_t sensor_MPU6050_Interrupt_PIN_D5       = 5;
  41.  
  42. /***** DEFINITION OF PWM OUTPUT PINS *****/
  43. const uint8_t servo1_Servomotor_PWMSignal_PIN_D6        = 6;
  44. const uint8_t servo2_Servomotor_PWMSignal_PIN_D7        = 7;
  45.  
  46. /***** DEFINITION OF I2C PINS *****/
  47. const uint8_t sensor_MPU6050_I2C_PIN_SDA_D2     = 2;
  48. const uint8_t sensor_MPU6050_I2C_PIN_SCL_D1     = 1;
  49. const uint8_t sensor_MPU6050_I2C_SLAVE_ADDRESS      = 104;
  50.  
  51. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  52. uint8_t servo1_Servomotor_PWMSignal_PIN_D6_rawData      = 0;
  53. uint8_t servo2_Servomotor_PWMSignal_PIN_D7_rawData      = 0;
  54.  
  55. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  56. float   servo1_Servomotor_PWMSignal_PIN_D6_phyData      = 0.0;
  57. float   servo2_Servomotor_PWMSignal_PIN_D7_phyData      = 0.0;
  58.  
  59. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  60. // Create instances for MPU6050 and Servos
  61. MPU6050 mpu(sensor_MPU6050_I2C_SLAVE_ADDRESS);
  62. Servo servo1;
  63. Servo servo2;
  64.  
  65. void setup(void)
  66. {
  67.     // put your setup code here, to run once:
  68.  
  69.     pinMode(sensor_MPU6050_Interrupt_PIN_D5, INPUT);
  70.     pinMode(servo1_Servomotor_PWMSignal_PIN_D6, OUTPUT);
  71.     pinMode(servo2_Servomotor_PWMSignal_PIN_D7, OUTPUT);
  72.  
  73.     // Setup for built-in LED
  74.     pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
  75.  
  76.     // Initialize the MPU6050
  77.     mpu.initialize();
  78. }
  79.  
  80. void loop(void)
  81. {
  82.     // put your main code here, to run repeatedly:
  83.  
  84.     readSensorData(); // Read data from the MPU6050 sensor
  85.     updateOutputs(); // Refresh output data
  86.  
  87.     // USER CODE: Control the built-in LED
  88.     digitalWrite(LED_BUILTIN, LOW);  
  89.     delay(1000);                    
  90.     digitalWrite(LED_BUILTIN, HIGH);  
  91.     delay(2000);                      
  92. }
  93.  
  94. void updateOutputs()
  95. {
  96.     // Update PWM signals for servos
  97.     servo1.write(servo1_Servomotor_PWMSignal_PIN_D6_rawData);
  98.     servo2.write(servo2_Servomotor_PWMSignal_PIN_D7_rawData);
  99. }
  100.  
  101. void readSensorData()
  102. {
  103.     // Read accelerometer and gyroscope data
  104.     int16_t ax, ay, az, gx, gy, gz;
  105.     mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  106.  
  107.     // Process the sensor data to control the servos
  108.     // Example: Map accelerometer values to servo positions
  109.     servo1_Servomotor_PWMSignal_PIN_D6_rawData = map(ax, -17000, 17000, SERVOMIN, SERVOMAX);
  110.     servo2_Servomotor_PWMSignal_PIN_D7_rawData = map(ay, -17000, 17000, SERVOMIN, SERVOMAX);
  111. }
  112.  
  113. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement