Advertisement
safwan092

Sumo-Robot-2-Sensors-only-motor-movement

Feb 19th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.53 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. front();
  93. delay(500);
  94. back();
  95. delay(500);
  96. left();
  97. delay(500);
  98. right();
  99. delay(500);
  100. stopM();
  101. delay(5000);
  102. }//end of loop
  103.  
  104. //---------------------------------------------------------------------------------------------------------/End of [ Loop Function ]
  105.  
  106. //******************************************************************************************************************************************************\\
  107.  
  108. //--------------------------------------------------------------------------------------------------------[ Car Directions Functions ]
  109.  
  110. // Stopping [RIGH] motors and [LEFT] motors
  111. void stopM() {
  112.   analogWrite(ENA, 0); // Setting speed of Motors on the LEFT
  113.   analogWrite(ENB, 0); // Setting speed of Motors on the RIGH
  114. }
  115.  
  116. // Moving [RIGH] motors [forward] and [LEFT] motors [forward]
  117. void front() {
  118.   digitalWrite(IN1, 0);
  119.   digitalWrite(IN2, 1);
  120.   digitalWrite(IN3, 0);
  121.   digitalWrite(IN4, 1);
  122.   analogWrite(ENA, 150); // Setting speed of Motors on the LEFT
  123.   analogWrite(ENB, 150); // Setting speed of Motors on the RIGH
  124. }
  125.  
  126. // Moving [RIGH] motors [backwards] and [LEFT] motors [backwards]
  127. void back() {
  128.   digitalWrite(IN1, 1);
  129.   digitalWrite(IN2, 0);
  130.   digitalWrite(IN3, 1);
  131.   digitalWrite(IN4, 0);
  132.   analogWrite(ENA, 150); // Setting speed of Motors on the LEFT
  133.   analogWrite(ENB, 150); // Setting speed of Motors on the RIGH
  134. }
  135.  
  136. // Moving [LEFT] motors [forward] and [RIGHT] motors [backwards]
  137. void right() {
  138.   digitalWrite(IN1, 1);
  139.   digitalWrite(IN2, 0);
  140.   digitalWrite(IN3, 0);
  141.   digitalWrite(IN4, 1);
  142.   analogWrite(ENA, 255); // Setting speed of Motors on the LEFT
  143.   analogWrite(ENB, 255); // Setting speed of Motors on the RIGH
  144. }
  145.  
  146. // Moving [RIGH] motors [forward] and [LEFT] motors [backwards]
  147. void left() {
  148.   digitalWrite(IN1, 0);
  149.   digitalWrite(IN2, 1);
  150.   digitalWrite(IN3, 1);
  151.   digitalWrite(IN4, 0);
  152.   analogWrite(ENA, 255); // Setting speed of Motors on the LEFT
  153.   analogWrite(ENB, 255); // Setting speed of Motors on the RIGH
  154. }
  155.  
  156. //---------------------------------------------------------------------------------------------------------/End of [ Car Directions Functions ]
  157.  
  158. //******************************************************************************************************************************************************\\
  159.  
  160. //--------------------------------------------------------------------------------------------------------[ Attack Function ]
  161.  
  162. void fight() {
  163.  
  164.   // Triggering the Ultrasonic Wave :
  165.   digitalWrite(trigPin, LOW);//     |
  166.   delayMicroseconds(2);//           |
  167.   digitalWrite(trigPin, HIGH);//    |
  168.   delayMicroseconds(10);//          |
  169.   digitalWrite(trigPin, LOW);//     |
  170.   //---------------------------------
  171.  
  172.   //Sensing Ultrasonic Wave on echo Pin and callculating the duration
  173.   duration = pulseIn(echoPin, HIGH);
  174.  
  175.   //Calculate the distance (in cm) based on the speed of sound.
  176.   distance = duration / 58.2;
  177.  
  178.   // If object in range of attack start Attacking:
  179.   if (distance <= maximumRange) {
  180.     digitalWrite(LEDPin, HIGH);
  181.     front();
  182.     delay(200);
  183.   }
  184.  
  185.   // If object is not in range of attack start Searching:
  186.   else {
  187.     digitalWrite(LEDPin, LOW);
  188.     right();
  189.     delay(500);
  190.   }
  191.  
  192. }//end of fight function
  193.  
  194. //---------------------------------------------------------------------------------------------------------/End of [ Attack Function ]
  195.  
  196. //******************************************************************************************************************************************************\\
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement