Advertisement
Jim421616

robot_L293N

May 7th, 2018
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.77 KB | None | 0 0
  1. #include <Servo.h>          //Servo motor library. This is standard library
  2. #include <NewPing.h>        //Ultrasonic sensor function library. You must install this library
  3.  
  4. //L298N control pins
  5. const int LeftMotorForward = 7;
  6. const int LeftMotorBackward = 6;
  7. const int RightMotorForward = 4;
  8. const int RightMotorBackward = 5;
  9.  
  10. //sensor pins
  11. #define trig_pin A1 //analog input 1
  12. #define echo_pin A2 //analog input 2
  13.  
  14. #define maximum_distance 200
  15. boolean goesForward = false;
  16. int distance = 100;
  17.  
  18. NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
  19. Servo servo_motor; //servo name
  20.  
  21.  
  22. void setup(){
  23.   Serial.begin(9600);
  24.   pinMode(RightMotorForward, OUTPUT);
  25.   pinMode(LeftMotorForward, OUTPUT);
  26.   pinMode(LeftMotorBackward, OUTPUT);
  27.   pinMode(RightMotorBackward, OUTPUT);
  28.  
  29.   servo_motor.attach(10); //servo pin
  30.  
  31.   servo_motor.write(115);
  32.   delay(2000);
  33.   distance = readPing();
  34.   delay(100);
  35.   distance = readPing();
  36.   delay(100);
  37.   distance = readPing();
  38.   delay(100);
  39.   distance = readPing();
  40.   delay(100);
  41.   Serial.println("Setup complete");
  42. }
  43.  
  44. void loop(){
  45.  
  46.   int distanceRight = 0;
  47.   int distanceLeft = 0;
  48.   delay(50);
  49.  
  50.   if (distance <= 20){
  51.     Serial.println("Stop\n");
  52.     moveStop();
  53.     delay(300);
  54.     moveBackward();
  55.     delay(400);
  56.     moveStop();
  57.     delay(300);
  58.     distanceRight = lookRight();
  59.     delay(300);
  60.     distanceLeft = lookLeft();
  61.     delay(300);
  62.  
  63.     if (distance >= distanceLeft){
  64.       turnRight();
  65.       moveStop();
  66.     }
  67.     else{
  68.       turnLeft();
  69.       moveStop();
  70.     }
  71.   }
  72.   else{
  73.     moveForward();
  74.   }
  75.     distance = readPing();
  76. }
  77.  
  78. int lookRight(){
  79.   Serial.println("Looking right\n");  
  80.   servo_motor.write(50);
  81.   delay(500);
  82.   int distance = readPing();
  83.   delay(100);
  84.   servo_motor.write(115);
  85.   return distance;
  86. }
  87.  
  88. int lookLeft(){
  89.   Serial.println("Looking left\n");
  90.   servo_motor.write(170);
  91.   delay(500);
  92.   int distance = readPing();
  93.   delay(100);
  94.   servo_motor.write(115);
  95.   return distance;
  96.   delay(100);
  97. }
  98.  
  99. int readPing(){
  100.   Serial.println("Distance: ");
  101.   delay(70);
  102.   int cm = sonar.ping_cm();
  103.   if (cm==0){
  104.     cm=250;
  105.   }
  106.   Serial.print(cm);
  107.   return cm;
  108. }
  109.  
  110. void moveStop(){
  111.   Serial.println("Stopping\n");
  112.   digitalWrite(RightMotorForward, LOW);
  113.   digitalWrite(LeftMotorForward, LOW);
  114.   digitalWrite(RightMotorBackward, LOW);
  115.   digitalWrite(LeftMotorBackward, LOW);
  116. }
  117.  
  118. void moveForward(){
  119.  
  120.   if(!goesForward){
  121.  
  122.     goesForward=true;
  123.     Serial.println("Going forward");
  124.     digitalWrite(LeftMotorForward, HIGH);
  125.     digitalWrite(RightMotorForward, HIGH);
  126.  
  127.     digitalWrite(LeftMotorBackward, LOW);
  128.     digitalWrite(RightMotorBackward, LOW);
  129.   }
  130. }
  131.  
  132. void moveBackward(){
  133.  
  134.   goesForward=false;
  135.   Serial.println("Going backward");
  136.   digitalWrite(LeftMotorBackward, HIGH);
  137.   digitalWrite(RightMotorBackward, HIGH);
  138.  
  139.   digitalWrite(LeftMotorForward, LOW);
  140.   digitalWrite(RightMotorForward, LOW);
  141.  
  142. }
  143.  
  144. void turnRight(){
  145.   Serial.println("Turning right");
  146.   digitalWrite(LeftMotorForward, HIGH);
  147.   digitalWrite(RightMotorBackward, HIGH);
  148.  
  149.   digitalWrite(LeftMotorBackward, LOW);
  150.   digitalWrite(RightMotorForward, LOW);
  151.  
  152.   delay(500);
  153.  
  154.   digitalWrite(LeftMotorForward, HIGH);
  155.   digitalWrite(RightMotorForward, HIGH);
  156.  
  157.   digitalWrite(LeftMotorBackward, LOW);
  158.   digitalWrite(RightMotorBackward, LOW);
  159.  
  160.  
  161.  
  162. }
  163.  
  164. void turnLeft(){
  165.   Serial.println("Turning left");
  166.   digitalWrite(LeftMotorBackward, HIGH);
  167.   digitalWrite(RightMotorForward, HIGH);
  168.  
  169.   digitalWrite(LeftMotorForward, LOW);
  170.   digitalWrite(RightMotorBackward, LOW);
  171.  
  172.   delay(500);
  173.  
  174.   digitalWrite(LeftMotorForward, HIGH);
  175.   digitalWrite(RightMotorForward, HIGH);
  176.  
  177.   digitalWrite(LeftMotorBackward, LOW);
  178.   digitalWrite(RightMotorBackward, LOW);
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement