Advertisement
Yuvalxp8

RobotProject

Feb 15th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.40 KB | None | 0 0
  1. /*##### Motor Shield (L298N) #####*/
  2. /*
  3. * Arduino Pin --> L298N
  4. * 5 --> ENA
  5. * 6 --> ENB
  6. * 2 --> IN1
  7. * 3 --> IN2
  8. * 4 --> IN3
  9. * 7 --> IN4
  10. */
  11. const int ENA = 5;
  12. const int ENB = 6;
  13. /*
  14. * IN1: HIGH; IN2: LOW --> Direction 1
  15. * IN1: LOW; IN2: HIGH --> Direction 2
  16. * IN3: HIGH; IN4: LOW --> Direction 1
  17. * IN3: LOW; IN4: HIGH --> Direction2
  18. */
  19. const int IN1 = 11;
  20. const int IN2 = 10;
  21. const int IN3 = 9;
  22. const int IN4 = 8;
  23.  
  24. const int leftTrigger=0;
  25. const int leftEcho=1;
  26.  
  27. const int forTrigger =2;
  28. const int forEcho=3;
  29.  
  30. const int rightTopTrigger = 4;
  31. const int rightTopEcho =5;
  32.  
  33. const int rightBottomTrigger = 7;
  34. const int rightBottomEcho = 13;
  35.  
  36. void setup()
  37. {
  38.  pinMode(ENA, OUTPUT);
  39.  pinMode(ENB, OUTPUT);
  40.  pinMode(IN1, OUTPUT);
  41.  pinMode(IN2, OUTPUT);
  42.  pinMode(IN3, OUTPUT);
  43.  pinMode(IN4, OUTPUT);
  44.  pinMode(leftTrigger, OUTPUT);
  45.  pinMode(leftEcho, INPUT);
  46.  pinMode(forTrigger, OUTPUT);
  47.  pinMode(forEcho, INPUT);
  48.  pinMode(rightTopTrigger, OUTPUT);
  49.  pinMode(rightTopEcho, INPUT);
  50.   pinMode(rightBottomTrigger, OUTPUT);
  51.  pinMode(rightBottomEcho, INPUT);
  52.  // Enable Motor A, Motor B: Constant Speed
  53.  digitalWrite(ENA, HIGH);
  54.  digitalWrite(ENB, HIGH);
  55.  // Serial communication
  56.  Serial.begin(9600);
  57. }
  58.  
  59. void loop()
  60. {
  61.   if(getForDistance()>30)
  62.   {
  63.     MotorAB_Direction1(1000);
  64.     MotorAB_Brake(1000);
  65.   }
  66.   else
  67.   {
  68.     turnLeft(1000);
  69.   }
  70. }
  71.  
  72.  
  73.  long microsecondsToCentimeters(long microseconds)
  74. {
  75.   return microseconds / 29 / 2;
  76. }
  77.  
  78. void MotorAB_Direction1(int milliseconds)
  79. {
  80.   analogWrite(IN1,200);
  81.    analogWrite(IN2, 100);
  82.    analogWrite(IN3, 200);
  83.    analogWrite(IN4,100);
  84.  if (milliseconds > 0)
  85.  delay(milliseconds);
  86. }
  87.  
  88. void MotorAB_Direction2(int milliseconds)
  89. {
  90.   analogWrite(IN1,100);
  91.    analogWrite(IN2, 200);
  92.    analogWrite(IN3, 100);
  93.    analogWrite(IN4,200);
  94.  if(milliseconds > 0)
  95.  delay(milliseconds);
  96. }
  97.  
  98. void MotorAB_Brake(int milliseconds)
  99. {
  100.   analogWrite(IN1,100);
  101.    analogWrite(IN2, 100);
  102.    analogWrite(IN3, 100);
  103.    analogWrite(IN4,100);
  104.  if(milliseconds > 0)
  105.  delay(milliseconds);
  106. }
  107.  
  108. void turnLeft(int milliSeconds)
  109. {
  110.    analogWrite(IN1,200);
  111.    analogWrite(IN2, 100);
  112.    analogWrite(IN3, 100);
  113.    analogWrite(IN4,200);
  114.    if(milliSeconds > 0)
  115.      delay(milliSeconds);  
  116. }
  117.  
  118. void turnRight(int milliSeconds)
  119. {
  120.    analogWrite(IN1, 100);
  121.    analogWrite(IN2, 200);
  122.    analogWrite(IN3, 200);
  123.    analogWrite(IN4, 100);
  124.    if(milliSeconds > 0)
  125.      delay(milliSeconds);  
  126. }
  127.  
  128.  
  129.  int getForDistance()
  130.  {
  131.   digitalWrite(forTrigger,HIGH);
  132.   int duration = pulseIn(forEcho,HIGH);
  133.   Serial.print("    duration:    ");
  134.   Serial.println(duration);
  135.  
  136.   int cmDistance = microsecondsToCentimeters(duration);
  137.   Serial.print("    jjj    ");
  138.   Serial.println(cmDistance);
  139.   return cmDistance;
  140.  }
  141.  
  142. int getRightTopDistance()
  143.  {
  144.   digitalWrite(rightTopTrigger,HIGH);
  145.   int duration = pulseIn(rightTopEcho,HIGH);
  146.   int cmDistance = microsecondsToCentimeters(duration);
  147.   return cmDistance;
  148.  }
  149.  
  150.  
  151. int getRightBottomDistance()
  152.  {
  153.   digitalWrite(rightTopTrigger,HIGH);
  154.   int duration = pulseIn(rightTopEcho,HIGH);
  155.   int cmDistance = microsecondsToCentimeters(duration);
  156.   return cmDistance;
  157.  }
  158.  
  159.  
  160. void stickToRight()
  161. {
  162.   while((getRightTopDistance()<30)&&(getRightBottomDistance()<30))
  163.   {
  164.     if(getRightTopDistance()<30)
  165.   {
  166.     turnLeft(100);
  167.   }
  168.   if(getRightBottomDistance()<30)
  169.   {
  170.     turnRight(100);
  171.   }
  172.   }
  173.  
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement