Untitled

mail@pastecode.io avatar
unknown
csharp
2 years ago
1.3 kB
2
Indexable
Never
#define pingPin 2

#define echoPin 3


void setup() {
   Serial.begin(9600);
   pinMode(pingPin,OUTPUT); 
   pinMode(echoPin,INPUT);
   pinMode(12,OUTPUT);
   pinMode(A3,OUTPUT);
}

void loop() {
   long duration, cm;
   digitalWrite(12, LOW);
   
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);    
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(pingPin, LOW);

  
   duration = pulseIn(echoPin, HIGH);
   
   cm = microsecondsToCentimeters(duration);
   
   /* Serial.print(cm);
    Serial.print("cm");
    Serial.println();
    delay(100);
    
    debug
    */

   if (cm > 70 && < 30) {
     //detactar buracos
     analogWrite(A3,523);
     delay(100); 
     analogWrite(A3,0); 
     delay(100);
   }else if (cm < 30 && cm > 20) {
     analogWrite(A3,440); 
     delay(500); 
     analogWrite(A3,0); 
     delay(500);
   } else if (cm < 20 && cm > 10) {
     analogWrite(A3,440); 
     delay(300); 
     analogWrite(A3,0); 
     delay(300);
   } else if (cm < 10 && cm > 0) {
     analogWrite(A3,440); 
     delay(100); 
     analogWrite(A3,0);
     delay(100);
   } else analogWrite(A3,0);
}

long microsecondsToCentimeters(long microseconds) {
   return microseconds / 29 / 2;
}