IkeKap

Untitled

Nov 21st, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const int AIN1 = 12;        
  2. const int AIN2 = 13;            
  3. const int PWMA = 10;            
  4.  
  5.  
  6. const int PWMB = 11;          
  7. const int BIN2 = 2;          
  8. const int BIN1 = 5;          
  9.  
  10. const int trigPinLeft=3;
  11. const int echoPinLeft=4;
  12. const int trigPinRight=6;
  13. const int echoPinRight=7;
  14. const int trigPin = 9;
  15. const int echoPin = 8;
  16. //int switchPin = 7;            
  17.  
  18. float distance = 0;
  19. float distanceRight=0;
  20. float distanceLeft=0;
  21. /********************************************************************************/
  22. void setup()
  23. {
  24.   pinMode(trigPin, OUTPUT);      
  25.   pinMode(echoPin, INPUT);      
  26.  
  27.   pinMode(AIN1, OUTPUT);
  28.   pinMode(AIN2, OUTPUT);
  29.   pinMode(PWMA, OUTPUT);
  30.  
  31.   pinMode(BIN1, OUTPUT);
  32.   pinMode(BIN2, OUTPUT);
  33.   pinMode(PWMB, OUTPUT);
  34.  
  35.   Serial.begin(9600);                      
  36. }
  37.  
  38. /********************************************************************************/
  39. void loop()
  40. {
  41.   distance = getDistance();
  42.   Serial.println(distance);
  43.     if(distance < 12){              
  44.       stop();
  45.       delay(100);
  46.       goLeft();
  47.       delay(100);
  48.       distanceLeft=getDistanceLeft();
  49.       //Serial.println(distanceLeft);
  50.       distanceRight=getDistanceRight();
  51.       //Serial.println(distanceRight);
  52.       delay(100);
  53.       while(distanceLeft<1||distanceRight<1){
  54.        
  55.         if(distanceLeft<1){
  56.           goRight();
  57.         }
  58.         else if(distanceRight<1){
  59.           goLeft();
  60.         }
  61.         distanceLeft=getDistanceLeft();
  62.        
  63.         distanceRight=getDistanceRight();
  64.        
  65.         delay(5);
  66.       }
  67.     }
  68.     else{                        
  69.       goForwards(255);
  70.     }
  71.   delay(5);                      
  72.  
  73.  //goForwards(255);
  74.  
  75. }
  76.  
  77. /********************************************************************************/
  78. void rightMotor(int motorSpeed)                    
  79. {
  80.   if (motorSpeed > 0)                                
  81.     {
  82.       digitalWrite(AIN1, HIGH);                        
  83.       digitalWrite(AIN2, LOW);                        
  84.     }
  85.   else if (motorSpeed < 0)                          
  86.     {
  87.       digitalWrite(AIN1, LOW);                          
  88.       digitalWrite(AIN2, HIGH);                        
  89.     }
  90.   else                                                
  91.     {
  92.       digitalWrite(AIN1, LOW);                          
  93.       digitalWrite(AIN2, LOW);                        
  94.     }
  95.   analogWrite(PWMA, abs(motorSpeed));                
  96. }
  97.  
  98. /********************************************************************************/
  99. void leftMotor(int motorSpeed)                        
  100. {
  101.   if (motorSpeed > 0)
  102.     {
  103.       digitalWrite(BIN1, HIGH);                        
  104.       digitalWrite(BIN2, LOW);                        
  105.     }
  106.   else if (motorSpeed < 0)                            
  107.     {
  108.       digitalWrite(BIN1, LOW);                          
  109.       digitalWrite(BIN2, HIGH);                        
  110.     }
  111.   else                                              
  112.     {
  113.       digitalWrite(BIN1, LOW);                          
  114.       digitalWrite(BIN2, LOW);                          
  115.     }
  116.   analogWrite(PWMB, abs(motorSpeed));                
  117. }
  118. /********************************************************************************/
  119. void goForwards(int speed){
  120.   rightMotor(speed);
  121.   leftMotor(speed-127);
  122. }
  123. /********************************************************************************/
  124. void goBackwards(int speed){
  125.   rightMotor(speed*-1);
  126.   leftMotor(speed*-1);
  127. }
  128. /********************************************************************************/
  129. void goRight(){
  130.   rightMotor(-155);
  131.   leftMotor(155);
  132. }
  133. /********************************************************************************/
  134. void goLeft(){
  135.   rightMotor(155);
  136.   leftMotor(-155);
  137. }
  138. /********************************************************************************/
  139. void stop(){
  140.   rightMotor(0);
  141.   leftMotor(0);
  142. }
  143. /********************************************************************************/
  144. float getDistance()
  145. {
  146.   float echoTime;                  
  147.   float calculatedDistance;        
  148.   digitalWrite(trigPin, HIGH);
  149.   delayMicroseconds(10);
  150.   digitalWrite(trigPin, LOW);
  151.  
  152.   echoTime = pulseIn(echoPin, HIGH);      
  153.   calculatedDistance = echoTime / 148.0;  
  154.   return calculatedDistance;            
  155. }
  156. /********************************************************************************/
  157. float getDistanceRight()
  158. {
  159.   float echoTimeRight;                  
  160.   float calculatedDistanceRight;        
  161.  
  162.   digitalWrite(trigPinRight, HIGH);
  163.   delayMicroseconds(10);
  164.   digitalWrite(trigPinRight, LOW);
  165.  
  166.   echoTimeRight = pulseIn(echoPinRight, HIGH);      
  167.   calculatedDistanceRight = echoTimeRight / 148.0;
  168.  
  169.   return calculatedDistanceRight;            
  170. }
  171. /********************************************************************************/
  172. float getDistanceLeft()
  173. {
  174.   float echoTimeLeft;                  
  175.   float calculatedDistanceLeft;        
  176.  
  177.   digitalWrite(trigPinLeft, HIGH);
  178.   delayMicroseconds(10);
  179.   digitalWrite(trigPinLeft, LOW);
  180.  
  181.   echoTimeLeft = pulseIn(echoPinLeft, HIGH);      
  182.   calculatedDistanceLeft = echoTimeLeft/ 148.0;
  183.  
  184.   return calculatedDistanceLeft;            
  185. }
Advertisement
Add Comment
Please, Sign In to add comment