safwan092

BT_Round_CAR_1_2_3_4_5

Dec 12th, 2022
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. #define INT_A 2 // control the left motor direction pin D2
  2. #define INT_B 4 // control the right motor direction pin D4
  3. #define left_A 9 // define the left motor speed control pin as D9
  4. #define right_B 5 // define the right motor speed control pin as D5
  5.  
  6. String str;
  7. int blueToothVal;
  8.  
  9.  
  10. void setup()
  11. {
  12. Serial.begin(9600);
  13. pinMode(INT_A, OUTPUT); // set the motor control pin as OUTPUT
  14. pinMode(INT_B, OUTPUT);
  15. pinMode(left_A, OUTPUT);
  16. pinMode(right_B, OUTPUT);
  17. }
  18.  
  19.  
  20. void loop() {
  21.  
  22. bluetooth();
  23. }//end of loop
  24.  
  25. void bluetooth()
  26. {
  27. while (Serial.available()){
  28. {
  29. str = Serial.readStringUntil('\n'); // str is the temporary variable for storing the last sring sent over bluetooth from Android device
  30. //Serial.print(str);
  31. }
  32.  
  33. blueToothVal = (str.toInt()); // convert the string 'str' into an integer and assign it to blueToothVal
  34.  
  35. switch (blueToothVal)
  36. {
  37. case 1:
  38. FRONT();
  39. delay(10);
  40. break;
  41.  
  42. case 2:
  43. BACKWARDS();
  44. delay(10);
  45. break;
  46.  
  47. case 3:
  48. LEFTT();
  49. delay(10);
  50. //STOP();
  51. break;
  52.  
  53. case 4:
  54. RIGHTT();
  55. delay(10);
  56. //STOP();
  57. break;
  58.  
  59. case 5:
  60. STOP();
  61. break;
  62.  
  63. } // end of switch case
  64.  
  65. } // end of while loop Serial1 read
  66.  
  67.  
  68. }
  69. // go forward
  70. void FRONT()
  71. {
  72. digitalWrite(INT_A, LOW); // control the left motor turn forward
  73. digitalWrite(INT_B, LOW); // control the right motor turn forward
  74. analogWrite(left_A, 200); // set the two motors’ speed(PWM=200)
  75. analogWrite(right_B, 200);
  76. }
  77. // backward
  78. void BACKWARDS()
  79. {
  80. digitalWrite(INT_A, HIGH); // control the left motor turn backward
  81. digitalWrite(INT_B, HIGH); // control the right motor turn backward
  82. analogWrite(left_A, 200); // set the two motors’ speed (PWM=200)
  83. analogWrite(right_B, 200);
  84. }
  85. // turn left
  86. void LEFTT()
  87. {
  88. digitalWrite(INT_A, HIGH); // control the left motor turn backward
  89. digitalWrite(INT_B, LOW); // control the right motor turn forward
  90. analogWrite(left_A, 100); // set the two motors’ speed(PWM为100)
  91. analogWrite(right_B, 100);
  92. }
  93. // turn right
  94. void RIGHTT()
  95. {
  96. digitalWrite(INT_A, LOW); // control the left motor turn forward
  97. digitalWrite(INT_B, HIGH); // control the right motor turn backward
  98. analogWrite(left_A, 100); // two motors’ speed(PWM为100)
  99. analogWrite(right_B, 100);
  100. }
  101. // stop
  102. void STOP()
  103. {
  104. digitalWrite(INT_A, LOW);
  105. digitalWrite(INT_B, LOW);
  106. analogWrite(left_A, 0); // both PWM are 0
  107. analogWrite(right_B, 0);
  108. }
Add Comment
Please, Sign In to add comment