CODE
#include <AFMotor.h>
#include <Servo.h>
// Connect a stepper motor to motor port #2 (M3 and M4)
AF_Stepper stepperMotor(20, 2);
// DC motor on M1
AF_DCMotor motor(1);
// DC hobby servo
Servo servo1;
int motorSpeed = 50;
uint8_t currentDirection = FORWARD;
bool switchDirection = true;
void setup() {
// turn on servo
servo1.attach(9);
// turn on motor #2
stepperMotor.setSpeed(200); // 10 rpm
// turn on motor #2
motor.setSpeed(motorSpeed);
motor.run(RELEASE);
}
int i = 0;
void loop() {
if (switchDirection) {
motor.run(RELEASE);
switchDirection = false;
if (currentDirection == FORWARD) {
currentDirection = BACKWARD;
motor.run(BACKWARD);
} else {
currentDirection = FORWARD;
motor.run(FORWARD);
}
}
stepperMotor.step(1, currentDirection, MICROSTEP);
servo1.write(i);
delay(3);
i++;
if (i > 254) {
i = 0;
switchDirection = true;
motor.run(RELEASE);
delay(1000);
}
}