Guest User

Untitled

a guest
Dec 11th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. //#define E1 10  // Enable Pin for motor 1
  2. //#define E2 11  // Enable Pin for motor 2
  3.  
  4. #define I1  9 // Control pin 1 for motor 1
  5. #define I2 10 // Control pin 2 for motor 1
  6. #define I3 11 // Control pin 1 for motor 2
  7. #define I4 12  // Control pin 2 for motor 2
  8. int QREi1113_Pin0 = 0; //connected to analog 0
  9. int QREi1113_Pin1 = 1; //connected to analog 1
  10. #define trigPin 5
  11. #define echoPin 4
  12.  
  13. // defines variables
  14. //long duration, distance;
  15.  
  16. void setup() {
  17.     Serial.begin(9600);
  18.     pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  19.    pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  20.     
  21.     //pinMode(E1, OUTPUT);
  22.     //pinMode(E2, OUTPUT);
  23.  
  24.     pinMode(I1, OUTPUT);
  25.     pinMode(I2, OUTPUT);
  26.     pinMode(I3, OUTPUT);
  27.     pinMode(I4, OUTPUT);
  28.     
  29.     
  30. }
  31.  
  32. void loop() {
  33.   int QRE_Value = analogRead(QREi1113_Pin0);
  34. Serial.println(QRE_Value);
  35. //int QRE_Value = analogRead(QRE1113_Pin1)
  36. //Serial.println(QRE_Value1);
  37. long duration, distance;
  38.   digitalWrite(trigPin, LOW);  // Added this line
  39.   delayMicroseconds(2); // Added this line
  40.   digitalWrite(trigPin, HIGH);
  41. //  delayMicroseconds(1000); - Removed this line
  42.   delayMicroseconds(2); // Added this line
  43.   digitalWrite(trigPin, LOW);
  44.   duration = pulseIn(echoPin, HIGH);
  45.   distance = (duration/2) / 29.1;
  46.   
  47.     Serial.print(distance);
  48.     Serial.println(" cm");
  49. ROTATE(); // start roteteint QRE_Serial.print("xroma ");
  50.  
  51.  
  52.  
  53.  if (distance < 10){
  54.     Stop();
  55.     while (distance < 10 ) {
  56.     FORWARD(); 
  57.    
  58.   distance = (duration/2) / 29.1;
  59.   
  60.     Serial.print(distance);
  61.     Serial.println(" cm");
  62.   
  63.     QRE_Value = analogRead(QREi1113_Pin0);
  64.     
  65.     if ( QRE_Value > 150 ) { break;}
  66.      delayMicroseconds(2); }
  67.   }
  68.   
  69.  if (QRE_Value< 150 )  // < 650 means white line
  70.    {
  71.    Stop();
  72.     delay(20);
  73.    BACKWARD();
  74.     delay(10);
  75.    } 
  76.   
  77.  
  78. }
  79.  
  80.  
  81. //--------------------------------------------
  82. void FORWARD (){
  83.  
  84.     //analogWrite(E1, 255); // Run in half speed
  85.     //analogWrite(E2, 255); // Run in full speed
  86.  
  87.     digitalWrite(I1,HIGH );
  88.     digitalWrite(I2, LOW);
  89.     digitalWrite(I3, HIGH);
  90.     digitalWrite(I4, LOW);
  91.     Serial.println(" mprosta");
  92. }
  93.  
  94.  
  95. //--------------------------------------------
  96. void BACKWARD (){
  97.   
  98.   
  99.    // analogWrite(E1, 255); // Run in half speed
  100.     //analogWrite(E2, 255); // Run in full speed
  101.  
  102.     digitalWrite(I1, LOW);
  103.     digitalWrite(I2, HIGH);
  104.     digitalWrite(I3, LOW);
  105.     digitalWrite(I4, HIGH);
  106.     Serial.println("piso");
  107. }
  108.  
  109. //--------------------------------------------
  110. void ROTATE (){//RORATE LEFT
  111.    
  112.    // analogWrite(E1, 200); // Run in half speed
  113.    // analogWrite(E2, 200); // Run in full speed
  114.  
  115.     digitalWrite(I1, LOW);
  116.     digitalWrite(I2, HIGH);
  117.     digitalWrite(I3, HIGH);
  118.     digitalWrite(I4, LOW);
  119.     Serial.println(" stript");
  120. }
  121.  
  122.  
  123. //--------------------------------------------
  124. void Stop(){
  125.   
  126.    
  127.  
  128.     digitalWrite(I1, LOW);
  129.     digitalWrite(I2, LOW);
  130.     digitalWrite(I3, LOW);
  131.     digitalWrite(I4, LOW);
  132.   Serial.println(" stamata");
  133.   //delay(2000);
  134. }
Add Comment
Please, Sign In to add comment