Advertisement
pleasedontcode

"Bluetooth Motor" rev_09

Jun 1st, 2024
683
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:19:51
  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_MotorDriver.h"  // Include the L298N_MotorDriver library
  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 IN1_PIN = 2;
  38. const uint8_t IN2_PIN = 4;
  39. const uint8_t EN_PIN = 5;
  40.  
  41. /***** DEFINITION OF Software Serial *****/
  42. const uint8_t blue_HC05_mySerial_PIN_SERIAL_TX_A0 = A0;
  43. const uint8_t blue_HC05_mySerial_PIN_SERIAL_RX_A1 = A1;
  44. SoftwareSerial blue_HC05_mySerial(blue_HC05_mySerial_PIN_SERIAL_RX_A1, blue_HC05_mySerial_PIN_SERIAL_TX_A0);
  45.  
  46. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  47. /***** used to store raw data *****/
  48. bool IN1_PIN_rawData = 0;
  49. bool IN2_PIN_rawData = 0;
  50. uint8_t EN_PIN_rawData = 0;
  51.  
  52. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  53. /***** used to store data after characteristic curve transformation *****/
  54. float IN1_PIN_phyData = 0.0;
  55. float IN2_PIN_phyData = 0.0;
  56. float EN_PIN_phyData = 0.0;
  57.  
  58. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  59. L298N_MotorDriver motor(EN_PIN, IN1_PIN, IN2_PIN);  // Initialize the motor driver
  60.  
  61. void setup(void)
  62. {
  63.     // put your setup code here, to run once:
  64.     pinMode(IN1_PIN, OUTPUT);
  65.     pinMode(IN2_PIN, OUTPUT);
  66.     pinMode(EN_PIN, OUTPUT);
  67.  
  68.     blue_HC05_mySerial.begin(9600);  // Initialize Bluetooth serial communication
  69.     Serial.begin(9600);              // Initialize serial communication
  70.     while (!Serial) {}
  71.  
  72.     motor.setSpeed(70);  // Set initial speed
  73.     motor.enable();      // Enable the motor
  74. }
  75.  
  76. void loop(void)
  77. {
  78.     // Check if data is available from the Bluetooth module
  79.     if (blue_HC05_mySerial.available())
  80.     {
  81.         char command = blue_HC05_mySerial.read();  // Read the command
  82.  
  83.         // Process the command
  84.         switch (command)
  85.         {
  86.             case 'F':  // Forward
  87.                 motor.setDirection(true);
  88.                 motor.enable();
  89.                 break;
  90.             case 'B':  // Backward
  91.                 motor.setDirection(false);
  92.                 motor.enable();
  93.                 break;
  94.             case 'L':  // Left
  95.                 motor.setSpeed(70);  // Adjust speed for turning
  96.                 motor.setDirection(true);
  97.                 motor.enable();
  98.                 break;
  99.             case 'R':  // Right
  100.                 motor.setSpeed(70);  // Adjust speed for turning
  101.                 motor.setDirection(false);
  102.                 motor.enable();
  103.                 break;
  104.             case 'S':  // Stop
  105.                 motor.disable();
  106.                 break;
  107.             default:
  108.                 Serial.println("Unknown command");
  109.                 break;
  110.         }
  111.  
  112.         printSomeInfo();  // Print motor status
  113.     }
  114.  
  115.     delay(100);  // Small delay to avoid overwhelming the serial buffer
  116. }
  117.  
  118. void updateOutputs()
  119. {
  120.     digitalWrite(IN1_PIN, IN1_PIN_rawData);
  121.     digitalWrite(IN2_PIN, IN2_PIN_rawData);
  122.     analogWrite(EN_PIN, EN_PIN_rawData);
  123. }
  124.  
  125. void printSomeInfo()
  126. {
  127.     Serial.print("Motor is moving = ");
  128.     Serial.print(motor.isEnabled());
  129.     Serial.print(" at speed = ");
  130.     Serial.println(motor.getSpeed());
  131. }
  132.  
  133. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement