Arduino Uno

Hey! I am trying to do an object following robot. For this i use:

  • arduino uno
  • 3 x IR Sharp rangefinder for reading the distance (proximity sensors)
  • 1 servo for changing one of the IR position
  • 2 motors.
    Well i have some problems on 2 things
    One of them is regarding the motors - The robot will go only left or right but no straight. I cannot make the both wheels to run at the same time. And the other is colliding the object, how can i make the robot to keep a certain distance of the object and not to collide with the object.
    Well the colliding is not a must have but i need to make it work fast. To follow an object correctly. Well here is my code, if you have any solutions or any questions please take your time to write an replay.

Thanks in advance!

#include <Servo.h> 
 
 Servo servo;              
 int pos = 0;
 long sensors[]={0, 0, 0};
 int a[]={0, 0, 0};//senzori
 int n,m;
 int proportional=0;
 int integral=0;
 int derivative;
 int read_value, power_difference;
 float last_proportional=0;
 int led = 13;

void setup()
{
  Serial.begin(9600);
  servo.attach(11); 
  pinMode(led, OUTPUT);
}


void loop()
{
  //LED
  digitalWrite(led, HIGH);   
  delay(60);               
  digitalWrite(led, LOW);    
  delay(100);
  digitalWrite (led, HIGH);
  delay(10);
  digitalWrite(led, LOW); 
  delay (30);
  
  //Sensors Read
  for(int i=0; i<3;i++)
  {
    sensors[i]=analogRead(i);
     if (sensors[1]>=480)
      {
        a[1]=1;
        n=0;
        m=1000;
      }
    else
      a[1]=0;
      
    if (sensors[0]>=200)
      {
        a[0]=1;
        n=1;
        m=0;
      }
    else
      a[0]=0;
   
    if (sensors[2]>=480)
      {
        a[2]=1;
        n=2;
        m=2000;
      }
    else
      a[2]=0;
    
    if((a[0]==0) && (a[1]==0) && (a[2]==0))
      n=0;
    Serial.print(a[i]);
    Serial.print( " " );
    

  }
  Serial.print(n);
  Serial.println( "" );
  //unghiurile posibile pt n==i
  if(n==1)
  {
    servo.write(60);   
  }
  else if(n==2)
  {
    servo.write(120);   
  }
  else if(n==0)
  {
    servo.write(90);   
  }
  
  proportional = m - 1000;
  int derivative = proportional - last_proportional;
  integral += proportional;
 
  // Ultima pozitie a senzorului
  last_proportional = proportional;
  power_difference = proportional/4.67 + integral/5000 + derivative*3/4;
  
 
  read_value=power_difference;
  
  Serial.print( m );
   Serial.print( " " );
   Serial.print( proportional );
   Serial.print( " " );
   Serial.print( power_difference );
   Serial.print( " " );
   Serial.print( read_value );
   Serial.print( " " );
   
   //motor();
  delay(50);
  if(read_value > 240)
         read_value = 240;
      if(read_value < -240)
         read_value = -240;

      if(read_value > 0.0)//Viraj dreapta
      {
         if(read_value>240){
               Serial.print( "3" );
          Serial.println( " ");            
            analogWrite(6, read_value-242);}
         else{
               Serial.print( "4" );
          Serial.println( " ");   
            analogWrite(6, 242-read_value);}

        
         analogWrite(3, 240);
      }
      if(read_value <= 0.0)//Viraj stanga
      {
         if(read_value<-240){
               Serial.print( "1" );
          Serial.println( " ");   
           
            analogWrite(3, -(240+read_value));}
         else
         {
               Serial.print( "2" );
          Serial.println( " ");   
            analogWrite(3, (240+read_value));}

         
         analogWrite(6, 242);

  
      }
}

Thanks in advance for your time!

You provided no clues as to which pins the motors are connected to.

You should have used Tools + Auto Format before posting that mess.

Thanks for your answer.

I did used an auto format (ctrl + T). Here are the pins: Sensors are on Pin 0, 1 and 2; The servo is on pin 11; And for the motors i use an driver, i will attach a photo.

I don’t understand very well the code for the reasons that PaulS point, and because the comments are in your native language. But I don’t see the code for the robot run forward.

I believe that this piece of code makes the robot turn right:

      if(read_value > 0.0)//Viraj dreapta
      {
         if(read_value>240){
               Serial.print( "3" );
          Serial.println( " ");            
            analogWrite(6, read_value-242);}
         else{
               Serial.print( "4" );
          Serial.println( " ");   
            analogWrite(6, 242-read_value);}

        
         analogWrite(3, 240);
      }

And this piece of code makes it turn left:

      if(read_value <= 0.0)//Viraj stanga
      {
         if(read_value<-240){
               Serial.print( "1" );
          Serial.println( " ");   
           
            analogWrite(3, -(240+read_value));}
         else
         {
               Serial.print( "2" );
          Serial.println( " ");   
            analogWrite(3, (240+read_value));}
         analogWrite(6, 242);
      }

If is like this, where is the code to run forward?

Regards.

EDIT: I believe that PaulS don’t mean: “Send us a photo”! What he means was “at what pin o Arduino is connected the driver of the motor of the left wheel, and the right, (etc., etc., etc.)”.

Sorry about that. If you mean the PWN pins are: 3,5,6 and 9 and i use 6 for left and 3 for right

And what about the other question?

Where is the code to run forward?

I don't know PID that much, i must admit i need more documentation, but i don't have the time required to do that. So that's why i am posting here. And maybe you are right, this PID i took it from the supplier of the Driver, i just did some minor changes. I dont fully understand it.