Advertisement
pleasedontcode

"Bluetooth Motor" rev_07

Jun 1st, 2024
662
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 20:05:20
  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. #include <L298N.h>  //https://github.com/AndreaLombardo/L298N
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void updateOutputs(void);
  34. void printSomeInfo(void);
  35.  
  36. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  37. const uint8_t motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2 = 2;
  38. const uint8_t motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4 = 4;
  39. const uint8_t motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6 = 6;
  40.  
  41. /***** DEFINITION OF PWM OUTPUT PINS *****/
  42. const uint8_t motordriver_L298NDualFull_BridgeMotorDriver_EN2_PIN_D5 = 5;
  43.  
  44. /***** DEFINITION OF Software Serial *****/
  45. const uint8_t blue_HC05_mySerial_PIN_SERIAL_TX_A0 = A0;
  46. const uint8_t blue_HC05_mySerial_PIN_SERIAL_RX_A1 = A1;
  47. SoftwareSerial blue_HC05_mySerial(blue_HC05_mySerial_PIN_SERIAL_RX_A1, blue_HC05_mySerial_PIN_SERIAL_TX_A0);
  48.  
  49. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  50. /***** used to store raw data *****/
  51. bool motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2_rawData = 0;
  52. bool motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4_rawData = 0;
  53. bool motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6_rawData = 0;
  54. uint8_t motordriver_L298NDualFull_BridgeMotorDriver_EN2_PIN_D5_rawData = 0;
  55.  
  56. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  57. /***** used to store data after characteristic curve transformation *****/
  58. float motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2_phyData = 0.0;
  59. float motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4_phyData = 0.0;
  60. float motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6_phyData = 0.0;
  61. float motordriver_L298NDualFull_BridgeMotorDriver_EN2_PIN_D5_phyData = 0.0;
  62.  
  63. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  64. L298N motor(motordriver_L298NDualFull_BridgeMotorDriver_EN2_PIN_D5, motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2, motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4);
  65.  
  66. void setup(void)
  67. {
  68.   // put your setup code here, to run once:
  69.   pinMode(motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2, OUTPUT);
  70.   pinMode(motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4, OUTPUT);
  71.   pinMode(motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6, OUTPUT);
  72.   pinMode(motordriver_L298NDualFull_BridgeMotorDriver_EN2_PIN_D5, OUTPUT);
  73.  
  74.   blue_HC05_mySerial.begin(9600); // Initialize Bluetooth serial communication
  75.   Serial.begin(9600); // Initialize serial communication for debugging
  76.   while (!Serial) {}
  77.  
  78.   motor.setSpeed(70); // Set initial motor speed
  79. }
  80.  
  81. void loop(void)
  82. {
  83.   // put your main code here, to run repeatedly:
  84.   updateOutputs(); // Refresh output data
  85.  
  86.   if (blue_HC05_mySerial.available()) {
  87.     char command = blue_HC05_mySerial.read(); // Read command from Bluetooth
  88.  
  89.     switch (command) {
  90.       case 'F': // Forward
  91.         motor.forward();
  92.         break;
  93.       case 'B': // Backward
  94.         motor.backward();
  95.         break;
  96.       case 'L': // Left
  97.         // Implement left turn logic
  98.         motor.setSpeed(70); // Reduce speed for turning
  99.         motor.forward();
  100.         delay(500); // Adjust delay for turning duration
  101.         motor.setSpeed(255); // Restore speed after turning
  102.         break;
  103.       case 'R': // Right
  104.         // Implement right turn logic
  105.         motor.setSpeed(70); // Reduce speed for turning
  106.         motor.backward();
  107.         delay(500); // Adjust delay for turning duration
  108.         motor.setSpeed(255); // Restore speed after turning
  109.         break;
  110.       case 'S': // Stop
  111.         motor.stop();
  112.         break;
  113.       default:
  114.         // Invalid command
  115.         break;
  116.     }
  117.     printSomeInfo(); // Print motor status
  118.   }
  119. }
  120.  
  121. void updateOutputs()
  122. {
  123.   digitalWrite(motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2, motordriver_L298NDualFull_BridgeMotorDriver_IN1_PIN_D2_rawData);
  124.   digitalWrite(motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4, motordriver_L298NDualFull_BridgeMotorDriver_IN2_PIN_D4_rawData);
  125.   digitalWrite(motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6, motordriver_L298NDualFull_BridgeMotorDriver_IN3_PIN_D6_rawData);
  126.   analogWrite(motordriver_L298NDualFull_BridgeMotorDriver_EN2_PIN_D5, motordriver_L298NDualFull_BridgeMotorDriver_EN2_PIN_D5_rawData);
  127. }
  128.  
  129. void printSomeInfo()
  130. {
  131.   Serial.print("Motor is moving = ");
  132.   Serial.print(motor.isMoving());
  133.   Serial.print(" at speed = ");
  134.   Serial.println(motor.getSpeed());
  135. }
  136.  
  137. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement