Advertisement
safwan092

Untitled

Feb 7th, 2023
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1.  
  2. #include <SoftwareSerial.h>
  3.  
  4. const byte rxPin = 9;
  5. const byte txPin = 10;
  6.  
  7. // Set up a new SoftwareSerial object
  8. SoftwareSerial mySerial(rxPin, txPin);
  9.  
  10. int IN1 = 2;
  11. int IN2 = 7;
  12. int IN3 = 4;
  13. int IN4 = 3;
  14. const int LSpeed = 6;
  15. const int RSpeed = 5;
  16. int speedR = 150; //0-2e55
  17. int speedL = 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. }
  38.  
  39.  
  40. void loop() {
  41. analogWrite(LSpeed, speedL);
  42. analogWrite(RSpeed, speedR);
  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. void bluetooth() {
  73. while (mySerial.available()) // Read bluetooth commands over Serial1 // Warning: If an error with Serial1 occurs, make sure Arduino Mega 2560 is Selected
  74. {
  75. {
  76. str = mySerial.readStringUntil('\n'); // str is the temporary variable for storing the last sring sent over bluetooth from Android device
  77. //Serial.print(str);
  78. }
  79.  
  80. blueToothVal = (str.toInt()); // convert the string 'str' into an integer and assign it to blueToothVal
  81. Serial.println(blueToothVal);
  82. switch (blueToothVal) {
  83. case 1:
  84. FRONT();
  85. delay(10);
  86. break;
  87.  
  88. case 2:
  89. BACKWARDS();
  90. delay(10);
  91. break;
  92.  
  93. case 3:
  94. LEFTT();
  95. delay(10);
  96. //STOP();
  97. break;
  98.  
  99. case 4:
  100. RIGHTT();
  101. delay(10);
  102. //STOP();
  103. break;
  104.  
  105. case 5:
  106. STOP();
  107. break;
  108.  
  109. } // end of switch case
  110.  
  111. } // end of while loop Serial1 read
  112. }
  113.  
  114. void STOP() {
  115. digitalWrite(IN1, 0);
  116. digitalWrite(IN2, 0);
  117.  
  118. digitalWrite(IN3, 0);
  119. digitalWrite(IN4, 0);
  120. }
  121.  
  122. void FRONT() {
  123. digitalWrite(IN1, 0);
  124. digitalWrite(IN2, 1);
  125.  
  126. digitalWrite(IN3, 0);
  127. digitalWrite(IN4, 1);
  128. }
  129.  
  130. void BACKWARDS() {
  131. digitalWrite(IN1, 1);
  132. digitalWrite(IN2, 0);
  133.  
  134. digitalWrite(IN3, 1);
  135. digitalWrite(IN4, 0);
  136. }
  137.  
  138. void RIGHTT() {
  139.  
  140. //delay(100);
  141. digitalWrite(IN1, 0);
  142. digitalWrite(IN2, 1);
  143.  
  144. digitalWrite(IN3, 1);
  145. digitalWrite(IN4, 0);
  146. }
  147.  
  148. void LEFTT() {
  149.  
  150. //delay(100);
  151. digitalWrite(IN1, 1);
  152. digitalWrite(IN2, 0);
  153.  
  154. digitalWrite(IN3, 0);
  155. digitalWrite(IN4, 1);
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement