Advertisement
pleasedontcode

"Bluetooth Motor" rev_08

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