robot arduino + HC-SR04 ultrasonic + servo

Hi I'm putting together a few days ago my arduino robot with one R3 but I have attached my code results used here hope your opinion

#include <Servo.h>

/* This is the Third version of the obstacle avoidance robot.
Improvements over version 2:
1)Scans while moving and makes small corrections vs. stoping to scan and making large corrections.
Issues:

*/
//Robot speed is 8.875 in/s
//rotation spedd is 120°/s

//Declare Servos
Servo leftservo; //Left wheel servo
Servo rightservo; //Right wheel servo
Servo scanservo; //Ping Sensor Servo
const int pingPin = 7; //Pin that the Ping sensor is attached to.
const int leftservopin = 9; //Pin number for left servo
const int rightservopin = 10; // Pin number for right servo
const int scanservopin = 6; // Pin number for scan servo
const int distancelimit = 20; //If something gets this many inched from
// the robot it stops and looks for where fo go.

int scantime = 0;
int lastscantime = 0;
char sensorpos = 'L';
long oldtime = 0;
long timesinceturnedleft = 0;
long timesinceturnedright = 0;

//Setup function. Runs once when Arduino is turned on or restarted
void setup()
{
leftservo.attach(leftservopin); //Attach left servo to its pin.
rightservo.attach(rightservopin); // Attch the right servo
scanservo.attach(scanservopin); // Attach the scan servo
Serial.begin(9600);
delay(2000); // wait two seconds
}

void loop(){
int leftdistance = 90;
int rightdistance = 90;
go(); // if nothing is wrong the go forward using go() function below.
if(millis()>oldtime+300){
if(sensorpos == 'L'){
leftdistance = ping();
sensorpos = 'R';
}
else{
rightdistance = ping();
sensorpos = 'L';
}
oldtime = millis();
}
switch (sensorpos){
case 'L':
scanservo.write(70);
break;
case 'R':
scanservo.write(110);
break;
}
if(leftdistance<distancelimit){
if (millis()<timesinceturnedleft + 500){
goback(500);
turnright(120);
}
else{
turnright(25);
}
timesinceturnedright = millis();
}
if(rightdistance<distancelimit){
if(millis()<timesinceturnedright + 500){
goback(500);
turnleft(120);
}
else{
turnleft(25);
}
timesinceturnedleft = millis();
}
}

int ping(){
long duration, inches;
//Send Pulse
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
//Read Echo
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
Serial.print("Ping: ");
Serial.println(inches);
return round(inches);
}

void go(){
leftservo.write(30);
rightservo.write(150);
}

void turnleft(float t){
leftservo.write(150);
rightservo.write(150);
t=(t/120)*1000;

Serial.print("Turning left for this many seconds: ");
Serial.println(t);
delay(t);
}

void turnright(float t){
leftservo.write(30);
rightservo.write(30);
t=(t/120)*1000;

Serial.print("Turning right for this many seconds: ");
Serial.println(t);
delay(t);
}

void goforward(int t){
leftservo.write(30);
rightservo.write(150);
delay(t);
}

void goback(int t){
leftservo.write(150);
rightservo.write(30);
delay(t);
}

void stopmotors(){
leftservo.write(90);
rightservo.write(90);
}

char scan(){
int lval, lcval, cval, rcval, rval;
int maximum = 0;
int choice;

scanservo.write(25); //Look left
delay(300);
lval = ping();
if(lval>maximum){
maximum = lval;
choice = 1;
}

scanservo.write(45); //Look left center
delay(300);
lcval = ping();
if(lcval>maximum){
maximum = lcval;
choice = 2;
}

scanservo.write(90); //Look center
delay(300);
cval = ping();
if(cval>maximum){
maximum = cval;
choice = 3;
}

scanservo.write(135); //Look right center
delay(300);
rcval = ping();
if(rcval>maximum){
maximum = rcval;
choice = 4;
}

scanservo.write(155); //Look right
delay(300);
rval = ping();
if(rval>maximum){
maximum = rval;
choice = 5;
}

if(maximum<=distancelimit){choice = 6;}

scanservo.write(88); //center scan servo
delay(300);

Serial.print("Choice: ");
Serial.println(choice);
return choice;
}

long microsecondsToInches(long microseconds){
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds){
return microseconds / 29 / 2;
}

Saludos :slight_smile:

ARDUINO_EXPLORADOR__III.ino (4.29 KB)