Advertisement
pleasedontcode

"Sensor-Controlled Motors" rev_15

Jun 23rd, 2024
429
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-Controlled Motors"
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2024-06-24 04:57:51
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* take input from 4 hall sensor for each wheel */
  21. /****** SYSTEM REQUIREMENT 2 *****/
  22.     /* take input pwm signal and send out to 4 brushless */
  23.     /* esc for each wheel. change the motor speed to */
  24.     /* maintain traction */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Arduino.h>
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void updateOutputs(void);
  34. void readHallSensors(void);
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t hallSensor1_PIN_A0 = A0;
  38. const uint8_t hallSensor2_PIN_A1 = A1;
  39. const uint8_t hallSensor3_PIN_A2 = A2;
  40. const uint8_t hallSensor4_PIN_A3 = A3;
  41. const uint8_t pwm_PIN_D2 = 2;
  42.  
  43. /***** DEFINITION OF PWM OUTPUT PINS *****/
  44. const uint8_t output1_PIN_D3 = 3;
  45. const uint8_t output2_PIN_D4 = 4;
  46. const uint8_t output3_PIN_D5 = 5;
  47. const uint8_t output4_PIN_D6 = 6;
  48.  
  49. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  50. /***** used to store raw data *****/
  51. uint8_t output1_PIN_D3_rawData = 0;
  52. uint8_t output2_PIN_D4_rawData = 0;
  53. uint8_t output3_PIN_D5_rawData = 0;
  54. uint8_t output4_PIN_D6_rawData = 0;
  55.  
  56. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  57. /***** used to store data after characteristic curve transformation *****/
  58. float output1_PIN_D3_phyData = 0.0;
  59. float output2_PIN_D4_phyData = 0.0;
  60. float output3_PIN_D5_phyData = 0.0;
  61. float output4_PIN_D6_phyData = 0.0;
  62.  
  63. /***** DEFINITION OF HALL SENSOR VARIABLES *****/
  64. /***** used to store hall sensor data *****/
  65. int hallSensor1_value = 0;
  66. int hallSensor2_value = 0;
  67. int hallSensor3_value = 0;
  68. int hallSensor4_value = 0;
  69.  
  70. void setup(void)
  71. {
  72.     // put your setup code here, to run once:
  73.     pinMode(hallSensor1_PIN_A0, INPUT);
  74.     pinMode(hallSensor2_PIN_A1, INPUT);
  75.     pinMode(hallSensor3_PIN_A2, INPUT);
  76.     pinMode(hallSensor4_PIN_A3, INPUT);
  77.     pinMode(pwm_PIN_D2, INPUT);
  78.  
  79.     pinMode(output1_PIN_D3, OUTPUT);
  80.     pinMode(output2_PIN_D4, OUTPUT);
  81.     pinMode(output3_PIN_D5, OUTPUT);
  82.     pinMode(output4_PIN_D6, OUTPUT);
  83. }
  84.  
  85. void loop(void)
  86. {
  87.     // put your main code here, to run repeatedly:
  88.     readHallSensors(); // Read hall sensor data
  89.     updateOutputs(); // Refresh output data
  90. }
  91.  
  92. void readHallSensors()
  93. {
  94.     // Read the value from each hall sensor
  95.     hallSensor1_value = analogRead(hallSensor1_PIN_A0);
  96.     hallSensor2_value = analogRead(hallSensor2_PIN_A1);
  97.     hallSensor3_value = analogRead(hallSensor3_PIN_A2);
  98.     hallSensor4_value = analogRead(hallSensor4_PIN_A3);
  99.  
  100.     // Process the hall sensor data to control motor speed
  101.     // This is a placeholder for the actual logic to maintain traction
  102.     output1_PIN_D3_rawData = map(hallSensor1_value, 0, 1023, 0, 255);
  103.     output2_PIN_D4_rawData = map(hallSensor2_value, 0, 1023, 0, 255);
  104.     output3_PIN_D5_rawData = map(hallSensor3_value, 0, 1023, 0, 255);
  105.     output4_PIN_D6_rawData = map(hallSensor4_value, 0, 1023, 0, 255);
  106. }
  107.  
  108. void updateOutputs()
  109. {
  110.     analogWrite(output1_PIN_D3, output1_PIN_D3_rawData);
  111.     analogWrite(output2_PIN_D4, output2_PIN_D4_rawData);
  112.     analogWrite(output3_PIN_D5, output3_PIN_D5_rawData);
  113.     analogWrite(output4_PIN_D6, output4_PIN_D6_rawData);
  114. }
  115.  
  116. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement