Untitled
unknown
csharp
a year ago
1.4 kB
3
Indexable
Never
#define pingPin 2 #define echoPin 3 long beforeCm = 0; void setup() { Serial.begin(9600); pinMode(pingPin,OUTPUT); pinMode(echoPin,INPUT); pinMode(12,OUTPUT); pinMode(A3,OUTPUT); } void loop() { long duration, cm, diference; digitalWrite(12, LOW); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(10); digitalWrite(pingPin, LOW); duration = pulseIn(echoPin, HIGH); cm = microsecondsToCentimeters(duration); if (beforeCm != 0) { if (beforeCm > cm) { //distancia almentou diference = beforeCm - cm; if (diference > 4) { for (int i = 0; i < 6; i++) { //repetir o código 5 vezes analogWrite(A3,440); delay(300); analogWrite(A3,0); delay(300); } } } else { //distancia diminuiu diference = cm - beforeCm; if (diference > 4) { for (int i = 0; i < 6; i++) { //repetir o código 5 vezes analogWrite(A3,523); delay(100); analogWrite(A3,0); delay(100); } } } } beforeCm = cm; analogWrite(A3,0); } long microsecondsToCentimeters(long microseconds) { return microseconds / 29 / 2; }