Skip to main content
Added query about doing 1023 analogWrite calls.
Source Link
Nick Gammon
  • 38.9k
  • 13
  • 70
  • 126
  for(int i=0;i<1023;i++)
  {
  analogWrite(which, i);  
  }
  for(int i=0;i<1023;i++)
  {
  analogWrite(which, i);  
  }
// To turn the robot left side
void turnLeft()
{
  stopmotor(M2);
  stopmotor(M3);
  delay(5000);
  Runmotor(M2);
  Runmotor(M3); 
}
// To turn the robot left side
void turnLeft()
{
  stopmotor(M2);
  stopmotor(M3);
  delay(5000);
  Runmotor(M2);
  Runmotor(M3); 
}
// To turn the robot left side
void turnLeft()
{
  stopmotor(M1);
  stopmotor(M4);
  delay(5000);
  Runmotor(M2);
  Runmotor(M3); 
}
// To turn the robot left side
void turnLeft()
{
  stopmotor(M1);
  stopmotor(M4);
  delay(5000);
  Runmotor(M2);
  Runmotor(M3); 
}
// To turn the robot left side
void turnLeft()
{
  stopmotor(M1);
  stopmotor(M4);
  Runmotor(M2);
  Runmotor(M3); 
}
// To turn the robot left side
void turnLeft()
{
  stopmotor(M1);
  stopmotor(M4);
  Runmotor(M2);
  Runmotor(M3); 
}
const int NUMBER_OF_MOTORS = 4;
const int motorPins [NUMBER_OF_MOTORS] = { 3, 4, 6, 9 };
const int NUMBER_OF_MOTORS = 4;
const int motorPins [NUMBER_OF_MOTORS] = { 3, 4, 6, 9 };
// Motors Initilization

for (int i = 0; i < NUMBER_OF_MOTORS; i++)
  pinMode (motorPins [i], OUTPUT);
// Motors Initilization

for (int i = 0; i < NUMBER_OF_MOTORS; i++)
  pinMode (motorPins [i], OUTPUT);
for (int i = 0; i < NUMBER_OF_MOTORS; i++)
  stopmotor (i);
for (int i = 0; i < NUMBER_OF_MOTORS; i++)
  stopmotor (i);
// Stop Motor
void stopmotor(int which)
{
  digitalWrite(motorPins [which],LOW);
}
// Stop Motor
void stopmotor(int which)
{
  digitalWrite(motorPins [which],LOW);
}

(Edited to add)

I also don't see what this achieves:

void Runmotor(int which)
{
  for(int i=0;i<1023;i++)
  {
 analogWrite(which, i);  
  }
}

Apart from the fact that you can't go up to 1023, if you stop at 255, the time taken to do all those analogWrite is just over 2 ms. Why bother? Why not just do this:

void Runmotor(int which)
{
  digitalWrite (which, HIGH);
}
  for(int i=0;i<1023;i++)
  {
  analogWrite(which, i);  
  }
// To turn the robot left side
void turnLeft()
{
  stopmotor(M2);
  stopmotor(M3);
  delay(5000);
  Runmotor(M2);
  Runmotor(M3); 
}
// To turn the robot left side
void turnLeft()
{
  stopmotor(M1);
  stopmotor(M4);
  delay(5000);
  Runmotor(M2);
  Runmotor(M3); 
}
// To turn the robot left side
void turnLeft()
{
  stopmotor(M1);
  stopmotor(M4);
  Runmotor(M2);
  Runmotor(M3); 
}
const int NUMBER_OF_MOTORS = 4;
const int motorPins [NUMBER_OF_MOTORS] = { 3, 4, 6, 9 };
// Motors Initilization

for (int i = 0; i < NUMBER_OF_MOTORS; i++)
  pinMode (motorPins [i], OUTPUT);
for (int i = 0; i < NUMBER_OF_MOTORS; i++)
  stopmotor (i);
// Stop Motor
void stopmotor(int which)
{
  digitalWrite(motorPins [which],LOW);
}
  for(int i=0;i<1023;i++)
  {
  analogWrite(which, i);  
  }
// To turn the robot left side
void turnLeft()
{
  stopmotor(M2);
  stopmotor(M3);
  delay(5000);
  Runmotor(M2);
  Runmotor(M3); 
}
// To turn the robot left side
void turnLeft()
{
  stopmotor(M1);
  stopmotor(M4);
  delay(5000);
  Runmotor(M2);
  Runmotor(M3); 
}
// To turn the robot left side
void turnLeft()
{
  stopmotor(M1);
  stopmotor(M4);
  Runmotor(M2);
  Runmotor(M3); 
}
const int NUMBER_OF_MOTORS = 4;
const int motorPins [NUMBER_OF_MOTORS] = { 3, 4, 6, 9 };
// Motors Initilization

for (int i = 0; i < NUMBER_OF_MOTORS; i++)
  pinMode (motorPins [i], OUTPUT);
for (int i = 0; i < NUMBER_OF_MOTORS; i++)
  stopmotor (i);
// Stop Motor
void stopmotor(int which)
{
  digitalWrite(motorPins [which],LOW);
}

(Edited to add)

I also don't see what this achieves:

void Runmotor(int which)
{
  for(int i=0;i<1023;i++)
  {
 analogWrite(which, i);  
  }
}

Apart from the fact that you can't go up to 1023, if you stop at 255, the time taken to do all those analogWrite is just over 2 ms. Why bother? Why not just do this:

void Runmotor(int which)
{
  digitalWrite (which, HIGH);
}
Source Link
Nick Gammon
  • 38.9k
  • 13
  • 70
  • 126

I think this needs a major rework. First here:

  for(int i=0;i<1023;i++)
  {
  analogWrite(which, i);  
  }

The maximum you can analogWrite is 255, so change 1023 to 255.

Next, get rid of the delay() calls.


Let's look at turnLeft :

// To turn the robot left side
void turnLeft()
{
  stopmotor(M2);
  stopmotor(M3);
  delay(5000);
  Runmotor(M2);
  Runmotor(M3); 
}

Why stop the motor and then start it again? Do you mean:

// To turn the robot left side
void turnLeft()
{
  stopmotor(M1);
  stopmotor(M4);
  delay(5000);
  Runmotor(M2);
  Runmotor(M3); 
}

I don't see what the delay achieves anyway. Get rid of it:

// To turn the robot left side
void turnLeft()
{
  stopmotor(M1);
  stopmotor(M4);
  Runmotor(M2);
  Runmotor(M3); 
}

In fact, I don't see what any of the delay() calls achieve. Try removing them.


See my post How to do multiple things at once ... like cook bacon and eggs for tips on doing multiple things without using delay().


Also consider using arrays. Instead of M1, M2, M3, M4, make an array:

const int NUMBER_OF_MOTORS = 4;
const int motorPins [NUMBER_OF_MOTORS] = { 3, 4, 6, 9 };

Now it is easier to set them all to output:

// Motors Initilization

for (int i = 0; i < NUMBER_OF_MOTORS; i++)
  pinMode (motorPins [i], OUTPUT);

It is also easier to start or stop all of them:

for (int i = 0; i < NUMBER_OF_MOTORS; i++)
  stopmotor (i);

Where stopmotor is now:

// Stop Motor
void stopmotor(int which)
{
  digitalWrite(motorPins [which],LOW);
}