Advertisement
pleasedontcode

"Bluetooth Motor" rev_06

Jun 1st, 2024
655
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: "Bluetooth Motor"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-01 19:54:24
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The system must receive input via the HC-05 */
  21.     /* Bluetooth module to control the L298N motor driver */
  22.     /* for moving the wheels forward, backward, left, */
  23.     /* right, and stop. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <SoftwareSerial.h>
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void updateOutputs(void);
  33. void processCommand(char command);
  34.  
  35. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  36. const uint8_t motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2 = 2;
  37. const uint8_t motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4 = 4;
  38. const uint8_t motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6 = 6;
  39. const uint8_t motordriver_L298NDualFull_BridgeMotorDriver_IN4_PIN_D7 = 7;
  40.  
  41. /***** DEFINITION OF PWM OUTPUT PINS *****/
  42. const uint8_t motordriver_L298NDualFull_BridgeMotorDriver_ENA_PIN_D5 = 5;
  43. const uint8_t motordriver_L298NDualFull_BridgeMotorDriver_ENB_PIN_D9 = 9;
  44.  
  45. /***** DEFINITION OF Software Serial *****/
  46. const uint8_t blue_HC05_mySerial_PIN_SERIAL_TX_A0 = A0;
  47. const uint8_t blue_HC05_mySerial_PIN_SERIAL_RX_A1 = A1;
  48. SoftwareSerial blue_HC05_mySerial(blue_HC05_mySerial_PIN_SERIAL_RX_A1, blue_HC05_mySerial_PIN_SERIAL_TX_A0);
  49.  
  50. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  51. /***** used to store raw data *****/
  52. bool motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2_rawData = 0;
  53. bool motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4_rawData = 0;
  54. bool motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6_rawData = 0;
  55. bool motordriver_L298NDualFull_BridgeMotorDriver_IN4_PIN_D7_rawData = 0;
  56. uint8_t motordriver_L298NDualFull_BridgeMotorDriver_ENA_PIN_D5_rawData = 0;
  57. uint8_t motordriver_L298NDualFull_BridgeMotorDriver_ENB_PIN_D9_rawData = 0;
  58.  
  59. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  60. /***** used to store data after characteristic curve transformation *****/
  61. float motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2_phyData = 0.0;
  62. float motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4_phyData = 0.0;
  63. float motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6_phyData = 0.0;
  64. float motordriver_L298NDualFull_BridgeMotorDriver_IN4_PIN_D7_phyData = 0.0;
  65. float motordriver_L298NDualFull_BridgeMotorDriver_ENA_PIN_D5_phyData = 0.0;
  66. float motordriver_L298NDualFull_BridgeMotorDriver_ENB_PIN_D9_phyData = 0.0;
  67.  
  68. void setup(void)
  69. {
  70.     // Initialize motor driver pins as outputs
  71.     pinMode(motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2, OUTPUT);
  72.     pinMode(motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4, OUTPUT);
  73.     pinMode(motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6, OUTPUT);
  74.     pinMode(motordriver_L298NDualFull_BridgeMotorDriver_IN4_PIN_D7, OUTPUT);
  75.     pinMode(motordriver_L298NDualFull_BridgeMotorDriver_ENA_PIN_D5, OUTPUT);
  76.     pinMode(motordriver_L298NDualFull_BridgeMotorDriver_ENB_PIN_D9, OUTPUT);
  77.  
  78.     // Initialize Bluetooth serial communication
  79.     blue_HC05_mySerial.begin(9600);
  80. }
  81.  
  82. void loop(void)
  83. {
  84.     // Check if data is available from the Bluetooth module
  85.     if (blue_HC05_mySerial.available())
  86.     {
  87.         char command = blue_HC05_mySerial.read(); // Read the command
  88.         processCommand(command); // Process the command
  89.     }
  90.  
  91.     updateOutputs(); // Refresh output data
  92. }
  93.  
  94. void updateOutputs()
  95. {
  96.     // Update digital outputs
  97.     digitalWrite(motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2, motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2_rawData);
  98.     digitalWrite(motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4, motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4_rawData);
  99.     digitalWrite(motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6, motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6_rawData);
  100.     digitalWrite(motordriver_L298NDualFull_BridgeMotorDriver_IN4_PIN_D7, motordriver_L298NDualFull_BridgeMotorDriver_IN4_PIN_D7_rawData);
  101.    
  102.     // Update PWM outputs
  103.     analogWrite(motordriver_L298NDualFull_BridgeMotorDriver_ENA_PIN_D5, motordriver_L298NDualFull_BridgeMotorDriver_ENA_PIN_D5_rawData);
  104.     analogWrite(motordriver_L298NDualFull_BridgeMotorDriver_ENB_PIN_D9, motordriver_L298NDualFull_BridgeMotorDriver_ENB_PIN_D9_rawData);
  105. }
  106.  
  107. void processCommand(char command)
  108. {
  109.     switch (command)
  110.     {
  111.     case 'F': // Move Forward
  112.         motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2_rawData = HIGH;
  113.         motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4_rawData = LOW;
  114.         motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6_rawData = HIGH;
  115.         motordriver_L298NDualFull_BridgeMotorDriver_IN4_PIN_D7_rawData = LOW;
  116.         motordriver_L298NDualFull_BridgeMotorDriver_ENA_PIN_D5_rawData = 255; // Full speed
  117.         motordriver_L298NDualFull_BridgeMotorDriver_ENB_PIN_D9_rawData = 255; // Full speed
  118.         break;
  119.     case 'B': // Move Backward
  120.         motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2_rawData = LOW;
  121.         motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4_rawData = HIGH;
  122.         motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6_rawData = LOW;
  123.         motordriver_L298NDualFull_BridgeMotorDriver_IN4_PIN_D7_rawData = HIGH;
  124.         motordriver_L298NDualFull_BridgeMotorDriver_ENA_PIN_D5_rawData = 255; // Full speed
  125.         motordriver_L298NDualFull_BridgeMotorDriver_ENB_PIN_D9_rawData = 255; // Full speed
  126.         break;
  127.     case 'L': // Turn Left
  128.         motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2_rawData = LOW;
  129.         motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4_rawData = HIGH;
  130.         motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6_rawData = HIGH;
  131.         motordriver_L298NDualFull_BridgeMotorDriver_IN4_PIN_D7_rawData = LOW;
  132.         motordriver_L298NDualFull_BridgeMotorDriver_ENA_PIN_D5_rawData = 255; // Full speed
  133.         motordriver_L298NDualFull_BridgeMotorDriver_ENB_PIN_D9_rawData = 255; // Full speed
  134.         break;
  135.     case 'R': // Turn Right
  136.         motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2_rawData = HIGH;
  137.         motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4_rawData = LOW;
  138.         motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6_rawData = LOW;
  139.         motordriver_L298NDualFull_BridgeMotorDriver_IN4_PIN_D7_rawData = HIGH;
  140.         motordriver_L298NDualFull_BridgeMotorDriver_ENA_PIN_D5_rawData = 255; // Full speed
  141.         motordriver_L298NDualFull_BridgeMotorDriver_ENB_PIN_D9_rawData = 255; // Full speed
  142.         break;
  143.     case 'S': // Stop
  144.         motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2_rawData = LOW;
  145.         motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4_rawData = LOW;
  146.         motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6_rawData = LOW;
  147.         motordriver_L298NDualFull_BridgeMotorDriver_IN4_PIN_D7_rawData = LOW;
  148.         motordriver_L298NDualFull_BridgeMotorDriver_ENA_PIN_D5_rawData = 0; // Stop
  149.         motordriver_L298NDualFull_BridgeMotorDriver_ENB_PIN_D9_rawData = 0; // Stop
  150.         break;
  151.     default:
  152.         // Invalid command
  153.         break;
  154.     }
  155. }
  156.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement