Advertisement
safwan092

Untitled

Feb 26th, 2023
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1.  
  2. #include <SoftwareSerial.h>
  3.  
  4. const byte rxPin = 8;
  5. const byte txPin = 9;
  6.  
  7. // Set up a new SoftwareSerial object
  8. SoftwareSerial mySerial(rxPin, txPin);
  9.  
  10. int IN1 = 4;
  11. int IN2 = 3;
  12. int IN3 = 7;
  13. int IN4 = 2;
  14. const int LSpeed = 5;
  15. const int RSpeed = 6;
  16. int speedR = 150;//150; //0-255
  17. int speedL = 150;//150;
  18.  
  19. String str;
  20. int blueToothVal;
  21.  
  22. #define trigPin 11 //ULTRASONIC input 1
  23. #define echoPin 12 //ULTRASONIC input 2
  24. long duration, distance;
  25.  
  26. void setup() {
  27. Serial.begin(9600);
  28. mySerial.begin(9600);
  29. pinMode(IN1, OUTPUT);
  30. pinMode(IN2, OUTPUT);
  31. pinMode(IN3, OUTPUT);
  32. pinMode(IN4, OUTPUT);
  33. pinMode(LSpeed, OUTPUT);
  34. pinMode(RSpeed, OUTPUT);
  35. pinMode(trigPin, OUTPUT);
  36. pinMode(echoPin, INPUT);
  37. analogWrite(LSpeed, speedL);
  38. analogWrite(RSpeed, speedR);
  39. }
  40.  
  41.  
  42. void loop() {
  43. delay(50);
  44.  
  45. digitalWrite(trigPin, LOW);
  46. delayMicroseconds(2);
  47. digitalWrite(trigPin, HIGH);
  48. delayMicroseconds(10);
  49. digitalWrite(trigPin, LOW);
  50. duration = pulseIn(echoPin, HIGH);
  51. distance = duration * 0.034 / 2;
  52. Serial.println(distance);
  53. delay(10);
  54.  
  55. if (distance <= 30) {
  56. STOP();
  57. delay(300);
  58. BACKWARDS();
  59. delay(400);
  60. STOP();
  61. delay(300);
  62. RIGHTT();
  63. delay(700);
  64. STOP();
  65. }
  66.  
  67. else {
  68. bluetooth();
  69. }
  70.  
  71. }
  72.  
  73. void bluetooth() {
  74. while (mySerial.available()) // Read bluetooth commands over Serial1 // Warning: If an error with Serial1 occurs, make sure Arduino Mega 2560 is Selected
  75. {
  76. {
  77. str = mySerial.readStringUntil('\n'); // str is the temporary variable for storing the last sring sent over bluetooth from Android device
  78. //Serial.print(str);
  79. }
  80.  
  81. blueToothVal = (str.toInt()); // convert the string 'str' into an integer and assign it to blueToothVal
  82. Serial.println(blueToothVal);
  83. switch (blueToothVal) {
  84. case 1:
  85. FRONT();
  86. delay(10);
  87. break;
  88.  
  89. case 2:
  90. BACKWARDS();
  91. delay(10);
  92. break;
  93.  
  94. case 3:
  95. LEFTT();
  96. delay(10);
  97. //STOP();
  98. break;
  99.  
  100. case 4:
  101. RIGHTT();
  102. delay(10);
  103. //STOP();
  104. break;
  105.  
  106. case 5:
  107. STOP();
  108. break;
  109.  
  110. } // end of switch case
  111.  
  112. } // end of while loop Serial1 read
  113. }
  114.  
  115. void STOP() {
  116. digitalWrite(IN1, 0);
  117. digitalWrite(IN2, 0);
  118. digitalWrite(IN3, 0);
  119. digitalWrite(IN4, 0);
  120. }
  121.  
  122. void FRONT() {
  123. digitalWrite(IN1, 0);
  124. digitalWrite(IN2, 1);
  125. digitalWrite(IN3, 1);
  126. digitalWrite(IN4, 0);
  127. }
  128.  
  129. void BACKWARDS() {
  130. digitalWrite(IN1, HIGH);
  131. digitalWrite(IN2, LOW);
  132. digitalWrite(IN3, LOW);
  133. digitalWrite(IN4, HIGH);
  134. }
  135.  
  136. void RIGHTT() {
  137. delay(100);
  138. digitalWrite(IN1, HIGH);
  139. digitalWrite(IN2, LOW);
  140. digitalWrite(IN3, HIGH);
  141. digitalWrite(IN4, LOW);
  142.  
  143. }
  144.  
  145. void LEFTT() {
  146.  
  147. delay(100);
  148. digitalWrite(IN1, LOW);
  149. digitalWrite(IN2, HIGH);
  150. digitalWrite(IN3, LOW);
  151. digitalWrite(IN4, HIGH);
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement