Advertisement
Yuvalxp8

RobotProject

Mar 21st, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.97 KB | None | 0 0
  1. /*##### Motor Shield (L298N) #####*/
  2.  
  3. const int ENA = 5;
  4. const int ENB = 6;
  5. /*
  6. * IN1: HIGH; IN2: LOW --> Direction 1
  7. * IN1: LOW; IN2: HIGH --> Direction 2
  8. * IN3: HIGH; IN4: LOW --> Direction 1
  9. * IN3: LOW; IN4: HIGH --> Direction2
  10. */
  11. const int IN1 = 11;
  12. const int IN2 = 10;
  13. const int IN3 = 9;
  14. const int IN4 = 8;
  15.  
  16. /*const int leftTrigger=0;
  17. const int leftEcho=1;
  18.  
  19. //const int forTrigger =13;
  20. //const int forEcho=12;
  21.  
  22. const int rightTopTrigger = 4;        
  23. const int rightTopEcho =3;
  24.  
  25. const int rightBottomTrigger = 7;
  26. const int rightBottomEcho = 2;
  27. */
  28.  
  29. #include <NewPing.h>
  30.  
  31. #define leftTrigger 0
  32. #define leftEcho 1
  33.  
  34. #define rightTopTrigger 14
  35. #define rightTopEcho 15
  36.  
  37. #define rightBottomTrigger 19
  38. #define rightBottomEcho 18                   // **************************** DEFINE replaces the "const int"
  39.  
  40. #define forTrigger 13
  41. #define forEcho 12
  42. #define MAX_DISTANCE 200
  43.  
  44. NewPing forSonar( forTrigger, forEcho, MAX_DISTANCE);
  45.  
  46. NewPing rightTopSonar( rightTopTrigger , rightTopEcho , MAX_DISTANCE);
  47.  
  48. NewPing rightBottomSonar( rightBottomTrigger , rightBottomEcho, MAX_DISTANCE);
  49.  
  50. NewPing leftSonar( leftTrigger , leftEcho , MAX_DISTANCE);
  51.  
  52.  int counter = 0;
  53.  
  54. void setup()
  55. {
  56.  pinMode(ENA, OUTPUT);
  57.  pinMode(ENB, OUTPUT);
  58.  pinMode(IN1, OUTPUT);
  59.  pinMode(IN2, OUTPUT);
  60.  pinMode(IN3, OUTPUT);
  61.  pinMode(IN4, OUTPUT);
  62.  pinMode(leftTrigger, OUTPUT);
  63.  pinMode(leftEcho, INPUT);
  64.  pinMode(forTrigger, OUTPUT);
  65.  pinMode(forEcho, INPUT);
  66.  pinMode(rightTopTrigger, OUTPUT);
  67.  pinMode(rightTopEcho, INPUT);
  68.   pinMode(rightBottomTrigger, OUTPUT);
  69.  pinMode(rightBottomEcho, INPUT);
  70.  // Enable Motor A, Motor B: Constant Speed
  71.  digitalWrite(ENA, HIGH);
  72.  digitalWrite(ENB, HIGH);
  73.  // Serial communication
  74.  Serial.begin(9600);
  75. }
  76.  
  77. void loop()
  78. {
  79. stickToRight();
  80. }
  81.  
  82. void stickToRight()
  83. {
  84.   if(getSonarDistance(rightTopSonar) - getSonarDistance(rightBottomSonar) >= 3)
  85.   {
  86.     turnRight1(10);
  87.   }
  88.     if(getSonarDistance(rightBottomSonar) - getSonarDistance(rightTopSonar) >= 3)
  89.   {
  90.     turnLeft1(10);
  91.   }
  92.   else
  93.   {
  94.     driveForward1(10);
  95.   }
  96.  
  97. }
  98.  
  99. long microsecondsToCentimeters(long microseconds)  //********************************************************* Function : from microSec to CM
  100. {
  101.   return microseconds / 29 / 2;
  102. }
  103.  
  104. // digitalWrietTry
  105. void driveForward1(int milliseconds)  //*********************************************************  Function : drive forward
  106. {
  107.   digitalWrite(IN1,HIGH);
  108.    digitalWrite(IN2, LOW);
  109.    digitalWrite(IN3, LOW);
  110.    digitalWrite(IN4,HIGH);
  111.  if (milliseconds > 0)
  112.  delay(milliseconds);
  113. }
  114.  
  115. void driveBackward1(int milliseconds) //********************************************************* Function : drive backward
  116. {
  117.    digitalWrite(IN1,LOW);
  118.    digitalWrite(IN2, HIGH);
  119.    digitalWrite(IN3, HIGH);
  120.    digitalWrite(IN4,LOW);
  121.  if(milliseconds > 0)
  122.  delay(milliseconds);
  123. }
  124.  
  125. void stopDrive1(int milliseconds)  //********************************************************* Function : stop  
  126. {
  127.    digitalWrite(IN1,LOW);
  128.    digitalWrite(IN2, LOW);
  129.    digitalWrite(IN3, LOW);
  130.    digitalWrite(IN4,LOW);
  131.  if(milliseconds > 0)
  132.  delay(milliseconds);
  133. }
  134.  
  135. void turnLeft1(int milliSeconds) //********************************************************* Function : turn left
  136. {
  137.    digitalWrite(IN1,HIGH);
  138.    digitalWrite(IN2, LOW);
  139.    digitalWrite(IN3, HIGH);
  140.    digitalWrite(IN4,LOW);
  141.    if(milliSeconds > 0)
  142.      delay(milliSeconds);  
  143. }
  144.  
  145. void turnRight1(int milliSeconds) //********************************************************* Function : turn right
  146. {
  147.    digitalWrite(IN1, LOW);
  148.    digitalWrite(IN2, HIGH);
  149.    digitalWrite(IN3, LOW);
  150.    digitalWrite(IN4, HIGH);
  151.    if(milliSeconds > 0)
  152.      delay(milliSeconds);  
  153. }
  154.  
  155.  
  156. int getSonarDistance(NewPing sonar)  //********************************************************* Function : recieves a specific Sonar and returns distance
  157. {
  158.   delay(200);                                
  159.    unsigned int uS = sonar.ping_cm();
  160.  //  Serial.print(uS);
  161.  //  Serial.println("cm");
  162.    return uS;
  163. }
  164.                                      
  165.  
  166. void funRun()          //****************************************** Function : just avoiding objcects and turns left
  167. {
  168.  
  169.   while(getSonarDistance(forSonar) > 30 || getSonarDistance(forSonar) == 0 )
  170.   {
  171.     driveForward1(50);
  172.   }
  173.   while(getSonarDistance(forSonar) < 30)
  174.    {
  175.     driveBackward1(70);
  176.     delay(50);
  177.       turnLeft1(800);
  178.    }
  179.    }
  180.  
  181. void funRun2()      // ********************* Function: same as funRun , just if turned left 4 times, it turns Right. PROBLEM not always return to its initial spot- in maze it will be ok
  182. {
  183.  
  184.   while(getSonarDistance(forSonar) > 30 || getSonarDistance(forSonar) == 0 )
  185.   {
  186.     driveForward1(50);
  187.   }
  188.   while(getSonarDistance(forSonar) < 30)
  189.    {
  190.     if(counter==4)
  191.     {
  192.       driveBackward1(70);
  193.     delay(50);
  194.       turnRight1(800);
  195.       counter = 0;
  196.     }
  197.     else
  198.     {
  199.     driveBackward1(70);
  200.     delay(50);
  201.       turnLeft1(800);
  202.       counter++;
  203.    }
  204.    }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement