/*
Forum: https://forum.arduino.cc/t/creating-a-sketch-to-control-a-stepper-motor-with-limit-switches-to-drive-a-prototype-seesaw-piston-vessel/1227569/4
Wokwi: https://wokwi.com/projects/390534148140979201
Motor Control Sketch for SeeSaw Vessel Cradle Control using AccelStepper Library
Created by James Goddings
Changes done 2024/02/23
ec2021
*/
#include "AccelStepper.h";
//Library created by Mike McCauley at http://www.airspayce.com/mikem/arduino/AccelStepper
//Used for programming stepper motors using Arduino microprocessors
//Setup Control Variables
AccelStepper stepperX(1, 6, 7); //AccelStepper Setup
//1=Easy Driver Interface
//Arduino Pin 6 Connected to PUL- Pin => Pulse width controls speed of motor
//Arduino Pin 7 Connected to DIR- Pin => Controls motor direction
//Define Limit Switch Pins
const int limit_switch_l = 2;//Left Limit Switch Connected to Pin 2
const int limit_switch_r = 3;//Left Limit Switch Connected to Pin 3
//Define Variables
//long TravelX;//Used to Store the Value X Entered in the Serial Monitor
//int move_finished;//Used to CHeck if Move is Completed
long initial_homing = -1; //Used to Home Stepper at Startup
long find_max = 1; //Used to Find Max Limit at Startup
long max_position;//Used to Set the Maximum Position of the SeeSaw
int variable_speed;//Used to Vary Speed of SeeSaw Using Potentiometer
int newspeed;
long left_position;
long right_position;
void setup() {
//Start the Serial Monitor at 9600 Baud Rate
Serial.begin(9600);
//Pull Up Resistor Debouncing on Each Switch
pinMode(limit_switch_l, INPUT_PULLUP);
pinMode(limit_switch_r, INPUT_PULLUP);
//Wait for Easy Driver to Wake
delay(5);
//Start the Homing Procedure ofthe Stepper Motor on Startup
stepperX.setMaxSpeed(100.0);//Set MaxSpeed of Stepper Low for Homing
stepperX.setAcceleration(100.0);//Set Acceleration of Stepper Low for Homing
Serial.print("Vessel is finding home position");
while (digitalRead(limit_switch_l)) { //Make the Stepper Move CCW Until the Left Limit Switch is Activated
stepperX.moveTo(initial_homing);//Set the Stepper Motor to Move CCW
stepperX.run();//Start Moving the Stepper Motor
initial_homing--;//Decrease by 1 Step if Needed
delay(5);//5 millisecond delay before looping this
}
stepperX.setCurrentPosition(0);//When the Switch is Activated Set This Position as Zero for Now
stepperX.setMaxSpeed(100.0);//Set MaxSpeed of Stepper Low for Final Home Position Seeking
stepperX.setAcceleration(100.0);//Set Acceleration of Stepper Low for Final Home Position Seeking
initial_homing = 1;
while (!digitalRead(limit_switch_l)) { //Make the Stepper Move CW Until the Left Limit Switch is Deactivated
stepperX.moveTo(initial_homing);//Set the Stepper Motor to Move CCW
stepperX.run();//Start Moving the Stepper Motor
initial_homing++;//Increase by 1 Step if Needed
delay(5);//5 millisecond delay before looping this
}
stepperX.setCurrentPosition(0);
Serial.println("Homing Complete");
Serial.println("");
Serial.println("Vessel now Seeking Max Position");
//Start the Max Seeking Procedure ofthe Stepper Motor
stepperX.setMaxSpeed(100.0);//Set MaxSpeed of Stepper Low for Max Seeking
stepperX.setAcceleration(100.0);//Set Acceleration of Stepper Low for Max Seeking
while (digitalRead(limit_switch_r)) { //Make the Stepper Move CW Until the Right Limit Switch is Activated
stepperX.moveTo(find_max);//Set the Stepper Motor to Move CW
stepperX.run();//Start Moving the Stepper Motor
find_max++;//Increase by 1 Step if Needed
delay(5);//5 millisecond delay before looping this
}
//stepperX.setCurrentPosition(max_position);//When the Switch is Activated Set This Position as Max Position for Now
max_position = stepperX.currentPosition();
stepperX.setMaxSpeed(100.0);//Set MaxSpeed of Stepper Low for Final Max Position Seeking
stepperX.setAcceleration(100.0);//Set Acceleration of Stepper Low for Final Max Position Seeking
find_max = -1;
while (!digitalRead(limit_switch_r)) { //Make the Stepper Move CCW Until the Right Limit Switch is Deactivated
stepperX.moveTo(find_max);//Set the Stepper Motor to Move CCW
stepperX.run();//Start Moving the Stepper Motor
find_max--;//Decrease by 1 Step if Needed
delay(5);//5 millisecond delay before looping this
}
max_position = stepperX.currentPosition();
stepperX.setCurrentPosition(max_position);
Serial.println("Max Seeking Complete");
Serial.println("");
Serial.println("Vessel Will Now Start Cycling");
left_position = 2; //Add a new left position variable just before left limit switch
right_position = max_position - 2; //Add a new right position variable just before left limit switch
stepperX.setMaxSpeed(3000.0);//Set Max Speed High for SeeSaw
stepperX.setAcceleration(300.0);//Set Acceleration High for SeeSaw
Serial.println(left_position);
Serial.println(right_position);
delay(2000);
Serial.println("Running Test");
}
boolean goLeft = true;
void loop() {
newspeed = map(analogRead(A1), 0, 1023, 300, 3000); //Set Potentiometer Controlled Speed Variable to Map on to Potentiomater Position
if (variable_speed != newspeed) {
variable_speed = newspeed;
stepperX.setSpeed(variable_speed);
}
if (goLeft) {
stepperX.runToNewPosition(left_position);
Serial.println(stepperX.currentPosition());
if (stepperX.currentPosition() == left_position) {
goLeft = false;
}
} else {
stepperX.runToNewPosition(right_position);
Serial.println(stepperX.currentPosition());
if (stepperX.currentPosition() == right_position) {
goLeft = true;
}
}
}