Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //created by Nugroho Budi P
- #define trigPin 2
- #define echoPin 3
- int Switch = 4;
- void setup() {
- Serial.begin (9600);
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- pinMode(Switch, OUTPUT);
- }
- void loop() {
- long duration, distance;
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- duration = pulseIn(echoPin, HIGH);
- distance = (duration/2)/70;
- if (distance <= 20) {
- digitalWrite(Switch, LOW);
- delay(7000);
- digitalWrite(Switch, HIGH);
- delay(5000);
- }
- else{
- digitalWrite(Switch,HIGH);
- }
- delay(200);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement