Moving a motor and servo for R/C car.

Alright, I've been trying to figure this out. I've never really made a project like this with arduino before. I made a few smalls ones, but this one is complicated for me. I want to drive a motor and servo. I found some code I've been working off of to control the motor control module. I got the speed set the way I want it. The problem is when I try to implement the servo controls into the code. Once I do this..the motor goes in reverse. I know this is probably an easy fix for you arduino gurus but I'm still new..so give me a break ;D

// this uses the Arduino servo library included with version 0012

// caution, this code sweeps the motor up to maximum speed !
// make sure the motor is mounted securily before running.

#include <Servo.h>
Servo myservo;
Servo myservo2;
int pos = 0;
void arm(){
  // arm the speed controller, modify as necessary for your ESC  
  setSpeed(0);
  delay(1000); //delay 1 second,  some speed controllers may need longer
}

void setSpeed(int speed){
  // speed is from 0 to 100 where 0 is off and 100 is maximum speed
  //the following maps speed values of 0-100 to angles from 0-180,
  // some speed controllers may need different values, see the ESC instructions
  int angle = map(speed, 0, 70, 0, 180);
  myservo.write(angle);    
}

void setup()
{
  myservo.attach(8);
  myservo.attach(9);
  arm();  

}


void loop()
{
  int speed;

  // sweep up from 0 to to maximum speed in 20 seconds
  for(speed = 0; speed <= 90; speed += 5) {
    setSpeed(speed);
    delay(0);
  }
  
  
  setSpeed(100);  
  delay(500); 

for(pos = 90; pos < 160; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo2.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(0);                       // waits 15ms for the servo to reach the position 
 
}

}

Any help is appreciated, thanks.

I'm a super rookie but I know the others are going to want a little more info so....

This code looks like it's just for the servos. Where is the code for the motor? Or are you using the servo lib to control the motor?

Also, you say you want to sweep from 0 to max in 20 seconds, yet your delay is set at 0. Also you state Max is 100, but you're stopping at 90.

Finally your last line of code says wait 15ms yet you have delay(0).

I'm probably way out of line here but trying to be helpful. Good luck! :slight_smile:

i may have just found it. In setup you set myservo to 2 different pins, 8 and 9. I think you need to set like so...

void setup()
{
myservo.attach(8);
myservo2.attach(9); // you forgot the 2 here
arm();

}