Wall avoiding robot

Hi guys!! i’m building a wall avoiding robot, nothing special, but for learning how to use arduino it’s good :smiley:
As sensor i’m using a normal ultrasonic sensor and for motors i’ve built a motorshield with an l298n.
This is the code, it seems work even if the decision part is still missing, i’ll add it as soon as possible.
What do you think about? Suggestions are accepted to improve it :grin:

#include <Servo.h>
//motore A
int enA=10;
int inA1=9;
int inA2=8;

//motore B
int enB=5;
int inB1=6;
int inB2=7;

int invio=4;
int ricevi=3;
int destra, sinistra;
int i;
long c;
Servo servo; 

void setup(){
  servo.attach(12);
  pinMode(enA,OUTPUT);
  pinMode(inA1,OUTPUT);
  pinMode(inA2,OUTPUT);
  
  pinMode(enB,OUTPUT);
  pinMode(inB1,OUTPUT);
  pinMode(inB2,OUTPUT);
  
  pinMode(invio,OUTPUT);
  pinMode(ricevi,INPUT);
}

void loop(){
  servo.write(90); 
  int distanza=misura();//object's distance
  if (distanza>10){
    servo.detach();
    avanti();
  }
  
  else{ //goes back until distance is at least 15cm
    while(distanza<15){
      indietro();
      distanza=misura();
    }   
    ferma();//stop
    servo.attach(12);
    giradx();//look right
    delay(500);
    destra=misura();//space at right
    delay(500);  
    girasx();//look left
    delay(500);
    sinistra=misura();//space at left
    delay(500);
    servo.write(90);//came back frontal     
    delay(500);
  }
}



//this function send the signal (1° part), and work out object's distance (2°part)

long misura(){
  digitalWrite(invio,LOW);
  digitalWrite(invio,HIGH);
  delayMicroseconds(10);
  digitalWrite(invio,LOW);
  
  long tempo=pulseIn(ricevi, HIGH);
  long distanza=tempo*0.034/2;
  return distanza;
}


void giradx(){
  for(i=90; i<180; i++){
    servo.write(i);
    delayMicroseconds(10);
  }
}

void girasx(){
  for(i=180; i>1; i--){
    servo.write(i);
    delayMicroseconds(10);
  }
}

void avanti(){
  analogWrite(enA,250);
  analogWrite(enB,250);
  digitalWrite(inA1,HIGH);
  digitalWrite(inA2,LOW);
  digitalWrite(inB1,HIGH);
  digitalWrite(inB2,LOW);
}


void ferma(){
  digitalWrite(enA,LOW);
  digitalWrite(enB,LOW);
}  
 
void indietro(){
  analogWrite(enA,50);
  analogWrite(enB,50);
  digitalWrite(inA2,HIGH);
  digitalWrite(inA1,LOW);
  digitalWrite(inB2,HIGH);
  digitalWrite(inB1,LOW);
}

void dx(){
  analogWrite(enA,50);
  analogWrite(enB,50);
  digitalWrite(inA2,HIGH);
  digitalWrite(inA1,LOW);
  digitalWrite(inB1,HIGH);
  digitalWrite(inB2,LOW);
}

void sx(){
  analogWrite(enA,50);
  analogWrite(enB,50);
  digitalWrite(inA1,HIGH);
  digitalWrite(inA2,LOW);
  digitalWrite(inB2,HIGH);
  digitalWrite(inB1,LOW);
}
for(i=90; i<180; i++){
    servo.write(i);
    delayMicroseconds(10);
  }

The delay here is pretty pointless.
A servo frame is 20 milliseconds long, so on average, it takes the servo 10 milliseconds to react to a servo.write, which is one thousand times longer than your delay.

So it’s better if i remove it?

The below seems to have working code.

http://arduino.cc/forum/index.php/topic,153438.0.html

that car looks a bit cunfused :open_mouth: