Motorshield and servo problem

Building an IR tracked vehicle with a robotic arm and when the servo is attached to pin in void setup, motor shield stops responding. Still shows lights indicating directional change, just no output, as if the brake is not being released.
take out "s1.attach(3);" and motorshield runs fine but without servo.
any sudgestions?

here is the code....

#include <Servo.h>
#include <IRremote.h>
#include <IRremoteInt.h>
int RECV_PIN = 2;
IRrecv irrecv(RECV_PIN);
decode_results results;
Servo s1;
int angle1 = 160;
int angle2 = 10;



const int one = 0x8;
const int two = 0x808;
const int three = 0x408;
const int four = 0xC08;
const int five = 0x208;
const int six = 0xA08;
const int seven = 0x608;
const int eight = 0xE08;
const int nine = 0x108;
const int zero = 0x908;
const int enter = 0xD08;
const int on = 0xED;
const int off = 0x40ED;
const int up = 0x20ED;
const int down = 0x60ED;
int pinI1=8;
int pinI2=11;
int speedpinA=9;
int pinI3=12;
int pinI4=13;
int speedpinB=10;
int speada =250;
int speadb =125;
int speadc =90;
int ledpin=7;

void setup()
{
  Serial.begin(9600);
  s1.attach(3);
  irrecv.enableIRIn(); 


  pinMode(ledpin,OUTPUT);
  pinMode(pinI1,OUTPUT);
  pinMode(pinI2,OUTPUT);
  pinMode(speedpinA,OUTPUT);
  pinMode(pinI3,OUTPUT);
  pinMode(pinI4,OUTPUT);
  pinMode(speedpinB,OUTPUT);
}



void backward()
{
  analogWrite(speedpinA,speada);
  analogWrite(speedpinB,speada);
  digitalWrite(pinI4,HIGH);
  digitalWrite(pinI3,LOW);
  digitalWrite(pinI2,LOW);
  digitalWrite(pinI1,HIGH);
}
void forward()
{
  analogWrite(speedpinA,speada);
  analogWrite(speedpinB,speada);
  digitalWrite(pinI4,LOW);
  digitalWrite(pinI3,HIGH);
  digitalWrite(pinI2,HIGH);
  digitalWrite(pinI1,LOW);
}
void backwards()
{
  analogWrite(speedpinA,speadc);
  analogWrite(speedpinB,speadc);
  digitalWrite(pinI4,HIGH);
  digitalWrite(pinI3,LOW);
  digitalWrite(pinI2,LOW);
  digitalWrite(pinI1,HIGH);
}
void forwardss()                  //SS=super slow
{
  analogWrite(speedpinA,speadc);
  analogWrite(speedpinB,speadc);
  digitalWrite(pinI4,LOW);
  digitalWrite(pinI3,HIGH);
  digitalWrite(pinI2,HIGH);
  digitalWrite(pinI1,LOW);
}

void forwards()
{
  analogWrite(speedpinA,speadb);
  analogWrite(speedpinB,speadb);
  digitalWrite(pinI4,LOW);
  digitalWrite(pinI3,HIGH);
  digitalWrite(pinI2,HIGH);
  digitalWrite(pinI1,LOW);
}
void left()
{
  analogWrite(speedpinA,speada);
  analogWrite(speedpinB,speada);
  digitalWrite(pinI4,HIGH);
  digitalWrite(pinI3,LOW);
  digitalWrite(pinI2,HIGH);
  digitalWrite(pinI1,LOW);
}
void right()
{
  analogWrite(speedpinA,speada);
  analogWrite(speedpinB,speada);
  digitalWrite(pinI4,LOW);
  digitalWrite(pinI3,HIGH);
  digitalWrite(pinI2,LOW);
  digitalWrite(pinI1,HIGH);
}
void lefts()
{
  analogWrite(speedpinA,speadb);
  analogWrite(speedpinB,speadb);
  digitalWrite(pinI4,HIGH);
  digitalWrite(pinI3,LOW);
  digitalWrite(pinI2,HIGH);
  digitalWrite(pinI1,LOW);
}
void rights()
{
  analogWrite(speedpinA,speadb);
  analogWrite(speedpinB,speadb);
  digitalWrite(pinI4,LOW);
  digitalWrite(pinI3,HIGH);
  digitalWrite(pinI2,LOW);
  digitalWrite(pinI1,HIGH);
}
void leftss()
{
  analogWrite(speedpinA,speadb);

  digitalWrite(pinI4,HIGH);
  digitalWrite(pinI3,LOW);

}
void rightss()
{

  analogWrite(speedpinB,speadb);

  digitalWrite(pinI2,LOW);
  digitalWrite(pinI1,HIGH);
}
void stop()
{
  digitalWrite(speedpinA,LOW);
  digitalWrite(speedpinB,LOW);


}


void loop() {



  if (irrecv.decode(&results)) {  


    if(results.value == one) {
      right();
      delay(200);
      stop();

    }




    if(results.value == two)  {
      forward();
      delay(1000);
      stop();

    }




    if(results.value == three) {
      left();
      delay(200);
      stop();


    }




    if(results.value == four) {
      rights();
      delay(200);
      stop();

    }




    if(results.value == five) {
      forward();
      delay(200);
      stop();

    }




    if(results.value == six) {
      lefts();
      delay(200);
      stop();


    }



    if(results.value == seven) {
      leftss();
      delay(200);
      stop();

    }




    if(results.value == eight) {
      forwardss();
      delay(200);
      stop();

    }



    if(results.value == nine) {
      rightss();
      delay(200);
      stop();

    }




    if(results.value == zero) {
      backwards();
      delay(200);
      stop();

    }



    if(results.value == enter) {
      backward();
      delay(200);
      stop();

    }




    if(results.value == on) {

      digitalWrite(ledpin,HIGH);
    }




    if(results.value == off) {

      digitalWrite(ledpin,LOW);
    }

    if(results.value == up) {

      s1.write(angle1);
      delay(100);
    }




    if(results.value == down) {

      s1.write(angle2);
      delay(100);
    }

    irrecv.resume();
  }
}

Inadequate power supply problem?

Which motor shield are you using? The Arduino shield uses pin 3!

Mark

I have the Seeed Studio and i have also tried all other free pins, same result. its one or the other, servo or motors, my power supply seems five due to the fact that when you overload your power supply on Arduino, it usually resets, and that is not happening.

itsthedanyole:
I have the Seeed Studio and i have also tried all other free pins, same result. its one or the other, servo or motors, my power supply seems five due to the fact that when you overload your power supply on Arduino, it usually resets, and that is not happening.

So you are trying to power the motor/servo from your arduino. Good luck!

lol, no, the motor shield has a power supply built in taking power from a 3200mah 20c lipo. i have plenty power. even if i unplug the servo and run the sketch, same result.

by the way, i have succesfully powered my gearboxes from the arduino powering the motorshield from usb cable. im suprised myself....

Servo control is accomplished using timers. The timers are also used for PWM. When you attach the first servo, PWM on pins 9 and 10 stops working, because the timer is not available for that purpose anymore.

Since you need PWM on pins 9 and 10 to control the motor speed, you can't use a servo. With that shield in place, anyway.

i was fearing that scenario. I find it odd considering there are 3 pwm outputs left.

I find it odd considering there are 3 pwm outputs left.

But not on the pins you need. The timers are tied to pins. It's unfortunate that the first use of the Servo class grabs the timer that is used for the pins that most motor shields use for PWM.

I'm not sure what it would take to change that.

Servo control is accomplished using timers. The timers are also used for PWM. When you attach the first servo, PWM on pins 9 and 10 stops working, because the timer is not available for that purpose anymore.

Since you need PWM on pins 9 and 10 to control the motor speed, you can't use a servo. With that shield in place, anyway.

You can use both the motor shield and servos what you can't do is use the servo lib.

Mark

Ah! how the heck do you use a servo without the library?