4 dc motors and 2 servos

hi all

i have a project running at the moment building a 4wd rover using arduino duemilanove,dfrobot i/o board v5 and a dagu 4 motor controller board.

my problem is i dont seem to be able to work out how to run 4 dc motors and 2 servos(pan and tilt) on one board i believe i will run into timer issues.

any help and guidence would be very gratefully recieved

thanks

paul

A link to your controller board would be useful.

hi thanks for the reply

here is the link to my motor controller http://robosavvy.com/store/product_info.php/products_id/1573

hope this helps

thanks
paul

I don't see why you should not be able to run four motors with this board they are just H-bridges. It will not help with the servos though.
As to timing issues what makes you think this? The board will allow you to control the direction of the motor and if you apply a PWM signal to one of the two line into the H-bridge will also allow you to control the speed.
Can you be more specific about what your problem is.

hi mike

my issue is the servos.

the motor side works perfectly any speed any direction no issue but the servos wont work they centre on startup then stay locked in place i thought it might be a timer issue but being a novice not to sure?? here is my sketch if that helps

#include <Servo.h>

int minPulse1     =  0;      // minimum servo position
int maxPulse1     =  170;  // maximum servo position
int turnRate1     =  10;    // servo turn rate increment (larger value, faster rate)
int minPulse2     =  0;     // minimum servo position
int maxPulse2     =  180; // maximum servo position
int turnRate2     =  10;   // servo turn rate increment (larger value, faster rate)
int buttonPin     = 26;    // pin that the trigger will be connected to

int centerServo1;
int centerServo2;
int pulseWidth1;	    // servo pulse width
int pulseWidth2;	    // servo pulse width 

//motor control

int EN1 = 9;     //speed control
int EN2 = 11;   //speed control
int EN3 = 6;    //speed control
int EN4 = 10;  //speed control
int IN1 = 3;    //direction
int IN2 = 5;   //direction
int IN3 = 7;   //direction
int IN4 = 2;   //direction

void Motor1(int pwm, boolean reverse)
        {
          analogWrite(EN1,pwm); //set pwm control, 0 for stop, and 255 for maximum speed
         if(reverse)
         { 
          digitalWrite(IN1,HIGH);  
         }
        else
        {
          digitalWrite(IN1,LOW);    
         }
        }  
        
void Motor2(int pwm, boolean reverse)
        {
          analogWrite(EN2,pwm);
         if(reverse)
         { 
          digitalWrite(IN2,HIGH);    
         }
        else
        {
          digitalWrite(IN2,LOW);    
         }
        }  
void Motor3(int pwm, boolean reverse)
        {
          analogWrite(EN3,pwm); //set pwm control, 0 for stop, and 255 for maximum speed
         if(reverse)
         { 
          digitalWrite(IN3,HIGH);    
         }
        else
        {
          digitalWrite(IN3,LOW);    
         }
        }
void Motor4(int pwm, boolean reverse)
        {
          analogWrite(EN4,pwm); //set pwm control, 0 for stop, and 255 for maximum speed
         if(reverse)
         { 
          digitalWrite(IN4,HIGH);    
         }
        else
        {
          digitalWrite(IN4,LOW);    
         }
        }

Servo servo1;
Servo servo2;

void setup() {
  pinMode(buttonPin, OUTPUT);
  servo1.attach(22);
  servo2.attach(24);
  centerServo1 = maxPulse1 - ((maxPulse1 - minPulse1)/2);
  centerServo2 = maxPulse2 - ((maxPulse2 - minPulse2)/2);
  pulseWidth1 =  centerServo1;
  pulseWidth2 =  centerServo2;
  Serial.begin(9600);	// opens serial port, sets data rate to 9600 bps
  Serial.println("	Arduino Serial Servo Control");
  Serial.println("Press j, l, i, or k to move, spacebar to center, and f to fire");
  Serial.println();
  
  int i;
   
    for(i=2;i<=11;i++)       //Motor Shield pin settings
    pinMode(i, OUTPUT);  //set pin 2,3,5,6,7,9,10,11 to output mode

    //Serial.begin(9600);
}

void loop() {

  // check for serial input
  if (Serial.available() > 0) {

    int data = Serial.read();	         // read the incoming byte:
    digitalWrite(buttonPin, LOW);  // turn the pin off on any incoming data
    switch(data)
    {
    case 'j' :  
      pulseWidth1 = pulseWidth1 - turnRate1;  
      break;
    case 'l' :  
      pulseWidth1 = pulseWidth1 + turnRate1;  
      break ;
    case ' ' :  
      pulseWidth1 = pulseWidth2 = centerServo1;  
      break;
    case 'i' :  
      pulseWidth2 = pulseWidth2 - turnRate1;  
      break;
    case 'k' :  
      pulseWidth2 = pulseWidth2 + turnRate1;  
      break ;
    case 'f' :  
      digitalWrite(buttonPin, HIGH); 
      delay (1000); 
      digitalWrite(buttonPin, LOW); 
      break;

    }
    // stop servo pulse at min and max
    if (pulseWidth1 > maxPulse1) { 
      pulseWidth1 = maxPulse1; 
    }
    if (pulseWidth1 < minPulse1) { 
      pulseWidth1 = minPulse1; 
    }

    // stop servo pulse at min and max
    if (pulseWidth2 > maxPulse2) { 
      pulseWidth2 = maxPulse2; 
    }
    if (pulseWidth2 < minPulse2) { 
      pulseWidth2 = minPulse2; 
    }

    servo1.write(pulseWidth1);
    servo2.write(pulseWidth2);

    // print pulseWidth back to the Serial Monitor (uncomment to debug)
    Serial.print("Servo 1: ");
    Serial.print(pulseWidth1);
    Serial.print(" Servo 2: ");
    Serial.print(pulseWidth2);
    Serial.println("degrees");
    
    int x,delay_en;
  char val;
  while(1)
  {
    val = Serial.read();
    if(val!=-1)
       {
          switch(val)
           {
             case 'w'://Move ahead
                        Motor1(100,true);  //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        Motor2(100,false);  //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        Motor3(100,true); //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        Motor4(100,false); //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                         break;
             case 'x'://move back
                        Motor1(100,false); //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        Motor2(100,true); //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        Motor3(100,false);  //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        Motor4(100,true);  //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                         break;
             case 'a'://turn left
                        Motor1(100,true);  //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        Motor2(100,true); //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        Motor3(100,false);  //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        Motor4(100,false); //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                         break;       
             case 'd'://turn right
                        Motor1(100,false); //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        Motor2(100,false);  //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        Motor3(100,true); //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        Motor4(100,true);  //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
                        break;   
               case 's'://stop
                        Motor1(0,false);
                        Motor2(0,false);
                        Motor3(0,false);
                        Motor4(0,false);
                         break;
                                  
           }     
        
       }
           
  }                           
}
  }

Try replacing:-
int data = Serial.read();
with
char data = Serial.read();

hi mike

thanks for the advice but sadly no go although pressing j or k etc it did move once and showed on the console as having moved but locks up and only a reset will unlock it =(

If you're powering the servos from the Arduino's 5V output then you're most likely causing the Arduino to reset due to the power drain. Most of the Arduino boards are only designed to supply around 800ma of current and a single "standard" servo will use ~1000ma at stall or ~250ma under no load.

hi mate

yes i do have a dedicated 6v power supply to the servos, the board itself has never reset though i have to push the reset button to free the servos after they lock up.

Grumpy_Mike:
Try replacing:-
int data = Serial.read();
with
char data = Serial.read();

How does that change anything?

Here is your problem:

while(1)

The first char you read from serial may allow you to act on the servos, after that you're looping forever looking for motor commands.

thank you so much wildbill you are a start (paul sends wildbill a crate of beer)

ok once you pointed out the error of my ways i rewrote the sketch doing away with the wait command it all works well appart from intermitent servo jitter any ideas how i might minimise this

thanks again mike and wildbill for taking the time out to help a novice like me

paul

ps my new sketch below:-

#include <Servo.h>

  int minPulse1     =  0;    // minimum servo position
  int maxPulse1     =  170;  // maximum servo position
  int turnRate1     =  10;   // servo turn rate increment (larger value, faster rate)
  int minPulse2     =  0;    // minimum servo position
  int maxPulse2     =  180;  // maximum servo position
  int turnRate2     =  10;   // servo turn rate increment (larger value, faster rate)
  //int buttonPin     =  26;    // pin that the trigger will be connected to

     /** The Arduino will calculate these values for you **/

                // connect apc220 after sketch upload to control wirelessly through serial monitor

  int EN1 = 3;  //speed control
  int EN2 = 5;  //speed control
  int EN3 = 6;  //speed control
  int EN4 = 9;  //speed control
  int IN1 = 2;  //direction
  int IN2 = 4;  //direction
  int IN3 = 7;  //direction
  int IN4 = 8;  //direction

  void Motor1(int pwm, boolean reverse)
{
 analogWrite(EN1,pwm);    //set pwm control, 0 for stop, and 255 for maximum speed
 if(reverse)
  { 
    digitalWrite(IN1,HIGH);  
  }
  else
  {
    digitalWrite(IN1,LOW);    
  }
}  

  void Motor2(int pwm, boolean reverse)
{
  analogWrite(EN2,pwm);
  if(reverse)
  { 
    digitalWrite(IN2,HIGH);    
  }
  else
  {
    digitalWrite(IN2,LOW);    
  }
}  
  void Motor3(int pwm, boolean reverse)
{
  analogWrite(EN3,pwm);      //set pwm control, 0 for stop, and 255 for maximum speed
  if(reverse)
  { 
    digitalWrite(IN3,HIGH);    
  }
  else
  {
    digitalWrite(IN3,LOW);    
  }
}
  void Motor4(int pwm, boolean reverse)
{
  analogWrite(EN4,pwm);     //set pwm control, 0 for stop, and 255 for maximum speed
  if(reverse)
  { 
    digitalWrite(IN4,HIGH);    
  }
  else
  {
    digitalWrite(IN4,LOW);    
  }
}

  Servo servo1;
  Servo servo2;                              
  int centerServo1;
  int centerServo2;
  int pulseWidth1;
  int pulseWidth2;        // servo pulse width
  
  void setup() {

  //pinMode(buttonPin, OUTPUT);


  servo1.attach(10);
  servo2.attach(11);

  centerServo1 = maxPulse1 - ((maxPulse1 - minPulse1)/2);
  centerServo2 = maxPulse2 - ((maxPulse2 - minPulse2)/2);
  pulseWidth1 =  centerServo1;
  pulseWidth2 =  centerServo2;



  Serial.begin(9600);	// opens serial port, sets data rate to 9600 bps
  Serial.println("Arduino Serial DC Motor Control");
  Serial.println("Press a, d, w, or x to move, s to stop");
  Serial.println("Arduino Serial Servo Control");
  Serial.println("Press j, l, i, or k to move, spacebar to center, and f to fire");
  Serial.println();

  int i;
  for(i=2;i<=9;i++)   //Motor Shield pin settings
    pinMode(i, OUTPUT);  //set pin 3,4,5,6,7,8,9, to output mode


}

  void loop() {

  // check for serial input
  if (Serial.available() > 0) {

    char data = Serial.read();	 // read the incoming byte:
    //digitalWrite(buttonPin, LOW);  // turn the pin off on any incoming data
    switch(data)
    
    {
    case 'w':  //Move ahead
          Motor1(150,false);  //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
          Motor2(150,true);   //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
          Motor3(150,false);  //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
          Motor4(150,true);   //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
          break;
        case 'x':  //move back
          Motor1(150,true);  //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
          Motor2(150,false); //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
          Motor3(150,true);  //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
          Motor4(150,false); //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
          break;
        case 'a':  //turn left
          Motor1(250,true);   //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
          Motor2(250,false);  //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
          Motor3(250,false);  //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
          Motor4(250,true);   //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
          break;       
       
       case 'd':  //turn right
          Motor1(250,false); //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
          Motor2(250,true);  //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
          Motor3(250,true);  //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
          Motor4(250,false); //adjust speed here 0 to 255 /adjust direction true=fowards false=reverse
         break;   
       case 's':  //stop
          Motor1(0,false);
          Motor2(0,false);
          Motor3(0,false);
          Motor4(0,false);
         break;



    //{
    case 'j' :  
      pulseWidth1 = pulseWidth1 - turnRate1;  
      break;
    case 'l' :  
      pulseWidth1 = pulseWidth1 + turnRate1;  
      break ;
    case ' ' :  
      pulseWidth1 = pulseWidth2 = centerServo1;  
      break;
    case 'i' :  
      pulseWidth2 = pulseWidth2 - turnRate1;  
      break;
    case 'k' :  
      pulseWidth2 = pulseWidth2 + turnRate1;  
      break ;
    case 'f' :  
      //digitalWrite(buttonPin, HIGH); 
      delay (1000); 
      //digitalWrite(buttonPin, LOW); 
      break;

    }
    // stop servo pulse at min and max
    if (pulseWidth1 > maxPulse1) { 
        pulseWidth1 = maxPulse1; 
    }
    if (pulseWidth1 < minPulse1) { 
        pulseWidth1 = minPulse1; 
    }

    // stop servo pulse at min and max
    if (pulseWidth2 > maxPulse2) { 
        pulseWidth2 = maxPulse2; 
    } 
    if (pulseWidth2 < minPulse2) { 
        pulseWidth2 = minPulse2; 
    }

    servo1.write(pulseWidth1);
    servo2.write(pulseWidth2);

    // print pulseWidth back to the Serial Monitor (uncomment to debug)
    Serial.print("Servo 1: ");
    Serial.print(pulseWidth1);
    Serial.print(" Servo 2: ");
    Serial.print(pulseWidth2);
    Serial.println("degrees");




   
        {
      
        }     

      }

    }

all sorted and working great thanks all for the help

very much appreciated

thanks

paul