int trig=12; int echo=11; float distance; float duration; void setup() { Serial.begin(9600); pinMode(13,OUTPUT); pinMode(trig,OUTPUT); pinMode(echo,INPUT); pinMode(10,OUTPUT); pinMode(9,OUTPUT); } void loop() { digitalWrite(trig,0); delayMicroseconds(2); digitalWrite(trig,1); delayMicroseconds(10); digitalWrite(trig,0); duration = pulseIn(echo,1); distance=duration/58; Serial.println(distance); if(distance< =100){ digitalWrite(13,1); delay(2000); digitalWrite(13,0); digitalWrite(10,1); delay(2000); digitalWrite(10,0); digitalWrite(9,1); delay(2000); digitalWrite(9,0); } else{ digitalWrite(13,0); digitalWrite(10,0); digitalWrite(9,0); } }
|