I'm trying to get a servo and stepper motor to work together. The code makes the servo move and the stepper right after.
I can't seem to add a delay AFTER the servo BEFORE the stepper moves.
Can anyone help?
#include <Stepper.h>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
const int stepsPerRevolution = 400; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
void setup(){
myservo.attach(7); // attaches the servo on pin 9 to the servo object
}
{
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
}
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 >= 0; 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
}
} **//I need a delay here**
{
// step one revolution in one direction:
myStepper.step(stepsPerRevolution);
delay(5000);
}
Did you get a compiler warning? Put your cursor on the final } - you'll note the editor hilights the corresponding {; now put your cursor on the { after the loop() statement.
Is the } that gets hilighted before or after your myStepper.step() call? It's on the line where you say you need a delay.
I'm trying to tell you gently, your stepper call isn't part of loop(), and therefore is just garbage to the compiler.
C
Also you have activated serial so use the serial monitor to debug your code. An “I am here” printed to serial can be moved through code to see where it fouls up. You can also print the variables and this make sure things are as you expect.
I reworked the code. Now it has the delay right where I want it. Thank you so much guys. I can't thank you all enough. I added my desired delay at the start of the void loop.
#include <Stepper.h>
#include <Servo.h>
const int stepsPerRevolution = 800; // change this to fit the number of steps per revolution
// for your motor
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 20; // variable to store the servo position
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
myservo.attach(7); // attaches the servo on pin 9 to the servo object
}
void loop() {
delay(4000);
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(100);
for (pos = 40; 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 >= 40; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
}