SHARE
TWEET

Untitled

a guest Mar 22nd, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define L1 A5 // defining the left sensors and stating the corresponding pins
  2. #define L2 A4
  3. #define L3 3
  4. #define R1 4 // defining the right sensors and stating corresponding pins
  5. #define R2 5
  6. #define R3 6
  7. #define RM_0 10 // defining right motors
  8. #define RM_1 8
  9. #define LM_0 A0 // defining left motors
  10. #define LM_1 12
  11. #define PWM_0 11 // power of the motors
  12. #define PWM_1 9
  13.  
  14. int s1l; // inputting all sensors
  15. int s2l;
  16. int s3l;
  17. int s1r;  
  18. int s2r;
  19. int s3r;
  20.  
  21. void setup()
  22. {
  23.     pinMode(L1, INPUT); // setting the sensors as inputs
  24.     pinMode(L2, INPUT);
  25.     pinMode(L3, INPUT);
  26.     pinMode(R1, INPUT);
  27.     pinMode(R2, INPUT);
  28.     pinMode(R3, INPUT);
  29.     pinMode(RM_0, OUTPUT); // setting the motors as outputs
  30.     pinMode(RM_1, OUTPUT);
  31.     pinMode(LM_0, OUTPUT);
  32.     pinMode(LM_1, OUTPUT);
  33.     Serial.begin(9600);
  34. }
  35.  
  36. //ACTIONS
  37. void botStop(){
  38.     digitalWrite(LM_0, LOW); // sets the motors to the low so that it stops
  39.     digitalWrite(LM_1, LOW);
  40.     digitalWrite(RM_0, LOW);
  41.     digitalWrite(RM_1, LOW);
  42.    
  43.     analogWrite(PWM_0, 0); // power of the motors to be 0 so that it does not move
  44.     analogWrite(PWM_1, 0);
  45. }
  46. void botGo(){
  47.     digitalWrite(LM_0, HIGH); // sets each motor to the desired value so that it can move forward
  48.     digitalWrite(LM_1, LOW);
  49.     digitalWrite(RM_0, HIGH);
  50.     digitalWrite(RM_1, LOW);
  51.    
  52.     analogWrite(PWM_0, 100); // power of the motor to be at maximum for fastest speed
  53.     analogWrite(PWM_1, 100);
  54. }
  55. void botRight(){
  56.     digitalWrite(LM_0, LOW); // sets only the right motor ot high so it is able to turn right
  57.     digitalWrite(LM_1, LOW);
  58.     digitalWrite(RM_0, HIGH);
  59.     digitalWrite(RM_1, LOW);
  60.  
  61.     analogWrite(PWM_0, 75); // desired power of the motor so that it can turn right
  62.     analogWrite(PWM_1, 150);
  63. }
  64. void botLeft(){
  65.     digitalWrite(LM_0, HIGH); // sets only the left motor to high so that it can turn left
  66.     digitalWrite(LM_1, LOW);
  67.     digitalWrite(RM_0, LOW);
  68.     digitalWrite(RM_1, LOW);
  69.  
  70.     analogWrite(PWM_0, 150); // desired power of the motor so that it can turn left
  71.     analogWrite(PWM_1, 75);
  72. }
  73. void bot360(){
  74.       digitalWrite(LM_0, LOW);
  75.       digitalWrite(LM_1, HIGH);
  76.       digitalWrite(RM_0, HIGH);
  77.       digitalWrite(RM_1, LOW);
  78.  
  79.       analogWrite(PWM_0, 100);
  80.       analogWrite(PWM_1, 100);
  81.       delay(3000);
  82. }
  83.  
  84.  
  85.  
  86. void loop()
  87. {
  88.   s1l = digitalRead(L1); // reading the inputs
  89.   s2l = digitalRead(L2);
  90.   s3l = digitalRead(L3);
  91.   s1r = digitalRead(R1);
  92.   s2r = digitalRead(R2);
  93.   s3r = digitalRead(R3);
  94.  
  95.  
  96. //statements with description of what bot is seeing
  97. //A STRAIGHT LINE AHEAD FROM LEFT TO RIGHT
  98. // if(s1l == HIGH && s2l == HIGH && s3l == HIGH && s1r == HIGH && s2r == HIGH && s3r == HIGH)
  99. //  {
  100. //   botStop();
  101. //  }
  102. //LINE IN THE DIRECTION OF TRAVEL (NO OBSTRUCTION)
  103.   if(s1l == LOW && s2l == LOW && s3l == HIGH && s1r == HIGH && s2r == LOW && s3r == LOW)// forward
  104.   {
  105.     botGo();
  106.   }
  107. //A STRAIGHT LINE AHEAD FROM RIGHT TO CENTER
  108.   if(s1l == LOW && s2l == LOW && s3l == HIGH && s1r == HIGH && s2r == HIGH && s3r == LOW)//soft right
  109.   {
  110.     botRight();
  111.   }
  112. //A STRAIGHT LINE AHEAD FROM LEFT TO CENTER
  113.   if(s1l == LOW && s2l == HIGH & s3l == HIGH && s1r == HIGH && s2r == LOW && s3r == LOW) //soft left
  114.   {
  115.     botLeft();
  116.   }
  117.  
  118.   if(s1l == HIGH && s2l == HIGH && s3l == HIGH && s1r == HIGH && s2r == LOW && s3r == LOW) //hard left
  119.   {
  120.     botLeft();
  121.   }
  122.  
  123.   if(s1l == LOW && s2l == LOW && s3l == HIGH && s1r == HIGH && s2r == HIGH && s3r == HIGH && (millis() < 13000) && (millis() > 10000)) //hard right (first)
  124.   {
  125.    
  126.     bot360();   //do 360 for 3 seconds
  127.  
  128.   }
  129.  
  130.     if(s1l == LOW && s2l == LOW && s3l == HIGH && s1r == HIGH && s2r == HIGH && s3r == HIGH && (millis() > 13000) && (millis() < 13000+1000)) //hard right (second)
  131.   {
  132.     botGo();  //continue after 360
  133.   }
  134.  
  135.       if(s1l == LOW && s2l == LOW && s3l == HIGH && s1r == HIGH && s2r == HIGH && s3r == HIGH && (millis() > 13000+1000) && (millis() < 13000+10000)) //hard right (third)
  136.   {
  137.     botStop(); //5 second stop
  138.    
  139.   }
  140.  
  141.  
  142.       if(s1l == LOW && s2l == LOW && s3l == HIGH && s1r == HIGH && s2r == HIGH && s3r == HIGH && (millis() > 13000+10000) && (millis() < 13000+11000)) //hard right (fourth)
  143.   {
  144.     botGo();  //continue after stop
  145.   }
  146.  
  147.   if(s1l == HIGH && s2l == HIGH && s3l == HIGH && s1r == HIGH && s2r == HIGH && s3r == HIGH && (millis() < 10000)) //stop, instead turn left
  148.   {
  149.     botLeft();
  150.   }
  151.     if(s1l == HIGH && s2l == HIGH && s3l == HIGH && s1r == HIGH && s2r == HIGH && s3r == HIGH && (millis() > 10000)) //stop, this time stop
  152.   {
  153.     botStop();
  154.   }
  155.  
  156. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top