Can't Get Motor and Servo to Run at Same Time - Uno

Hi all!

So I'm just going to preface this by saying this is my first Arduino project so while I am fairly familiar with EE concepts (graduating senior), I don't know the ins and outs of the Arduino just yet (hopefully in time though!). I've done a fair amount of research about my problem already but no older threads seem to help solve my problem. The problem I'm having is that I can only get either my motor or my servo to run, not both simultaneously. Initially, I thought it could be a PWM timer issue but I looked up online and and documentation says all the motor pins I tried used other timers. I'm driving the motor from the Arduino 5V pin, being PWM with a BJT, and speed controlled by a potientiometer (not ideal, I realize but the project is part restricted and almost all of the parts are god awful, I tried to drive the motor from a 9V source with so many different configurations but I destroyed the windings on so many motors)

Anyways, here's my code which makes it pretty obvious as to where I'm putting everything.

/*
Arduino Servo Test sketch
*/
#include <Servo.h>
Servo servoMain; // Define our Servo

#define MOTOR 5
#define VIN A3

int servoPin = 10;

void setup()
{
  servoMain.attach(servoPin); // servo on digital pin 10
  Serial.begin(9600);

  pinMode(MOTOR, OUTPUT);
  pinMode(VIN, INPUT);

  analogWrite(MOTOR, 0);
  delay(5000);
}

void loop()
{
  int input = analogRead(VIN);
  analogWrite(MOTOR, input/4);

  for (int i=0; i<=180; i=i+2) {
    input = analogRead(VIN);
    analogWrite(MOTOR, input/4);
    servoMain.write(i);
    delay(25);
    Serial.println("Loop A!");
  }
  //delay (2000);
  for (int i=180; i>=0; i=i-2) {
    input = analogRead(VIN);
    analogWrite(MOTOR, input/4);
    servoMain.write(i);
    delay(25);
    Serial.println("Loop B!");
  }

}

you make several analogwrites after each other
that is confusing, repair and put the code in again

It sounds as if you're trying to draw a quart out of a pint bottle. The Arduino's 5V regulator can only supply 500mA. Try driving both the motor and servo from a separate power supply and not the Arduino's 5V regulator.

Henry_Best:
It sounds as if you're trying to draw a quart out of a pint bottle. The Arduino's 5V regulator can only supply 500mA. Try driving both the motor and servo from a separate power supply and not the Arduino's 5V regulator.

I don't really have an option for that. I have a very limited amount of parts and a regulator is not one of them. The 9V battery I'm allowed to use as a supply for the motor or servo simply does not work with the parts I have. I know what I'm doing on the electronics side (the Arduino is the pretty murky bit) and it really is impossible, mainly due to the motor I have to use (which is terrible). Incidentally, the motors max current is is 110 mA (repeatedly exceeded and it breaks. I've tested that.) so unless the servo is drawing a ton of current, which I kinda doubt, I don't think the current is a problem. I think it's a coding to Arduino instruction that's causing the problem.

shooter:
you make several analogwrites after each other
that is confusing, repair and put the code in again

This is more what I was thinking what the problem was but I don't know what my alternatives are? My impression was that if I used the different timers it'd be fine but quite apparently, that's not the case.

The Arduino simply can't be used to power a motor or a servo (servos draw 1 ampere or more when loaded.)

Use 4AA, C or D batteries instead. If you are not allowed such alternatives, such ridiculous limitations guarantee that the project will not work.

Hmm. That would be fairly unfortunate (the best I could do would be take out a couple of cells out of my 9V to bring it down to 6V cause they're adamant it has to be a 9V :P) but also plausible. What confuses me though is that I can run both individually just fine and my servo does move my cart (the motor isn't very good, I doubt it takes up too many resources). I did use a ton of gears to torque down the servo load (same with the motor load). Would you have any suggestions for how to wire the servo to a 9V battery? I just don't really know anything about servos but motors I understand fairly well. I have a FET, a few resistors, and capacitors but nothing that I see as that useful. (Sidenote: I could care less how fast the 9V dissipates and I might even use a recheargable to bring it down to 8.4 V if that much better)

Still confused about the analogWrite issue though.

Most servos will be destroyed if operated on a 9V battery. Again, question the limitations imposed on this project!

It would be damaged because it's a 3-6 V servo. I wish I could but it's very limited resources competition and they didn't budge when I asked. I can settle to use the motor and the servo one at a time but that wouldn't be preferable. Would there be any way to effectively step down the voltage to the range? Also, I'm assuming yes but just to check, does a servos load vary when it's being written too.

But again, all of this still doesn't solve the analogWrite issue even if I were able to figure out how to supply the servo.

Let's spell things out very clearly and unambiguously.

A PP3 type of 9v battery is a poor choice for an Arduino board even with nothing attached to it. It will run down very quickly. Those batteries are not intended to provide the sort of current flow that an Arduino needs. A pack of 6 AA NiMh batteries is a good solution.

The 5v pin on an Arduino is NOT suitable for driving any motor. It cannot provide enough current and if it is overloaded even briefly it will cause the Arduino to reset which completely screws up your project and may cause damage to the Arduino.
You may get away wilth driving an unloaded small servo or motor from the 5v pin - but that is living on the edge. Don't expect to get away wilth two of them at a the same time. Also, back-emf from the motor is not good for the Arduino.

Motors and servos should have their own power supply with the GND connected to the Arduino GND.

Anyone who demands that you breach these simple guidelines does not know his donkey from his elbow - and feel free to show him this Thread.

...R