HOW? RUN STEPPER AND TWO SERVOS AT SAME TIME?

HELLO,
IF SOMEONE COULD HELP ME OUT. I'VE BEEN STUCK ON THIS PROBLEM FOR A FEW WEEKS.

**MY MAIN GOAL IS TO GET A SKETCH THAT I CAN MODIFY THAT WILL RUN A STEPPER MOTOR AND TWO SERVOS AT THE SAME TIME.

I AM BUILDING A MOTION CONTROL RIG FOR A CAMERA WITH A STEPPER FOR TRACKING. ONE SERVO FOR PAN. ONE SERVO FOR TILT.

I AM RUNNING EVERYTHING ON THE ADAFRUIT MOTOR SHIELD THAT CONNECTS ON TOP OF THE ARDUINO.

I HAVE SUCCESSFULLY GOT THE STEPPER TO RUN ON ITS OWN. WITH THIS CODE.

#include <AFMotor.h>

AF_Stepper motor(200, 2);

void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Stepper test!");

motor.setSpeed(50); // 10 rpm
}

void loop() {
Serial.println("Single coil steps");
motor.step(3000, BACKWARD, SINGLE);
delay(500);
motor.step(3000, FORWARD, SINGLE);
delay(500);

}

AND THE SERVO TO MOVE ON ITS OWN WITH THIS CODE

// Sweep

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position

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

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

ALSO, HOW DO YOU GET THE PROGRAM TO RUN ONCE... INSTEAD OF LOOPING OVER AND OVER AGAIN?

ANY HELP WOULD BE GREATLY APPRECIATED!!!

No need to shout, 'eh? Let go of the CAPS LOCK! :wink:

Sorry, I have a caps lock problem.

Code that is put in setup() runs once. Code that is put in loop() runs over and over. If you want the code to run once, it should (now) be obvious where to put it.

Put the code from setup() in each sketch into a new sketch, in the setup() function.

Put the code from loop() in each sketch into the new sketch, in the loop() function.

Thanks Paul. I'm going to try that out. That solves one hurdle.

Paul.
I am a bit of a beginner with the script (sketch) writing.

What does this mean?>

Put the code from setup() in each sketch into a new sketch, in the setup() function.

Put the code from loop() in each sketch into the new sketch, in the loop() function.

I tried to put the code from the loop() into the setup(). but it wont run. what do i do with the loop? i deleted it. and... it wont run. all these errors come up. I tried some different variations.

If you have this:

void setup()
{
   Serial.begin(9600);
}

void loop()
{
[glow]   Serial.println("Hello!");
[/glow]}

Hello will be printed over and over again.

If you change it to this:

void setup()
{
   Serial.begin(9600);
[glow]   Serial.println("Hello!");
[/glow]}

void loop()
{
}

Hello will print once.

Now, try that with your code.

This worked like a charm!
Servo only moved once.
Thank you PaulS

// Sweep

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}

void loop()
{

}

I got this code to work. It moves one servo...then moves the stepper.
How do i move them both at the same time?

// Sweep and step

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position

#include <AFMotor.h>

AF_Stepper motor(200, 2);

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}

Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Stepper test!");

motor.setSpeed(50); // 10 rpm

Serial.println("Single coil steps");

motor.step(2000, FORWARD, SINGLE);
delay(500);
}

void loop()
{

}

You are telling the servo to move in 1 degree increments, from 0 to 179, with a delay after each move starts.

Once the servo is all the way to the end, you tell it to move in 1 degree increments back to 0, with a delay after each move starts.

Then, you initialize the serial port, and write a message.

Then, you tell the stepper to move 2000 steps.

If you want the servo and stepper to move at the same time, you have to break the stepper stepping into smaller chunks, and move some steps each time the servo moves.

Look at the blink without delay example and copy that technique to break down the movement into small steps that will then appear to be happening at the same time.

I will try that Grumpy. Thanks.