Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define echoPin 7 // Echo Pin
- #define trigPin 6 // Trigger Pin
- #define motorPin 8 // Motor Pin
- long duration, distance;
- void setup()
- {
- Serial.begin(9600);
- pinMode(motorPin, OUTPUT);
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- digitalWrite(motorPin, LOW);
- }
- void loop()
- {
- US();
- }
- void US() {
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- duration = pulseIn(echoPin, HIGH);
- distance = duration / 58.2;
- Serial.println(distance);
- if (distance < 100 && distance > 50) {
- //move motor
- digitalWrite(motorPin, HIGH);
- delay(500);
- digitalWrite(motorPin, LOW);
- delay(500);
- }
- else if (distance < 50) {
- //stop motor
- digitalWrite(motorPin, HIGH);
- delay(300);
- digitalWrite(motorPin, LOW);
- delay(100);
- digitalWrite(motorPin, HIGH);
- delay(300);
- digitalWrite(motorPin, LOW);
- delay(300);
- }
- else {
- digitalWrite(motorPin, LOW);
- }
- delay(50);
- }
Advertisement
Add Comment
Please, Sign In to add comment