So basically i want my program to run my stepper back and forth at the number of steps chosen in a loop but i also want my servo to be moving left and right constantly. It should be something easy to achieve but i am not really keen on that kind of commands. I tried using a while(1) statement but only the code in the statement was active (servo) and the rest (stepper) didn't happen. Thanks!
my code:
#include <Servo.h>
Servo myservo;
const int dirPin = 2;
const int stepPin = 5;
const int enablePin = 8;
const int stepsPerRevolution = 200;
void setup()
{
// Declare pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, LOW);
myservo.attach(9);
myservo.write(45); // set servo to mid-point
delay(1000);
}
void loop()
{
// Set motor direction clockwise
while(1)
{
myservo.write(45);
delay(700);
myservo.write(135);
delay(700);
}
digitalWrite(dirPin, HIGH);
// Spin motor slowly
for(int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
delay(100);
// Set motor direction counterclockwise
digitalWrite(dirPin, LOW);
// Spin motor quickly
for(int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
delay(100);
}