Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sensor
- const int trig = 3;
- const int echo = 2;
- int distance, u_seconds;
- // Motor 1
- const int motorPin1 = 5; // Pin 15 of L293
- const int motorPin2 = 6; // Pin 10 of L293
- // Motor 2
- const int motorPin3 = 7; // Pin 7 of L293
- const int motorPin4 = 4; // Pin 2 of L293
- void setup()
- {
- // Sensor
- Serial.begin(9600);
- pinMode(trig, OUTPUT);
- pinMode(echo, INPUT);
- // Motors
- pinMode(motorPin1, OUTPUT);
- pinMode(motorPin2, OUTPUT);
- pinMode(motorPin3, OUTPUT);
- pinMode(motorPin4, OUTPUT);
- /* Motor Control - Motor 1: motorPin1, motorpin2
- Motor 2: motorpin3, motorpin4
- */
- }
- void loop()
- {
- digitalWrite(trig, LOW); // Trig reset
- delayMicroseconds(2);
- digitalWrite(trig, HIGH);
- delayMicroseconds(10);
- digitalWrite(trig, LOW);
- u_seconds = pulseIn(echo, HIGH); // Receiving US signal in time interval
- distance = u_seconds * 0.034 / 2; // Distance between sensor and object
- if (distance < 15)
- {
- digitalWrite(motorPin1, HIGH); // Go back
- digitalWrite(motorPin2, LOW);
- digitalWrite(motorPin3, HIGH);
- digitalWrite(motorPin4, LOW);
- delay(400);
- digitalWrite(motorPin1, HIGH); // Go left
- digitalWrite(motorPin2, LOW);
- digitalWrite(motorPin3, LOW);
- digitalWrite(motorPin4, HIGH);
- delay(100);
- }
- else
- {
- digitalWrite(motorPin1, LOW); // Turn ON motors
- digitalWrite(motorPin2, HIGH);
- digitalWrite(motorPin3, LOW);
- digitalWrite(motorPin4, HIGH);
- }
- Serial.print(distance);
- Serial.println(" cm");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement