safwan092

Untitled

Nov 25th, 2022
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. //For millis function:
  2. //Updating the millis for two event
  3. unsigned long previousMillis1 = 0;
  4. unsigned long previousMillis2 = 0;
  5. const unsigned long event_intervalR = 400; //The time to move right (in milliseconds)
  6. const unsigned long event_intervalF = 700; //The time to move front (in milliseconds)
  7.  
  8. //motors:
  9. int IN1 = 4;
  10. int IN2 = 7;
  11. int IN3 = 8;
  12. int IN4 = 9;
  13. int ENA = 5;
  14. int ENB = 6;
  15.  
  16. //IR sensor:
  17. #define senRPin 3
  18. int senRState = 0;
  19.  
  20. #define senLPin 2
  21. int senLState = 0;
  22.  
  23. #define senBPin 10
  24. int senBState = 0;
  25.  
  26. // needed in searching function
  27. bool frontFlag = 0;
  28. bool rightFlag = 0;
  29.  
  30. //Ultasonic sensor:
  31. #define echoPin A0
  32. #define trigPin A1
  33. int maximumRange = 90; //in cm
  34. //For calculating the distance to any object in front of the robot
  35. long duration, distance;
  36.  
  37. void setup() {
  38. Serial.begin(9600);
  39.  
  40. //motor
  41. pinMode(IN1, OUTPUT);
  42. pinMode(IN2, OUTPUT);
  43. pinMode(IN3, OUTPUT);
  44. pinMode(IN4, OUTPUT);
  45. pinMode(ENA, INPUT);
  46. pinMode(ENB, INPUT);
  47. //-------------------------------------------------------------
  48. //IR sensor
  49. pinMode(senRPin, INPUT);
  50. pinMode(senLPin, INPUT);
  51. pinMode(senBPin, INPUT);
  52. //-------------------------------------------------------------
  53. //Ultasonic sensor:
  54. pinMode(trigPin, OUTPUT);
  55. pinMode(echoPin, INPUT);
  56.  
  57. //The robot will stop 5 sec befor moving
  58. stopM();
  59. delay(5000);
  60. //Start moving front to initiate attack
  61. front(70);
  62. }
  63.  
  64. void loop() {
  65. //IR sensor
  66. //Reading the ir sensors states
  67. senRState = digitalRead(senRPin);
  68. senLState = digitalRead(senLPin);
  69. senBState = digitalRead(senBPin);
  70. Serial.print(senBState);
  71. Serial.print(senLState);
  72. Serial.println(senRState);
  73.  
  74. //Ultra sensor
  75. // Triggering the Ultrasonic Wave :
  76. digitalWrite(trigPin, LOW);
  77. delayMicroseconds(2);
  78. digitalWrite(trigPin, HIGH);
  79. delayMicroseconds(10);
  80. digitalWrite(trigPin, LOW);
  81.  
  82. //Sensing Ultrasonic Wave on echo Pin and calculating the duration
  83. duration = pulseIn(echoPin, HIGH);
  84. distance = duration / 58.2;
  85. Serial.println(distance);
  86.  
  87. //Check IR Sensor
  88. //If right&left sensor is seeing [White] -> [FIGHT]
  89. if (senRState == 0 && senLState == 0 && senBState == 0) {
  90. fight();
  91. }
  92. //If right sensor is seeing [Black], left sensor is seeing [White] -> [move Back and to the left]
  93. else if (senRState == 1 && senLState == 0 && senBState == 0) {
  94. back(60);//86
  95. delay(850);
  96. left();
  97. delay(300);
  98. }
  99. //If right sensor is seeing [White], left sensor is seeing [Black] -> [move Back and to the right]
  100. else if (senRState == 0 && senLState == 1 && senBState == 0) {
  101. back(70);
  102. delay(850);
  103. right( );
  104. delay(300);
  105. }
  106. //If right&left sensor is seeing [Black] -> [move Back and to the right]
  107. else if (senRState == 1 && senLState == 1 && senBState == 0) {
  108. back(70);//90
  109. delay(850);
  110. right();
  111. delay(300);
  112. }
  113. //If back sensor is seeing [Black] -> [move front and to the right]
  114. else{
  115. front(60);//90
  116. delay(300);
  117. right();
  118. delay(300);
  119. }
  120. }
  121.  
  122. //---------------------------------function-------------------------
  123. void stopM() {
  124. analogWrite(ENA, 0);
  125. analogWrite(ENB, 0);
  126. digitalWrite(IN1, 0);
  127. digitalWrite(IN2, 0);
  128. digitalWrite(IN3, 0);
  129. digitalWrite(IN4, 0);
  130. }
  131. void front(int s) {
  132. analogWrite(ENA, s);
  133. analogWrite(ENB, s);
  134. digitalWrite(IN1, 1);
  135. digitalWrite(IN2, 0);
  136. digitalWrite(IN3, 0);
  137. digitalWrite(IN4, 1);
  138. }
  139. void back(int t) {
  140. analogWrite(ENA, t);
  141. analogWrite(ENB, t);
  142. digitalWrite(IN1, 0);
  143. digitalWrite(IN2, 1);
  144. digitalWrite(IN3, 1);
  145. digitalWrite(IN4, 0);
  146. }
  147. void right() {
  148. digitalWrite(IN1, 0);
  149. digitalWrite(IN2, 1);
  150. digitalWrite(IN3, 0);
  151. digitalWrite(IN4, 1);
  152. analogWrite(ENA, 60);
  153. analogWrite(ENB, 60);
  154. }
  155. void left() {
  156. digitalWrite(IN1, 1);
  157. digitalWrite(IN2, 0);
  158. digitalWrite(IN3, 1);
  159. digitalWrite(IN4, 0);
  160. analogWrite(ENA, 60);
  161. analogWrite(ENB, 60);
  162. }
  163.  
  164. // To attacking the object with a faster speed
  165. void FRONTattack() {
  166. if (millis() - previousMillis2 >= 500) {
  167. previousMillis2 = millis();
  168. front(85);
  169. }
  170. }
  171.  
  172. // Looking or searching around to find any object
  173. void Seaching()
  174. {
  175. //First event in searching mode is moving front for an interval
  176. if (millis() - previousMillis1 >= event_intervalF && frontFlag == 0 && rightFlag == 0) {
  177. Serial.println(millis() - previousMillis1);
  178. previousMillis1 = millis(); //update millis value
  179. front(80);
  180. frontFlag = 1;
  181. Serial.println("front ------ frontFlag = 1");
  182. }
  183.  
  184. //Second event in searching mode is moving right for an interval
  185. else if (millis() - previousMillis1 >= event_intervalR && frontFlag == 1 && rightFlag == 0) {
  186. Serial.println(millis() - previousMillis1);
  187. previousMillis1 = millis(); //update millis value
  188. right();
  189. rightFlag = 1;
  190. Serial.println("right ------ rightFlag = 1");
  191. }
  192.  
  193. //Third event in searching mode is moving left for an interval
  194. else if (millis() - previousMillis1 >= event_intervalR && frontFlag == 1 && rightFlag == 1) {
  195. Serial.println(millis() - previousMillis1);
  196. previousMillis1 = millis(); //update millis value
  197. left();
  198. frontFlag = 0;
  199. rightFlag = 0;
  200. Serial.println("left ------ Flags = 1");
  201.  
  202. }
  203. }
  204.  
  205. //To either go search for an object or attacking that object
  206. void fight() {
  207. if (distance <= maximumRange) {
  208. FRONTattack();
  209. frontFlag = 0;
  210. rightFlag = 0;
  211. }
  212.  
  213. // If object is not in range of attack start Searching:
  214. else {
  215. Seaching();
  216. }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment