// Start the simulation.
// Press the start button.
// Press "limit" to simulate the limit switch
#include <Stepper.h>
int stepsPerRevolution = 200;
Stepper stepper(stepsPerRevolution, 9, 10, 11, 12);
int stepperSpeed = 60; // RPM
#include <Servo.h>
Servo servo1;
Servo servo2;
int servo1pin = 3;
int servo2pin = 5;
int servo1home = 0;
int servo2home = 180;
int minmax = 3000; // minimum and maximum steps
int startButton = 2;
int limitSwitch = 4;
int startButtonState = 0;
int limitSwitchState = 0;
int randomSteps;
int stepStepper; // counter from zero to randomSteps
int stepHome; // hold the value of stepStepper when limit switch was pressed
int stepperDirection; // holds 1 or -1 to indicate direction of stepping
bool safeToStep; // if limit has not been reached
void setup() {
Serial.begin(9600);
randomSeed(analogRead(A0)); // increase randomness
pinMode(startButton, INPUT_PULLUP);
pinMode(limitSwitch, INPUT_PULLUP);
servo1.attach(servo1pin); // start the servos
servo2.attach(servo2pin);
servo1.write(servo1home);
servo2.write(servo2home);
welcome();
}
void loop() {
startButtonState = digitalRead(startButton);
limitSwitchState = digitalRead(limitSwitch);
if ((startButtonState == LOW)) { // green button
getRandomEndpoint(); // if limit switch is not pressed, use this to reverse direction
Serial.print("Step ");
if (stepperDirection == 1)
Serial.print("CCW ");
else
Serial.print("CW ");
Serial.print(abs(randomSteps));
Serial.print(" to ");
Serial.print(-randomSteps);
stepper.setSpeed(stepperSpeed);
safeToStep = 1; // a flag to step or stop the steppermotor
for (stepStepper = 0; stepStepper < abs(randomSteps); stepStepper++) {
checkLimitSwitch(); // verify limit switch has not been pressed
if (safeToStep) { // not at limit or end of steps
stepper.step(1 * stepperDirection); // one step in "direction"
stepHome++; // store the stepStepper only when stepping (not after limit)
}
}
Serial.print(" (LIMIT at "); // reached the limit switch
Serial.print(stepHome); // show where the limit switch was stopped
Serial.print(") ");
openGates(); // open the servos
goHome(); // step home
}
}
void checkLimitSwitch() {
limitSwitchState = digitalRead(limitSwitch);
if (limitSwitchState == LOW) { // limit switch (red) reached
stepper.step(0);
safeToStep = 0; // 0 = do not step
}
}
void getRandomEndpoint() {
randomSteps = random(-minmax, minmax); // get random step-to value... hope it is not zero :)
if (randomSteps < 0)
stepperDirection = -1; // counter-clockwise
else
stepperDirection = 1; // clockwise
}
void openGates() {
delay(500);
Serial.print("(OPEN) ");
servo1.write(90);
servo2.write(90);
Serial.print("(dramatic pause)");
delay(1000);
Serial.print(" (CLOSE) ");
servo1.write(0);
servo2.write(180);
delay(500);
}
void goHome() {
Serial.print("Step ");
if (stepperDirection == 1)
Serial.print("CW ");
else
Serial.print("CCW ");
Serial.print(stepHome);
Serial.print(" to 0 ");
stepper.setSpeed(stepperSpeed);
stepper.step(stepHome * -stepperDirection); // stepStepper may have been stopped by the limit switch
stepHome = 0; // reset
Serial.println("(Home)");
}
void welcome() {
Serial.println("1. Press the START button.");
Serial.println("2. You will see a direction (CW, CCW) and \"step-to\" value in the Serial Monitor.");
Serial.println("3. At any time before \"step-to\" is reached, press \"LIMIT\" to simulate a limit switch.");
Serial.println("4. Servo \"gates\" will open, \"gates\" will close.");
Serial.println("5. Stepper will step \"home\" from reaching LIMIT equal to \"-steps\".");
Serial.println();
}LIMIT
START