accuracy with motor speed

I am making a line follower.
my hardware is working properly. my code is compiled correctly. I am using this hardware

  1. Arduino UNO
  2. L293D motor driver ic
  3. D.c. motor [12v, 100rpm] http://robokits.co.in/shop/index.php?main_page=product_info&cPath=2&products_id=48
  4. 7 sensor array. http://robokits.co.in/shop/index.php?main_page=product_info&cPath=11&products_id=43

PROBLEM: my sensors are detecting line correctly. but my robot does not follow line because the d.c. motors run on there full speed. the right and left turns of both motors respond to the line but due to its maximum speed the robot moves forward and can not rotate on TURNS even sensors and code respond to this.

I want to minimise my motor speed so that it can follow line.
here is my code also i am attaching file.

// Left Motor Controls

define Lp 5 // ip B

define Ln 3 // ip A

define El 10

// Right Motor Controls

define Rp 6 // ip A

define Rn 9 // ip B

define Er 11

// Grid Sensors // holding the bot with gripper oriented outwards

define Rs A2

define Ms A3

define Ls A4

define RRs A1

define LLs A5

void setup()
{ // put your setup code here, to run once:
Serial.begin(9600);
pinMode (Lp, OUTPUT);
pinMode (Ln, OUTPUT);
pinMode (El, OUTPUT);
pinMode (Rp, OUTPUT);
pinMode (Rn, OUTPUT);
pinMode (Er, OUTPUT);

digitalWrite (El,HIGH);
digitalWrite (Er,HIGH);

pinMode (Rs, INPUT);
pinMode (Ms, INPUT);
pinMode (Ls, INPUT);
pinMode (RRs, INPUT);
pinMode (LLs, INPUT);
}

void loop() {
// put your code here to run for ever
if ((!digitalRead(Ls)) && (!digitalRead(Ms)) && (!digitalRead(Rs))) // if all white // s3, s2, s1 on white
{ MotorControl(1,1);
Serial.println(“forward”);
}
else if ((!digitalRead(Ls)) && (!digitalRead(Ms)) && (digitalRead(Rs))) // if Right on black // S1 out
{ MotorControl(0,1); Serial.println(“left”); }
else if ((!digitalRead(Ls)) && (digitalRead(Ms)) && (digitalRead(Rs))) // if Right & Middle on black // S1 & s2 out
{ MotorControl(0,1); Serial.println(“left sharp”); }
else if ((digitalRead(Ls)) && (!digitalRead(Ms)) && (!digitalRead(Rs))) // if Left on black // s2 out
{ MotorControl(1,0); Serial.println(“right”); }
else if ((digitalRead(Ls)) && (digitalRead(Ms)) && (!digitalRead(Rs))) // if Left & Middle on black // S3 & s2 out
{ MotorControl(1,0); Serial.println(“right sharp”); }
}

void MotorControl(int driveL, int driveR)
{
switch (driveL) {

case 0: // lft STOP
digitalWrite (Ln,LOW);
digitalWrite (Lp,LOW);
break;

case 1: // lft FORWARD
digitalWrite (Ln,HIGH);
digitalWrite (Lp,LOW);
break;

case 2: // lft REVERSE
digitalWrite (Ln,LOW);
digitalWrite (Lp,HIGH);
break;

default:break;
}

switch (driveR) {

case 0: // rgt STOP
digitalWrite (Rn,LOW);
digitalWrite (Rp,LOW);
break;

case 1: // rgt FORWARD
digitalWrite (Rn,HIGH);
digitalWrite (Rp,LOW);
break;

case 2: // rgt REVERSE
digitalWrite (Rn,LOW);
digitalWrite (Rp,HIGH);
break;

default:break;
}
}

white_line_follow.ino (2.49 KB)

I want to minimise my motor speed so that it can follow line.

That would be done using analogWrite() on the speed pins, not turning them HIGH using digitalWrite().

What shield interface are you using, some have strange control, the chinese copies.

The chip you quote is standard but it can be implemented in many forms..

@paul s : I used analogWrite() but bot does not move in that case. might be i don't know the perfect format in which the motor is set speed. please provide me some code for speed control using analogWrite.

i chaged to analogWrite() the bot is not moving and sensors are working correctly. Please review this new code

[/

// Left Motor Controls
# define Lp  5    // ip B
# define Ln  3    // ip A
# define El  10    
// Right Motor Controls
# define Rp  6    // ip A
# define Rn  9   // ip B
# define Er  11

// Grid Sensors // holding the bot with gripper oriented outwards
# define Rs   A2
# define Ms   A3
# define Ls   A4
# define RRs  A1
# define LLs  A5


void setup()
{  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode (Lp, OUTPUT);
  pinMode (Ln, OUTPUT);
  pinMode (El, OUTPUT);
  pinMode (Rp, OUTPUT);
  pinMode (Rn, OUTPUT);
  pinMode (Er, OUTPUT);
  
  analogWrite (El,HIGH);
  analogWrite (Er,HIGH);
  
  pinMode (Rs, INPUT);
  pinMode (Ms, INPUT);
  pinMode (Ls, INPUT);
  pinMode (RRs, INPUT);
  pinMode (LLs, INPUT);
}
  
void loop()  {
// put your code here to run for ever
  if ((!digitalRead(Ls)) && (!digitalRead(Ms)) && (!digitalRead(Rs)))         // if all white // s3, s2, s1 on white
     {  MotorControl(1,1); 
         Serial.println("forward");   
       }
  else if ((!digitalRead(Ls)) && (!digitalRead(Ms)) && (digitalRead(Rs)))    // if Right on black // S1 out
     {  MotorControl(0,1); Serial.println("left"); }
  else if ((!digitalRead(Ls)) && (digitalRead(Ms)) && (digitalRead(Rs)))  // if Right & Middle on black // S1 & s2 out
     {  MotorControl(0,1); Serial.println("left sharp"); }
  else if ((digitalRead(Ls)) && (!digitalRead(Ms)) && (!digitalRead(Rs)))    // if Left on black // s2 out
     {  MotorControl(1,0); Serial.println("right"); }
  else if ((digitalRead(Ls)) && (digitalRead(Ms)) && (!digitalRead(Rs)))  // if Left & Middle on black // S3 & s2 out
     {  MotorControl(1,0); Serial.println("right sharp"); }
}

void MotorControl(int driveL, int driveR)
{
switch (driveL) {
      
    case 0:                  // lft STOP
      analogWrite (Ln,0);
      analogWrite (Lp,0);
      break;
      
    case 1:                  // lft FORWARD
      analogWrite (Ln,50);
      analogWrite (Lp,0);
      break;
      
    case 2:                  // lft REVERSE
      analogWrite (Ln,0);
      analogWrite (Lp,50);
      break;
      
    default:break;
  } 
  
switch (driveR) {
      
    case 0:                  // rgt STOP
      analogWrite (Rn,0);
      analogWrite (Rp,50);
      break;
      
    case 1:                  // rgt FORWARD
      analogWrite (Rn,50);
      analogWrite (Rp,0);
      break;
      
    case 2:                  // rgt REVERSE
      analogWrite (Rn,0);
      analogWrite (Rp,50);
      break;
      
    default:break;
  }  
}

]
analogWrite (El,HIGH);

That’s a very low speed.