Advertisement
DrAungWinHtut

bluetoothcar.ino

Mar 24th, 2022
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   This program is to communicate with bluetooth
  3.   and android mobile phone
  4.   by Dr. Aung Win Htut
  5.  
  6. */
  7. #include <SoftwareSerial.h>
  8.  
  9. SoftwareSerial mySerial(12, 11); // RX, TX
  10.  
  11. int in1=2; //Arduino pin 2 connected to L298 pin in1
  12. int in2=3; //Arduino pin 3 connected to L298 pin in2
  13. int in3=4; //Arduino pin 4 connected to L298 pin in3
  14. int in4=5; //Arduino pin 5 connected to L298 pin in4
  15. int ena=9; //Arduino pin 6 connected to L298 pin ena
  16. int enb=10; //Arduino pin 7 connected to L298 pin enb
  17. int d=0; //delay if you need to control speed
  18.  
  19. char ch;
  20. void setup() {
  21.   // Open serial communications and wait for port to open:
  22.   Serial.begin(9600);
  23.   while (!Serial) {
  24.     ; // wait for serial port to connect. Needed for native USB port only
  25.   }
  26.  
  27.   pinMode(in1, OUTPUT);
  28.   pinMode(in2, OUTPUT);
  29.   pinMode(in3, OUTPUT);
  30.   pinMode(in4, OUTPUT);
  31.   pinMode(ena, OUTPUT);
  32.   pinMode(enb, OUTPUT);
  33.  
  34.   Serial.println("Goodnight GreenHackers!, I am your bluetooth car");
  35.  
  36.   // set the data rate for the SoftwareSerial port
  37.   mySerial.begin(9600);
  38.   mySerial.println("Hello, world?");
  39. }
  40.  
  41. void loop() { // run over and over
  42.  
  43.   if (mySerial.available()) {
  44.     //Serial.println("bluetooth sending");
  45.     ch = mySerial.read();
  46.     Serial.println(ch);
  47.     if (ch == 'L')
  48.     {
  49.       funTurnLeft();
  50.     }
  51.     else if (ch == 'R')
  52.     {
  53.       funTrunRight();
  54.     }
  55.     else if (ch == 'F')
  56.     {
  57.       funForward();
  58.     }
  59.     else if (ch == 'B')
  60.     {
  61.       funBackward();
  62.     }
  63.     else if (ch == '5')
  64.     {
  65.       funLoopLeft();
  66.     }
  67.     else if (ch == '6')
  68.     {
  69.       funLoopRight();
  70.     }
  71.     else if (ch == 'S')
  72.     {
  73.       funStop();
  74.     }
  75.     else if (ch == '8')
  76.     {
  77.       funStop();
  78.     }
  79.  
  80.   }
  81.   if (Serial.available()) {
  82.     mySerial.write(Serial.read());
  83.   }
  84. }
  85.  
  86.  
  87. void funForward()
  88. {
  89.   digitalWrite(in1, HIGH);
  90.   digitalWrite(in2, LOW);
  91.   analogWrite(ena, 255);
  92.   digitalWrite(in3, HIGH);
  93.   digitalWrite(in4, LOW);
  94.   analogWrite(enb, 255);
  95.   delay(d);
  96.   //funStop();
  97.  
  98. }
  99. void funBackward()
  100. {
  101.   digitalWrite(in1, LOW);
  102.   digitalWrite(in2, HIGH);
  103.   analogWrite(ena, 255);
  104.   digitalWrite(in3, LOW);
  105.   digitalWrite(in4, HIGH);
  106.   analogWrite(enb, 255);
  107.   delay(d);
  108.   //funStop();
  109. }
  110. void funTurnLeft()
  111. {
  112.   digitalWrite(in1, HIGH);
  113.   digitalWrite(in2, LOW);
  114.   analogWrite(ena, 255);
  115.   digitalWrite(in3, LOW);
  116.   digitalWrite(in4, HIGH);
  117.   analogWrite(enb, 255);
  118.   delay(100);
  119.   funStop();
  120.  
  121. }
  122. void funTrunRight()
  123. {
  124.   digitalWrite(in1, LOW);
  125.   digitalWrite(in2, HIGH);
  126.   analogWrite(ena, 255);
  127.   digitalWrite(in3, HIGH);
  128.   digitalWrite(in4, LOW);
  129.   analogWrite(enb, 255);
  130.   delay(100);
  131.   funStop();
  132. }
  133. void funLoopLeft()
  134. {
  135.   digitalWrite(in1, HIGH);
  136.   digitalWrite(in2, LOW);
  137.   analogWrite(ena, 255);
  138.   digitalWrite(in3, LOW);
  139.   digitalWrite(in4, HIGH);
  140.   analogWrite(enb, 255);
  141. }
  142. void funLoopRight()
  143. {
  144.    digitalWrite(in1, LOW);
  145.   digitalWrite(in2, HIGH);
  146.   analogWrite(ena, 255);
  147.   digitalWrite(in3, HIGH);
  148.   digitalWrite(in4, LOW);
  149.   analogWrite(enb, 255);
  150. }
  151. void funStop()
  152. {
  153.   analogWrite(ena,0);
  154.   analogWrite(enb,0);
  155.   digitalWrite(in1, LOW);
  156.   digitalWrite(in2, LOW);
  157.   digitalWrite(in3, LOW);
  158.   digitalWrite(in4, LOW);
  159. }
  160.  
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement