Servo and Motor wont move at the same time

#include <Servo.h>

//Servo servo1;
//int pos1=0;

void setup()
void loop()
/* for(pos1=180;pos1<0; pos1+=1)

The code above will move the RC car forward and backward. When you change the code to run the portion that is commented out, the code will move the servo from left to right.

I would like to move the servo then move forward or backward. I’ve tried moving the code inside the servo for loop but I haven’t got any luck.

Any reason why both sections of the code can’t be executed at the same time?

If you follow the advice given in "How to post..." You would use code tags when attaching the code. That makes it much more convienient to read thje code and more helpers will check it. If You want to move the servo before driving, why don't You put that code before the moving code?

Where is the servo and motor power coming from? Hopefully NOT the Arduino's 5 volt pin. Also, when using the servo library, PWM is disabled for pins 9 & 10, use pin 3 or 11 for motor speed.

tchristian: Any reason why both sections of the code can't be executed at the same time?

Delay() calls stop excecution of the code for that delay time. Nothing happens during delay(). I count almost 4 seconds of it. The servo is not updated until the motor code has completed, and vice versa.

Must replace those delays with millis() timing, to not stop the loop() from excecuting other things. Study the "BlinkWithoutDelay" example in the IDE. Leo..

If you use VarSpeedServo.h instead of the standard Servo.h you don’t need the for loops to move the servo slowly. You can just use the (non-blocking) speed parameter in VarSpeedServo.

But just using pin 3 or 11 instead of pin 9 for the analogWrites will make your original code work. Try it.


your code is completely wrong for servo motor you have didn't describe servo objects and pin...

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.,148850.0.html . Then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :)