int AIA = 5; int AIB = 6; int BIA = 11; int BIB =10; int trigPin = 12; int echoPin = 13; long previousMillis=0; long interval=1000; char val; void setup() { pinMode(AIA, OUTPUT); pinMode(AIB, OUTPUT); pinMode(BIA, OUTPUT); pinMode(BIB, OUTPUT); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); Serial.begin(9600); } // Fordward action void go_forward() { analogWrite(AIA, 0); analogWrite(AIB, 200); analogWrite(BIA, 0); analogWrite(BIB, 200); } // Stop Forward action void stop_go_forward() { analogWrite(AIA, 0); analogWrite(AIB, 0); analogWrite(BIA, 0); analogWrite(BIB, 0); } // Reverse action void go_reverse() { analogWrite(AIA, 200); analogWrite(AIB, 0); analogWrite(BIA, 200); analogWrite(BIB, 0); } // Stop Reverse action void stop_go_reverse() { analogWrite(AIA, 0); analogWrite(AIB, 0); analogWrite(BIA, 0); analogWrite(BIB, 0); } // Left action void go_left() { analogWrite(AIA, 0); analogWrite(AIB, 150); analogWrite(BIA, 0); analogWrite(BIB, 0); } // Right action void go_right() { analogWrite(AIA, 0); analogWrite(AIB, 0); analogWrite(BIA, 0); analogWrite(BIB, 150); } // Stop turn action void stop_turn() { analogWrite(AIA, 0); analogWrite(AIB, 0); analogWrite(BIA, 0); analogWrite(BIB, 0); } // Stop car void stop_car() { analogWrite(AIA, 0); analogWrite(AIB, 0); analogWrite(BIA, 0); analogWrite(BIB, 0); } void performCommand() { if (Serial.available()) { val = Serial.read(); } if (val == 'f') { // Forward go_forward(); } else if (val == 'z') { // Stop Forward stop_go_forward(); } else if (val == 'b') { // Backward go_reverse(); } else if (val == 'y') { // Stop Backward stop_go_reverse(); } else if (val == 'l') { // Right go_right(); } else if (val == 'r') { // Left go_left(); } else if (val == 'v') { // Stop Turn stop_turn(); } else if (val == 's') { // Stop stop_car(); } } void medition(){ long distance, duration; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; Serial.print(distance); } void loop() { performCommand(); unsigned long currentMillis= millis(); if (currentMillis - previousMillis > interval){ previousMillis=currentMillis; medition(); } }