hi guys,
i am making a robot that manourves away from obstacles, the robot move forwards and if anything was in the way it will stop, then rotates from left to right, but now when I want a basic code to work with it, I just found a code using servo motors but I only have dc motors, so please if it's possible,can you give me a basic code using dc motors. or you could just give me some changes of this code which using servo motors so it could work with dc motors.
thank you
#include <Servo.h>
int trigPin = 2;
int echoPin = 4;
long duration, cm, inches;
Servo servoLeft;
Servo servoRight;
int forward = 180; // full speed forward
int backward = 0; // full speed backward
void setup() {
servoLeft.attach(10);
servoRight.attach(9);
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
//sets both servos to full speed in the same direction
servoLeft.write(forward);
servoRight.write(backward);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(4);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// converts the time to a distance
cm = (duration / 2) / 29.1;
inches = (duration / 2) / 74;
delay(250);
//Sets object detection distance to 4.5
if (inches < 4.5) {
//stops both servos
servoLeft.write(90);
servoRight.write(90);
delay(1000);
//sets both servos to full speed in opposite direction
servoLeft.write(backward);
servoRight.write(forward);
delay(1000);
//sets only the left servo to rotate so the robot turns
servoLeft.write(forward);
delay(1500);
}//end if statement
}//ends loop