Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int trigPin = A1;
- const int echoPin = A2;
- int Motor1 = 13;
- int Motor2 = 12;
- int en1 = 11;
- int en2 = 10;
- int Motor3 = 9;
- int Motor4 = 8;
- void setup() {
- Serial.begin(9600);
- pinMode(Motor1,OUTPUT);
- pinMode(Motor2,OUTPUT);
- pinMode(Motor3,OUTPUT);
- pinMode(Motor4,OUTPUT);
- pinMode(en1,OUTPUT);
- pinMode(en2,OUTPUT);
- }
- void loop() {
- long duration, cm;
- pinMode(trigPin, OUTPUT);
- digitalWrite(trigPin, LOW);
- delayMicrosecond(2);
- digitalWrite(trigPin, HIGH);
- delayMicrosecond(5);
- digitalWrite(trigPin, LOW);
- pinMode(echoPin, INPUT);
- duration = pulseIn(echoPin, HIGH);
- cm = microsecondsToCentimeters(duration);
- Serial.print(cm);
- Serial.print("cm");
- serial.println();
- delay(100);
- if (cm<-40){
- digitalWrite(Motor1, LOW);
- digitalWrite(Motor2, HIGH);
- digitalWrite(Motor3, HIGH);
- digitalWrite(Motor4, LOW);
- digitalWrite(en1, 255);
- digitalWrite(en2, 255);
- }
- else {
- digitalWrite(Motor1, LOW);
- digitalWrite(Motor2, HIGH);
- digitalWrite(Motor3, LOW);
- digitalWrite(Motor4, HIGH);
- digitalWrite(en1, 255);
- digitalWrite(en2, 255);
- }
- }
- long microsecondsToCentimeters(long microseconds) {
- return microsecond / 29 / 2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement