Beginner: Motor

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(180);                  // sets the servo position according to the scaled value
  delay(2000);                           // wait 2 seconds
  myservo.write(90);                  // move the servo to 90
  delay(2000);                           //wait 2 seconds before repeating
}

I have this, to turn the car, but I'm not sure how I can have the motor going before it turns and while it turns and after it turns before coming to a stop. Could someone help me out

Is that 'myservo' the motor your talking about? Or is that only turning it? If so, variable names are a tool, name it according to it's function :wink:

PS Good job using code tags! :smiley:

I'm not sure how I can have the motor going before it turns and while it turns and after it turns before coming to a stop.

The problem is that the delay() function stops anything else happening during the delay period

Avoid the problem. See Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

septillion:
Is that 'myservo' the motor your talking about? Or is that only turning it? If so, variable names are a tool, name it according to it's function :wink:

PS Good job using code tags! :smiley:

This is what I've got so far to steer the car, now I need to start adding the motor component. I don't expect anyone to write it for me, but if you could just let me know how to start off, that would be great. The motor is run through an ESC so will be listed as a "servo" as well. Thanks a lot!

if you could just let me know how to start off, that would be great

You need to eliminate any code that blocks the free running of the loop() function so that you can control the motor and steering at what appears to be the same time. In practice they will be controlled sequentially but by repeating it quickly you can appear to do two or more things at the same time. Have you looked at the links in reply #2 ?

UKHeliBob:
You need to eliminate any code that blocks the free running of the loop() function so that you can control the motor and steering at what appears to be the same time. In practice they will be controlled sequentially but by repeating it quickly you can appear to do two or more things at the same time. Have you looked at the links in reply #2 ?

Will look at it now, thanks!

Jluca:
This is what I've got so far to steer the car, now I need to start adding the motor component. I don't expect anyone to write it for me, but if you could just let me know how to start off, that would be great. The motor is run through an ESC so will be listed as a "servo" as well. Thanks a lot!

  1. The ESC part is pretty important
  2. That this was just steering was not clear. And it could have been with a proper variable name :wink: 'steeringServo' instead of 'myServo' would clear things up very much.

Similarity you make something like a 'driveServo' or 'driveESC'. But indeed, with blocking code it's going to be pretty hard to steer while diving.

For an example of non-blocking code have a look at Several Things at a Time and Planning and Implementing a Program

...R