Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define trigPin 2
- #define echoPin 3
- #define motor 4
- long duration;
- int distanceD;
- void setup() {
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- pinMode(motor, OUTPUT);
- digitalWrite(motor, 0);
- Serial.begin(9600);
- }
- void loop() {
- US();
- if (distanceD < 50) {
- digitalWrite(motor, 1);
- delay(1000);
- digitalWrite(motor, 0);
- }
- delay(100);
- }
- void US() {
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- duration = pulseIn(echoPin, HIGH);
- distanceD = duration * 0.034 / 2;
- Serial.print("Distance: ");
- Serial.print(distanceD);
- Serial.println(" cm");
- }
Advertisement
Add Comment
Please, Sign In to add comment