safwan092

Project-car-robot > fighting_robot_with_Ultrasonic

Apr 21st, 2021
781
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define trigPin 2
  2. #define echoPin 3
  3.  
  4. long duration;
  5. int distance;
  6.  
  7. char t;
  8.  
  9. void setup() {
  10.   pinMode(trigPin, OUTPUT);
  11.   pinMode(echoPin, INPUT);
  12.   pinMode(13, OUTPUT);  //left motors forward
  13.   pinMode(12, OUTPUT);  //left motors reverse
  14.   pinMode(11, OUTPUT);  //right motors forward
  15.   pinMode(10, OUTPUT);  //right motors reverse
  16.   pinMode(7, OUTPUT);  //Electric-Detonator
  17.   Serial.begin(9600);
  18. }
  19.  
  20. void loop() {
  21.   US();
  22.   if (distance <= 20) {
  23.     digitalWrite(13, HIGH);
  24.     digitalWrite(11, HIGH);
  25.     delay(1000);
  26.     digitalWrite(13, LOW);
  27.     digitalWrite(12, LOW);
  28.     digitalWrite(11, LOW);
  29.     digitalWrite(10, LOW);
  30.   }
  31.  
  32.   if (Serial.available()) {
  33.     t = Serial.read();
  34.     Serial.println(t);
  35.   }
  36.  
  37.   if (t == 'B') {          //move forward(all motors rotate in forward direction)
  38.     digitalWrite(13, HIGH);
  39.     digitalWrite(11, HIGH);
  40.   }
  41.  
  42.   else if (t == 'F') {    //move reverse (all motors rotate in reverse direction)
  43.     digitalWrite(12, HIGH);
  44.     digitalWrite(10, HIGH);
  45.   }
  46.  
  47.   else if (t == 'L') {    //turn right (left side motors rotate in forward direction, right side motors doesn't rotate)
  48.     digitalWrite(11, HIGH);
  49.   }
  50.  
  51.   else if (t == 'R') {    //turn left (right side motors rotate in forward direction, left side motors doesn't rotate)
  52.     digitalWrite(13, HIGH);
  53.   }
  54.  
  55.   else if (t == 'w') {  //turn Electric-Detonator on or off)
  56.     digitalWrite(7, HIGH);
  57.   }
  58.   else if (t == 'W') {
  59.     digitalWrite(7, LOW);
  60.   }
  61.  
  62.   else if (t == 'S') {    //STOP (all motors stop)
  63.     digitalWrite(13, LOW);
  64.     digitalWrite(12, LOW);
  65.     digitalWrite(11, LOW);
  66.     digitalWrite(10, LOW);
  67.   }
  68.  
  69. }// end of LOOP
  70.  
  71.  
  72. void US() {
  73.   digitalWrite(trigPin, LOW);
  74.   delayMicroseconds(5);
  75.   digitalWrite(trigPin, HIGH);
  76.   delayMicroseconds(10);
  77.   digitalWrite(trigPin, LOW);
  78.   duration = pulseIn(echoPin, HIGH);
  79.   distance = duration * 0.034 / 2;
  80.   //Serial.print("Distance = ");
  81.   //Serial.print(distance);
  82.   //Serial.println(" cm");
  83. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×