Advertisement
pleasedontcode

BluetoothControlledCar

Feb 13th, 2024
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.82 KB | Source Code | 0 0
  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: Mobile_Robot_v2
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-02-07 23:41:06
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* If HC receives "F", move forward at 90% speed.  If */
  21.     /* HC receives "B", move backward at 50% speed. */
  22. /****** SYSTEM REQUIREMENT 2 *****/
  23.     /* If HC receives "L", turn left.   If HC receives */
  24.     /* "R", turn right.  If HC receives "0", stop. */
  25. /****** SYSTEM REQUIREMENT 3 *****/
  26.     /* Define an instance to drive motor A.  Define an */
  27.     /* instance to drive motor B. */
  28. /****** END SYSTEM REQUIREMENTS *****/
  29.  
  30.  
  31. /********* User code review feedback **********
  32. #### Feedback 1 ####
  33. - updateOutputs() is unnecessary.
  34. ********* User code review feedback **********/
  35.  
  36. /****** DEFINITION OF LIBRARIES *****/
  37. #include <SoftwareSerial.h>
  38. #include <L298N.h>
  39.  
  40. /****** SYSTEM REQUIREMENTS *****/
  41. /****** SYSTEM REQUIREMENT 1 *****/
  42.     /* If HC receives "F", move forward at 90% speed.  If */
  43.     /* HC receives "B", move backward at 50% speed. */
  44. /****** SYSTEM REQUIREMENT 2 *****/
  45.     /* If HC receives "L", turn left.   If HC receives */
  46.     /* "R", turn right.  If HC receives "0", stop. */
  47. /****** SYSTEM REQUIREMENT 3 *****/
  48.     /* Define an instance to drive motor A.  Define an */
  49.     /* instance to drive motor B. */
  50. /****** END SYSTEM REQUIREMENTS *****/
  51.  
  52. /****** FUNCTION PROTOTYPES *****/
  53. void setup(void);
  54. void loop(void);
  55. void handleCommand(char command);
  56.  
  57. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  58. const uint8_t IN1_PIN = 2;
  59. const uint8_t IN2_PIN = 4;
  60. const uint8_t IN3_PIN = 6;
  61. const uint8_t IN4_PIN = 7;
  62.  
  63. /***** DEFINITION OF PWM OUTPUT PINS *****/
  64. const uint8_t EN1_PIN = 3;
  65. const uint8_t EN2_PIN = 5;
  66.  
  67. /***** DEFINITION OF Software Serial *****/
  68. const uint8_t HC05_SERIAL_TX_PIN = A0;
  69. const uint8_t HC05_SERIAL_RX_PIN = A1;
  70. SoftwareSerial HC05_SERIAL(HC05_SERIAL_RX_PIN, HC05_SERIAL_TX_PIN);
  71.  
  72. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  73. uint8_t EN1_rawData = 0;
  74. uint8_t EN2_rawData = 0;
  75.  
  76. /****** DEFINITION OF LIBRARY CLASS INSTANCES*****/
  77. L298N motorA(EN1_PIN, IN1_PIN, IN2_PIN);
  78. L298N motorB(EN2_PIN, IN3_PIN, IN4_PIN);
  79.  
  80. void setup(void)
  81. {
  82.   pinMode(IN1_PIN, OUTPUT);
  83.   pinMode(IN2_PIN, OUTPUT);
  84.   pinMode(IN3_PIN, OUTPUT);
  85.   pinMode(IN4_PIN, OUTPUT);
  86.   pinMode(EN1_PIN, OUTPUT);
  87.   pinMode(EN2_PIN, OUTPUT);
  88.  
  89.   HC05_SERIAL.begin(9600);
  90. }
  91.  
  92. void loop(void)
  93. {
  94.   if (HC05_SERIAL.available())
  95.   {
  96.     char command = HC05_SERIAL.read();
  97.     handleCommand(command);
  98.   }
  99. }
  100.  
  101. void handleCommand(char command)
  102. {
  103.   switch (command)
  104.   {
  105.     case 'F': // Move forward at 90% speed
  106.       motorA.setSpeed(230);
  107.       motorB.setSpeed(230);
  108.       motorA.forward();
  109.       motorB.forward();
  110.       break;
  111.  
  112.     case 'B': // Move backward at 50% speed
  113.       motorA.setSpeed(128);
  114.       motorB.setSpeed(128);
  115.       motorA.backward();
  116.       motorB.backward();
  117.       break;
  118.  
  119.     case 'L': // Turn left
  120.       motorA.setSpeed(0);
  121.       motorB.setSpeed(230);
  122.       motorA.backward();
  123.       motorB.forward();
  124.       break;
  125.  
  126.     case 'R': // Turn right
  127.       motorA.setSpeed(230);
  128.       motorB.setSpeed(0);
  129.       motorA.forward();
  130.       motorB.backward();
  131.       break;
  132.  
  133.     case '0': // Stop
  134.       motorA.setSpeed(0);
  135.       motorB.setSpeed(0);
  136.       motorA.stop();
  137.       motorB.stop();
  138.       break;
  139.   }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement