Lanyard machine.pdf (7.1 KB)
Why does my servo go to 110+ degrees instead of the int pos = 10? It woks fine after it gets going but will not go to pos 10 at the beginning.
#include <Stepper.h>
#include <Servo.h>
const byte buttonPin = 5; // the pin to which the pushbutton is attached
bool runStepper = false;
bool endCycle = false;
bool betweenCycleTiming = false;
unsigned long betweenCycleStartTime;
const unsigned long betweenCycleDelay = 3000;
const int stepsPerRevolution = 575; // steps to make 13 inch paracord lanyard
Servo myservo; // create servo object to control a servo
int pos = 10; // starting position is not top dead center
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(13, OUTPUT);
// set the speed at 30 rpm:
myStepper.setSpeed(30);
myservo.attach(7); // attaches the servo on pin 7 to the servo object
}
void loop() {
checkButton();
if (runStepper == true and betweenCycleTiming == false)
{
Serial.println("running");
myStepper.step(-stepsPerRevolution);
delay(100);
for (pos = 50; pos <= 110; pos += 1) { // goes from 50 degrees to 110 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(100); // waits 10ms for the servo to reach the position
}
for (pos = 110; pos >= 50; pos -= 1) { // goes from 110 degrees to 50 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 5ms for the servo to reach the position
}
endCycle = true;
Serial.println("end cycle");
}
if (endCycle == true)//pick up betweenCycleDelay start and enable timing
{
betweenCycleTiming = true;
betweenCycleStartTime = millis();
endCycle = false;
digitalWrite(13, HIGH);//indicate start of 3 second period
}
if (betweenCycleTiming == true)//allow for button read between cycles
{
checkButton();
//restart aftercooling delay if button not pressed to stop cycle
if (millis() - betweenCycleStartTime >= betweenCycleDelay)
{
betweenCycleTiming = false;
digitalWrite(13, LOW);
}
}
}
void checkButton()
{
const byte debouncePeriod = 50;
static byte oldButtonState = HIGH;
byte buttonState = digitalRead(buttonPin); //read push button state
if ((buttonState == LOW) && (oldButtonState == HIGH)) // check is button pressed and is changed using logic for input pullup
{
delay(debouncePeriod); //blocking debounce routine using delay
buttonState = digitalRead(buttonPin); //read button again
if ( buttonState == LOW) //still LOW
{
if (runStepper == false)
{
Serial.println("run");
runStepper = true;
}
else
{
Serial.println("stop");
runStepper = false;
}
}
}
oldButtonState = buttonState;
}