Advertisement
safwan092

sumo-robot-final-for-736

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