Advertisement
Yuvalxp8

RobotProject - NEED TO DECLARE RightBottomSonar

Jun 5th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.01 KB | None | 0 0
  1. /*##### Motor Shield (L298N) #####*/
  2.  /* To import NEWPING library go to - https://bitbucket.org/teckel12/arduino-new-ping/downloads/NewPing_v1.8.zip */
  3.  
  4. const int tDelay = 370; // without tires was 370         with tires:not turning good, do not use.
  5.                         // 896 after checks, could be 897/895/ more or less
  6.  
  7. // constanst to turn 90 degrees with delay
  8.  
  9. const int ENA = 5;      // right side
  10. const int ENB = 6;      // left side
  11. /*
  12. * IN1: HIGH; IN2: LOW --> Direction 1
  13. * IN1: LOW; IN2: HIGH --> Direction 2
  14. * IN3: HIGH; IN4: LOW --> Direction 1
  15. * IN3: LOW; IN4: HIGH --> Direction2
  16. */
  17. const int IN1 = 11;        //RIGHT SIDE
  18. const int IN2 = 10;        //RIGHT SIDE
  19. const int IN3 = 9;         //LEFT SIDE
  20. const int IN4 = 8;         //LEFT SIDE
  21.  
  22. /*const int leftTrigger=0;
  23. const int leftEcho=1;
  24.  
  25. //const int forTrigger =13;
  26. //const int forEcho=12;
  27.  
  28. const int rightTrigger = 4;        
  29. const int rightEcho =3;
  30.  
  31. const int rightBottomTrigger = 7;
  32. const int rightBottomEcho = 2;
  33. */
  34.  
  35. #include <NewPing.h>
  36.  
  37.  
  38. #define rightTrigger 14
  39. #define rightEcho 15
  40.  
  41. #define leftTrigger 19
  42. #define leftEcho 18                   // **************************** DEFINE replaces the "const int"
  43.  
  44. #define forTrigger 13
  45. #define forEcho 12
  46. #define MAX_DISTANCE 200
  47.  
  48. NewPing forSonar( forTrigger, forEcho, MAX_DISTANCE);
  49.  
  50. NewPing rightSonar( rightTrigger , rightEcho , MAX_DISTANCE);
  51.  
  52. NewPing leftSonar( leftTrigger , leftEcho, MAX_DISTANCE);
  53.  
  54.  
  55.  
  56.  int counter = 0;
  57.  
  58. void setup()
  59. {
  60.  pinMode(ENA, OUTPUT);
  61.  pinMode(ENB, OUTPUT);
  62.  pinMode(IN1, OUTPUT);
  63.  pinMode(IN2, OUTPUT);
  64.  pinMode(IN3, OUTPUT);
  65.  pinMode(IN4, OUTPUT);
  66.  
  67.  pinMode(forTrigger, OUTPUT);
  68.  pinMode(forEcho, INPUT);
  69.  pinMode(rightTrigger, OUTPUT);
  70.  pinMode(rightEcho, INPUT);
  71.   pinMode(leftTrigger, OUTPUT);
  72.  pinMode(leftEcho, INPUT);
  73.  // Enable Motor A, Motor B: Constant Speed
  74.  analogWrite(ENA, 60);                              // change back to 80
  75.  analogWrite(ENB, 60);                              // change back to 80
  76.  // Serial communication
  77.  Serial.begin(9600);
  78. }
  79.  
  80.  
  81. /* if(rightOpen())
  82.         turnRight()
  83.    elseif(frontOpen())
  84.           goForward()
  85.     else turnLeft()
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  */
  94. void loop()                       // *********** TODO --  PERFECT turn right, => right now it is too much - decrease angle, decrease curve
  95. {  
  96.  // testTurn();
  97.   //turnTillWall();
  98.  inspiration();                                          
  99. }
  100.  
  101. void uTurn()
  102. {
  103.   analogWrite(ENA, 100);
  104.   analogWrite(ENB, 100);
  105.  
  106.   digitalWrite(IN1,HIGH);
  107.   digitalWrite(IN2, LOW);
  108.   digitalWrite(IN3, HIGH);
  109.   digitalWrite(IN4,LOW);
  110.   delay(650);  
  111. }
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123. void testTurn()
  124. {
  125.   for(int i = 0; i < 4 ; i++)
  126.   {
  127.     turnRight(tDelay);
  128.     stopDrive1(500);
  129.   }
  130.   stopDrive1(10000);
  131. }
  132.  
  133. void stickToRight()
  134. {
  135.   if(getSonarDistance(rightSonar) - getSonarDistance(leftSonar) >= 3)
  136.   {
  137.     turnRight1(10);
  138.   }
  139.     if(getSonarDistance(leftSonar) - getSonarDistance(rightSonar) >= 3)
  140.   {
  141.     turnLeft1(10);
  142.   }
  143.   else
  144.   {
  145.     driveForward1(10);
  146.   }
  147.  
  148. }
  149.  
  150. long microsecondsToCentimeters(long microseconds)  //********************************************************* Function : from microSec to CM
  151. {
  152.   return microseconds / 29 / 2;
  153. }
  154.  
  155. // digitalWrietTry
  156.  
  157.  
  158. void driveForward1(int milliseconds)  //*********************************************************  Function : drive forward
  159. {
  160.  
  161.    analogWrite(ENA, 60);
  162.    analogWrite(ENB, 60);
  163.    
  164.    digitalWrite(IN1,HIGH);
  165.    digitalWrite(IN2, LOW);
  166.    digitalWrite(IN3, LOW);
  167.    digitalWrite(IN4,HIGH);
  168.  if (milliseconds > 0)
  169.  delay(milliseconds);
  170. }
  171.  
  172. void driveBackward1(int milliseconds) //********************************************************* Function : drive backward
  173. {
  174.    analogWrite(ENA, 60);
  175.    analogWrite(ENB, 60);
  176.    
  177.    digitalWrite(IN1,LOW);
  178.    digitalWrite(IN2, HIGH);
  179.    digitalWrite(IN3, HIGH);
  180.    digitalWrite(IN4,LOW);
  181.  if(milliseconds > 0)
  182.  delay(milliseconds);
  183. }
  184.  
  185. void stopDrive1(int milliseconds)  //********************************************************* Function : stop  
  186. {
  187.    digitalWrite(IN1,LOW);
  188.    digitalWrite(IN2, LOW);
  189.    digitalWrite(IN3, LOW);
  190.    digitalWrite(IN4,LOW);
  191.  if(milliseconds > 0)
  192.  delay(milliseconds);
  193. }
  194.  
  195. void turnLeft1(int milliSeconds) //********************************************************* Function : turn left
  196. {
  197.   analogWrite(ENA, 155);                              
  198.   analogWrite(ENB, 0);
  199.  
  200.   digitalWrite(IN1,HIGH);
  201.   digitalWrite(IN2, LOW);
  202.   digitalWrite(IN3, LOW);
  203.   digitalWrite(IN4,HIGH);
  204.    if(milliSeconds > 0)
  205.      delay(milliSeconds);  
  206. }
  207.  
  208.  
  209. void turnRight(int milliSeconds)
  210. {
  211.   analogWrite(ENA, 0);  //was 0                            
  212.   analogWrite(ENB, 155); //was 155
  213.  
  214.   digitalWrite(IN1,HIGH);
  215.   digitalWrite(IN2, LOW);
  216.   digitalWrite(IN3, LOW);
  217.   digitalWrite(IN4,HIGH);
  218.   if(milliSeconds > 0)
  219.     delay(milliSeconds);  
  220. }
  221. void turnRight1(int milliSeconds) //********************************************************* Function : turn right
  222. {
  223.   analogWrite(ENA, 80);                              // change back to 150
  224.   analogWrite(ENB, 80);
  225.  
  226.   digitalWrite(IN1,LOW);
  227.   digitalWrite(IN2, HIGH);
  228.   digitalWrite(IN3, LOW);
  229.   digitalWrite(IN4,HIGH);
  230.   if(milliSeconds > 0)
  231.     delay(milliSeconds);  
  232. }
  233.  
  234.  
  235. int getSonarDistance(NewPing sonar)  //********************************************************* Function : recieves a specific Sonar and returns distance
  236. {
  237.   delay(100);                                  
  238.   unsigned int distance = sonar.ping_cm();
  239.   //Serial.print(distance);
  240.   //Serial.println("cm");
  241.   if(distance !=0)
  242.   return distance;
  243.   else
  244.   return 1000; // Comes in place of the "0" so it wont disturb the loop that check "smaller than..."
  245.  
  246.  
  247.  
  248. }
  249.                                      
  250.  
  251. void funRun()          //****************************************** Function : just avoiding objcects and turns left
  252. {
  253.  
  254.   while(getSonarDistance(forSonar) > 30 || getSonarDistance(forSonar) == 0 )
  255.   {
  256.     driveForward1(50);
  257.   }
  258.   while(getSonarDistance(forSonar) < 30)
  259.    {
  260.     driveBackward1(70);
  261.     delay(50);
  262.       turnLeft1(800);
  263.    }
  264.    }
  265.  
  266. 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
  267. {
  268.  
  269.   while(getSonarDistance(forSonar) > 30 || getSonarDistance(forSonar) == 0 )
  270.   {
  271.     driveForward1(50);
  272.   }
  273.   while(getSonarDistance(forSonar) < 30)
  274.    {
  275.     if(counter==4)
  276.     {
  277.       driveBackward1(70);
  278.     delay(50);
  279.       turnRight1(800);
  280.       counter = 0;
  281.     }
  282.     else
  283.     {
  284.     driveBackward1(70);
  285.     delay(50);
  286.       turnLeft1(800);
  287.       counter++;
  288.    }
  289.    
  290. }
  291. }
  292.  
  293. void checkSpanish()
  294. {
  295.   if(getSonarDistance(forSonar) >8)
  296.   {
  297.     if(getSonarDistance(rightSonar) > 7 &&  getSonarDistance(rightSonar) <13  )
  298.     {
  299.       analogWrite(ENA , 60);    // 120
  300.       analogWrite(ENB , 90);  // 150
  301.       driveForward1(50);
  302.     }
  303.  
  304.     if(getSonarDistance(rightSonar) >= 13)
  305.     {
  306.       analogWrite(ENA , 150);  //255
  307.       analogWrite(ENB , 10);   // 60
  308.       driveForward1(100);
  309.     }
  310.  
  311.     if(getSonarDistance(rightSonar) <= 7 )
  312.     {
  313.       analogWrite(ENA , 10);    //60
  314.       analogWrite(ENB , 150);    //255
  315.       driveForward1(100);
  316.     }
  317.   }
  318.  
  319.   if( getSonarDistance(rightSonar) <=20 && getSonarDistance(rightSonar)  > 20 && getSonarDistance(forSonar) <=8)
  320.     turnRight1(700);
  321.  
  322.   if(getSonarDistance(rightSonar) > 20 && getSonarDistance(rightSonar)  > 20 && getSonarDistance(forSonar) <=8)
  323.     turnRight1(700);
  324.  
  325.   if(getSonarDistance(rightSonar) <=20 && getSonarDistance(rightSonar)  > 20 && getSonarDistance(forSonar) <=8)      
  326.     turnLeft1(800);
  327.  
  328.   if( getSonarDistance(rightSonar) <=20 && getSonarDistance(rightSonar)  <= 20 && getSonarDistance(forSonar) <=8)      
  329.     turnRight1(1200); // rotate 180
  330.  
  331. }
  332.  
  333. void inspiration()
  334. {
  335.   if(getSonarDistance(rightSonar) >25)
  336.   {
  337.       turnRight(tDelay); // 90 degrees
  338.   }
  339.   else if(getSonarDistance(forSonar) > 20)
  340.   {
  341.     driveForward1(20);       // previous version 100
  342.   }
  343.   else if(getSonarDistance(leftSonar) >30)
  344.   {
  345.     turnLeft1(tDelay);
  346.     stopDrive1(500);// 90 degrees
  347.   }
  348.   else
  349.   {
  350.    uTurn(); //tDelay*2
  351.   }
  352. }
  353.  
  354.  void turnTillWall()
  355.  {
  356.  
  357.   if(getSonarDistance(rightSonar) > 6 )
  358.   {
  359.     while(getSonarDistance(rightSonar) > 6 )
  360.     {
  361.       turnRight(105);         // 8 times = full turn (90 degrees)
  362.     }
  363.   }
  364.   else if(getSonarDistance(forSonar) > 20)
  365.   {
  366.     driveForward1(50);       // previous version 100
  367.   }
  368.   else if(getSonarDistance(leftSonar) >20)
  369.   {
  370.     turnLeft1(tDelay);
  371.     stopDrive1(500);// 90 degrees
  372.   }
  373.   else
  374.   {
  375.    turnRight(1350); //tDelay*2
  376.   }
  377.  }
  378.  /* void correctParallel()
  379.   {
  380.     for(int i = 0 ; i < 5; i++)
  381.     {
  382.       if(rightBottomSonar - toprigthSonar == i)
  383.       turnLeft(i*100);   // just a measure to turn need to check this
  384.       else if(topright - rightBottom == i)
  385.       turnRight(i*100); // same
  386.     }
  387.   }
  388. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement