safwan092

Sumo-Robot-2-Sensors

Feb 18th, 2018
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.84 KB | None | 0 0
  1.  
  2. /*
  3.    Connections:
  4.    --------------------------------------
  5.    sensor1Pin ---> Pin 3  on Arduino Uno
  6.    sensor2Pin ---> Pin 4  on Arduino Uno
  7.    echoPin    ---> Pin 11 on Arduino Uno
  8.    trigPin    ---> Pin 12 on Arduino Uno
  9.    LEDPin     ---> Pin 13 on Arduino Uno
  10.    IN1        ---> Pin 5  on Arduino Uno
  11.    IN2        ---> Pin 6  on Arduino Uno
  12.    IN3        ---> Pin 7  on Arduino Uno
  13.    IN4        ---> Pin 8  on Arduino Uno
  14.    ENA        ---> Pin 9  on Arduino Uno
  15.    ENB        ---> Pin 10 on Arduino Uno
  16.    ---------------------------------------
  17. */
  18.  
  19. //******************************************************************************************************************************************************\\
  20.  
  21. //--------------------------------------------------------------------------------------------------------[ Pins and Global Variables ]
  22.  
  23. #define sensor1Pin 3      // 1st Infra-Red sensor pin on the (RIGHT) <---- Very Important
  24. int sensor1State = 0;     // variable for reading sensor 1 status
  25.  
  26. #define sensor2Pin 4      // 2nd Infra-Red sensor pin on the (LEFT)  <---- Very Important
  27. int sensor2State = 0;     // variable for reading sensor 2 status
  28.  
  29. #define echoPin 11        // Echo Pin
  30. #define trigPin 12        // Trigger Pin
  31.  
  32. int maximumRange = 20;    // Maximum range to start attacking
  33.  
  34. long duration, distance;  // Variables to calculate distance
  35.  
  36.  
  37. #define LEDPin 13         // Onboard LED
  38.  
  39. // Motor Driver Board Pins
  40. int IN1 = 5;
  41. int IN2 = 6;
  42. int IN3 = 7;
  43. int IN4 = 8;
  44. int ENA = 9;
  45. int ENB = 10;
  46.  
  47. //---------------------------------------------------------------------------------------------------------/End of [ Pins and Global Variables ]
  48.  
  49. //******************************************************************************************************************************************************\\
  50.  
  51. //--------------------------------------------------------------------------------------------------------[ Setup Function ]
  52.  
  53. void setup() {
  54.  
  55.   // Delay of 5000 milli-seconds = 5 seconds
  56.   delay(5000);
  57.  
  58.   // Declare motor driver pins as OUTPUT
  59.   for (int i = 5; i < 11; i ++)
  60.   {
  61.     pinMode(i, OUTPUT);
  62.   }
  63.  
  64.   // Declare Ultrasonic sensor pins as OUTPUT and INPUT
  65.   pinMode(trigPin, OUTPUT);
  66.   pinMode(echoPin, INPUT);
  67.  
  68.   // Declare Infra-Red Sensor pins as INPUT
  69.   pinMode(sensor1Pin, INPUT);
  70.   pinMode(sensor2Pin, INPUT);
  71.  
  72.   // Declare internal LED on pin 13 as OUTPUT
  73.   pinMode(LEDPin, OUTPUT);
  74.  
  75.  
  76.   // Moving car forward to initiate attack
  77.   front();
  78.  
  79.   // This delay controls the speed of the car when going in any direction above this delay
  80.   delay(300);
  81.  
  82. }
  83.  
  84. //---------------------------------------------------------------------------------------------------------/End of [ Setup Function ]
  85.  
  86. //******************************************************************************************************************************************************\\
  87.  
  88. //--------------------------------------------------------------------------------------------------------[ Loop Function ]
  89.  
  90. void loop() {
  91.  
  92.   // Reading Infra-Red Sensors and saving their status to corresponding variables
  93.   sensor1State = digitalRead(sensor1Pin);
  94.   sensor2State = digitalRead(sensor2Pin);
  95.  
  96.   // Conrolling car direction based on Infra-Red Sensors:
  97.   //--------------------------------------------------------------------------------------------------------
  98.  
  99.   // If [RIGHT] sensor is seeing [Black] and [LEFT] sensor is seeing [Black] -> [FIGHT]
  100.   if (sensor1State == HIGH && sensor2State == HIGH) {
  101.     digitalWrite(LEDPin, HIGH);
  102.     fight();
  103.   }
  104.  
  105.   // If [RIGHT] sensor is seeing [Black] and [LEFT] sensor is seeing [White] -> [move Back and to the Right]
  106.   else if (sensor1State == HIGH && sensor2State == LOW) {
  107.     digitalWrite(LEDPin, LOW);
  108.     back();
  109.     delay(200);
  110.     right();
  111.     delay(500);
  112.   }
  113.  
  114.   // If [RIGHT] sensor is seeing [White] and [LEFT] sensor is seeing [Black] -> [move Back and to the Left]
  115.   else if (sensor1State == LOW && sensor2State == HIGH) {
  116.     digitalWrite(LEDPin, LOW);
  117.     back();
  118.     delay(200);
  119.     left();
  120.     delay(500);
  121.   }
  122.  
  123.   // If [RIGHT] sensor is seeing [White] and [LEFT] sensor is seeing [White] -> [move Back and to the Right]
  124.   else if (sensor1State == LOW && sensor2State == LOW) {
  125.     digitalWrite(LEDPin, LOW);
  126.     back();
  127.     delay(500);
  128.     right();
  129.     delay(500);
  130.   }
  131.   //--------------------------------------------------------------------------------------------------------
  132. }//end of loop
  133.  
  134. //---------------------------------------------------------------------------------------------------------/End of [ Loop Function ]
  135.  
  136. //******************************************************************************************************************************************************\\
  137.  
  138. //--------------------------------------------------------------------------------------------------------[ Car Directions Functions ]
  139.  
  140. // Stopping [RIGH] motors and [LEFT] motors
  141. void stopM() {
  142.   analogWrite(ENA, 0); // Setting speed of Motors on the LEFT
  143.   analogWrite(ENB, 0); // Setting speed of Motors on the RIGH
  144. }
  145.  
  146. // Moving [RIGH] motors [forward] and [LEFT] motors [forward]
  147. void front() {
  148.   digitalWrite(IN1, 0);
  149.   digitalWrite(IN2, 1);
  150.   digitalWrite(IN3, 0);
  151.   digitalWrite(IN4, 1);
  152.   analogWrite(ENA, 150); // Setting speed of Motors on the LEFT
  153.   analogWrite(ENB, 150); // Setting speed of Motors on the RIGH
  154. }
  155.  
  156. // Moving [RIGH] motors [backwards] and [LEFT] motors [backwards]
  157. void back() {
  158.   digitalWrite(IN1, 1);
  159.   digitalWrite(IN2, 0);
  160.   digitalWrite(IN3, 1);
  161.   digitalWrite(IN4, 0);
  162.   analogWrite(ENA, 150); // Setting speed of Motors on the LEFT
  163.   analogWrite(ENB, 150); // Setting speed of Motors on the RIGH
  164. }
  165.  
  166. // Moving [LEFT] motors [forward] and [RIGHT] motors [backwards]
  167. void right() {
  168.   digitalWrite(IN1, 1);
  169.   digitalWrite(IN2, 0);
  170.   digitalWrite(IN3, 0);
  171.   digitalWrite(IN4, 1);
  172.   analogWrite(ENA, 255); // Setting speed of Motors on the LEFT
  173.   analogWrite(ENB, 255); // Setting speed of Motors on the RIGH
  174. }
  175.  
  176. // Moving [RIGH] motors [forward] and [LEFT] motors [backwards]
  177. void left() {
  178.   digitalWrite(IN1, 0);
  179.   digitalWrite(IN2, 1);
  180.   digitalWrite(IN3, 1);
  181.   digitalWrite(IN4, 0);
  182.   analogWrite(ENA, 255); // Setting speed of Motors on the LEFT
  183.   analogWrite(ENB, 255); // Setting speed of Motors on the RIGH
  184. }
  185.  
  186. //---------------------------------------------------------------------------------------------------------/End of [ Car Directions Functions ]
  187.  
  188. //******************************************************************************************************************************************************\\
  189.  
  190. //--------------------------------------------------------------------------------------------------------[ Attack Function ]
  191.  
  192. void fight() {
  193.  
  194.   // Triggering the Ultrasonic Wave :
  195.   digitalWrite(trigPin, LOW);//     |
  196.   delayMicroseconds(2);//           |
  197.   digitalWrite(trigPin, HIGH);//    |
  198.   delayMicroseconds(10);//          |
  199.   digitalWrite(trigPin, LOW);//     |
  200.   //---------------------------------
  201.  
  202.   //Sensing Ultrasonic Wave on echo Pin and callculating the duration
  203.   duration = pulseIn(echoPin, HIGH);
  204.  
  205.   //Calculate the distance (in cm) based on the speed of sound.
  206.   distance = duration / 58.2;
  207.  
  208.   // If object in range of attack start Attacking:
  209.   if (distance <= maximumRange) {
  210.     digitalWrite(LEDPin, HIGH);
  211.     front();
  212.     delay(200);
  213.   }
  214.  
  215.   // If object is not in range of attack start Searching:
  216.   else {
  217.     digitalWrite(LEDPin, LOW);
  218.     right();
  219.     delay(500);
  220.   }
  221.  
  222. }//end of fight function
  223.  
  224. //---------------------------------------------------------------------------------------------------------/End of [ Attack Function ]
  225.  
  226. //******************************************************************************************************************************************************\\
Advertisement
Add Comment
Please, Sign In to add comment