Advertisement
NaroxEG

RC Bluetooth Car

Jun 26th, 2024
568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2.  
  3. #define MotorA_F 4
  4. #define MotorA_B 5
  5. #define MotorB_F 6
  6. #define MotorB_B 7
  7. #define ENA 9
  8. #define ENB 10
  9.  
  10. #define tx 3
  11. #define rx 2
  12.  
  13. SoftwareSerial Bluetooth(rx,tx);
  14.  
  15. char _read;
  16.  
  17. void setup() {
  18.   pinMode(MotorA_F, OUTPUT);
  19.   pinMode(MotorA_B, OUTPUT);
  20.   pinMode(MotorB_F, OUTPUT);
  21.   pinMode(MotorB_B, OUTPUT);
  22.  
  23.   pinMode(ENA, OUTPUT);
  24.   pinMode(ENB, OUTPUT);
  25.  
  26.   Bluetooth.begin(9600);
  27. }
  28.  
  29. void loop() {
  30.   // Controlling Speed
  31.   analogWrite(ENA, 255);
  32.   analogWrite(ENB, 255);
  33.   if (Bluetooth.available() > 0) {
  34.     _read = Bluetooth.read();
  35.  
  36.     if (_read == "F") {
  37.       digitalWrite(MotorA_F, HIGH);
  38.       digitalWrite(MotorA_B, LOW);
  39.  
  40.       digitalWrite(MotorB_F, HIGH);
  41.       digitalWrite(MotorB_B, LOW);
  42.     } else if (_read == "B") {
  43.       digitalWrite(MotorA_F, LOW);
  44.       digitalWrite(MotorA_B, HIGH);
  45.  
  46.       digitalWrite(MotorB_F, LOW);
  47.       digitalWrite(MotorB_B, HIGH);
  48.     } else if (_read == "L") {
  49.       digitalWrite(MotorA_F, LOW);
  50.       digitalWrite(MotorA_B, HIGH);
  51.  
  52.       digitalWrite(MotorB_F, HIGH);
  53.       digitalWrite(MotorB_B, LOW);
  54.     } else if (_read == "R") {
  55.       digitalWrite(MotorA_F, HIGH);
  56.       digitalWrite(MotorA_B, LOW);
  57.  
  58.       digitalWrite(MotorB_F, LOW);
  59.       digitalWrite(MotorB_B, HIGH);
  60.     }
  61.   }
  62.  
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement