Advertisement
Yuvalxp8

RobotProjectNewest

Feb 15th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.28 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.  
  37. #include <NewPing.h>
  38.  
  39. #define TRIGGER_PIN 13
  40. #define ECHO_PIN 12
  41. #define MAX_DISTANCE 200
  42.  
  43. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
  44.  
  45. void setup()
  46. {
  47.  pinMode(ENA, OUTPUT);
  48.  pinMode(ENB, OUTPUT);
  49.  pinMode(IN1, OUTPUT);
  50.  pinMode(IN2, OUTPUT);
  51.  pinMode(IN3, OUTPUT);
  52.  pinMode(IN4, OUTPUT);
  53.  pinMode(leftTrigger, OUTPUT);
  54.  pinMode(leftEcho, INPUT);
  55.  pinMode(forTrigger, OUTPUT);
  56.  pinMode(forEcho, INPUT);
  57.  pinMode(rightTopTrigger, OUTPUT);
  58.  pinMode(rightTopEcho, INPUT);
  59.   pinMode(rightBottomTrigger, OUTPUT);
  60.  pinMode(rightBottomEcho, INPUT);
  61.  // Enable Motor A, Motor B: Constant Speed
  62.  digitalWrite(ENA, HIGH);
  63.  digitalWrite(ENB, HIGH);
  64.  // Serial communication
  65.  Serial.begin(9600);
  66. }
  67.  
  68. void loop()
  69. {
  70.  
  71.   getForDistance();  //**************************************************************************************************loop TODO- getFunction that gets what sonar to run
  72. }
  73.  
  74.  long microsecondsToCentimeters(long microseconds)
  75. {
  76.   return microseconds / 29 / 2;
  77. }
  78.  
  79. void MotorAB_Direction1(int milliseconds)
  80. {
  81.   analogWrite(IN1,200);
  82.    analogWrite(IN2, 100);
  83.    analogWrite(IN3, 200);
  84.    analogWrite(IN4,100);
  85.  if (milliseconds > 0)
  86.  delay(milliseconds);
  87. }
  88.  
  89. void MotorAB_Direction2(int milliseconds)
  90. {
  91.   analogWrite(IN1,100);
  92.    analogWrite(IN2, 200);
  93.    analogWrite(IN3, 100);
  94.    analogWrite(IN4,200);
  95.  if(milliseconds > 0)
  96.  delay(milliseconds);
  97. }
  98.  
  99. void MotorAB_Brake(int milliseconds)
  100. {
  101.   analogWrite(IN1,100);
  102.    analogWrite(IN2, 100);
  103.    analogWrite(IN3, 100);
  104.    analogWrite(IN4,100);
  105.  if(milliseconds > 0)
  106.  delay(milliseconds);
  107. }
  108.  
  109. void turnLeft(int milliSeconds)
  110. {
  111.    analogWrite(IN1,200);
  112.    analogWrite(IN2, 100);
  113.    analogWrite(IN3, 100);
  114.    analogWrite(IN4,200);
  115.    if(milliSeconds > 0)
  116.      delay(milliSeconds);  
  117. }
  118.  
  119. void turnRight(int milliSeconds)
  120. {
  121.    analogWrite(IN1, 100);
  122.    analogWrite(IN2, 200);
  123.    analogWrite(IN3, 200);
  124.    analogWrite(IN4, 100);
  125.    if(milliSeconds > 0)
  126.      delay(milliSeconds);  
  127. }
  128.  
  129. int getForDistance()
  130.  {
  131.   delay(50);
  132.    unsigned int uS = sonar.ping_cm();
  133.    Serial.print(uS);
  134.    Serial.println("cm");
  135.  }
  136.  
  137. int getRightTopDistance()
  138.  {
  139.   delay(50);
  140.    unsigned int uS = sonar.ping_cm();
  141.    Serial.print(uS);
  142.    Serial.println("cm");
  143.  }
  144.  
  145.  
  146. int getRightBottomDistance()
  147.  {
  148.   delay(50);
  149.    unsigned int uS = sonar.ping_cm();
  150.    Serial.print(uS);
  151.    Serial.println("cm");
  152.  }
  153.  
  154.  
  155. void stickToRight()
  156. {
  157.   while((getRightTopDistance()<30)&&(getRightBottomDistance()<30))
  158.   {
  159.     if(getRightTopDistance()<30)
  160.   {
  161.     turnLeft(100);
  162.   }
  163.   if(getRightBottomDistance()<30)
  164.   {
  165.     turnRight(100);
  166.   }
  167.   }
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement