/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-stepper-motor-and-limit-switch
*/
#include <ezButton.h>
#include <AccelStepper.h>
#define MAX_POSITION 0x7FFFFFFF // maximum of position we can set (long type)
ezButton limitSwitch(4); // create ezButton object that attach to pin A0;
ezButton limitSwitch1(A0); // create ezButton object that attach to pin A1;
ezButton limitSwitch2(A2); // create ezButton object that attach to pin A2;
//ezButton startbut(11);
AccelStepper stepper(1, 2, 3);
AccelStepper stepper1(1, 5, 6);
AccelStepper stepper2(1, 7, 8);
AccelStepper stepperD(1, 9, 10);
int startbut=11;
bool stopForward = false;
bool stopBackward=false;
bool stopDownward=false;
bool Init=false;
int startRead=0;
int startRead1=0;
int led=12;
void setup() {
Serial.begin(9600);
pinMode(startbut,INPUT);
pinMode(led,OUTPUT);
limitSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds
limitSwitch1.setDebounceTime(50); // set debounce time to 50 milliseconds
limitSwitch2.setDebounceTime(50); // set debounce time to 50 milliseconds
//startbut.setDebounceTime(50);
stepper.setMaxSpeed(1000.0); // set the maximum speed
stepper.setAcceleration(600.0); // set acceleration
stepper.setSpeed(300); // set initial speed
stepper.setCurrentPosition(0); // set position
stepper1.setMaxSpeed(1000.0); // set the maximum speed
stepper1.setAcceleration(600.0); // set acceleration
stepper1.setSpeed(300); // set initial speed
stepper1.setCurrentPosition(0); // set position
stepper2.setMaxSpeed(1000.0); // set the maximum speed
stepper2.setAcceleration(600.0); // set acceleration
stepper2.setSpeed(300); // set initial speed
stepper2.setCurrentPosition(0); // set position
stepperD.setMaxSpeed(1000.0); // set the maximum speed
stepperD.setAcceleration(600.0); // set acceleration
stepperD.setSpeed(300); // set initial speed
stepperD.setCurrentPosition(0); // set position
// diluent test
/*for (int i=1; i>=0;i--){
do{
ForwardSlide();
}while(!limitSwitch.isPressed());
if(limitSwitch.isPressed()){
do{
BackwardSlide();
}while(!limitSwitch1.isPressed());
}
}
*/
// stepper.moveTo(MAX_POSITION);
/* do{
ForwardSlide();
}while(!limitSwitch.isPressed());
if(limitSwitch.isPressed())
{
do {
DownwardSlide();
}while(!limitSwitch2.isPressed());
} //delay(5000);*/
}
void ForwardSlide (){
// stepper.runToNewPosition(-MAX_POSITION);
stepper.moveTo(MAX_POSITION); stepper.runToPosition();
limitSwitch.loop(); // MUST call the loop() function first
limitSwitch1.loop(); // MUST call the loop() function first
limitSwitch2.loop(); // MUST call the loop() function first
if (limitSwitch.isPressed()) {
Serial.println(F("The limit switch: TOUCHED"));
stopForward = true;
//if (limitSwitch.isPressed()) {
// stopForward= false;
//}
} else{
stopForward= false;
}
if (stopForward == false) {
// without this part, the move will stop after reaching maximum position
if (stepper.distanceToGo() < 0 ) { // if motor moved to the maximum position
stepper.setCurrentPosition(0); // reset position to 0
stepper.moveTo(MAX_POSITION);
// stepper.runToNewPosition(MAX_POSITION); // move the motor to maximum position again
}
stepper.run(); // MUST be called in loop() function
}
else
{
// without calling stepper.run() function, motor stops immediately
// NOTE: stepper.stop() function does NOT stops motor immediately
Serial.println(F("The stepper motor is STOPPED"));
}
}
void BackwardSlide(){
stepper.moveTo(-MAX_POSITION); //stepper.runToPosition();
// stepper.runToNewPosition(-100);
limitSwitch.loop(); // MUST call the loop() function first
limitSwitch1.loop(); // MUST call the loop() function first
limitSwitch2.loop(); // MUST call the loop() function first
if (limitSwitch1.isPressed()) {
Serial.println(F("The limit switch: TOUCHED"));
stopBackward = true;
// if (limitSwitch1.isPressed()) {
// stopBackward= false;
// }
} else{
stopBackward= false;
}
if (stopBackward == false) {
// without this part, the move will stop after reaching maximum position
if(stepper.distanceToGo() > 0){// if motor moved to the maximum position
stepper.setCurrentPosition(0); // reset position to 0
// stepper.runToNewPosition(-MAX_POSITION); // move the motor to maximum position again
stepper.runToNewPosition(0);
//stepper.moveTo(-MAX_POSITION);
//stepper.stop();
}
stepper.run(); // MUST be called in loop() function
}
else
{
// without calling stepper.run() function, motor stops immediately
// NOTE: stepper.stop() function does NOT stops motor immediately
Serial.println(F("The stepper motor is STOPPED"));
}
}
void DownwardSlide(){
stepper2.moveTo(-MAX_POSITION); stepper2.runToPosition();
limitSwitch.loop(); // MUST call the loop() function first
limitSwitch1.loop(); // MUST call the loop() function first
limitSwitch2.loop(); // MUST call the loop() function first
if (limitSwitch2.isPressed()) {
Serial.println(F("The limit switch: TOUCHED"));
stopDownward = true;
//if (limitSwitch2.isPressed()) {
// stopDownward= false;
// }
} else{
stopDownward= false;
}
if (stopDownward == false) {
// without this part, the move will stop after reaching maximum position
if (stepper2.distanceToGo() == 0) { // if motor moved to the maximum position
stepper2.setCurrentPosition(0); // reset position to 0
stepper2.moveTo(-MAX_POSITION);
//stepper.runToNewPosition(-M0AX_POSITION); // move the motor to maximum position again
}
stepper2.run(); // MUST be called in loop() function
}
else
{
// without calling stepper.run() function, motor stops immediately
// NOTE: stepper.stop() function does NOT stops motor immediately
Serial.println(F("The stepper motor is STOPPED"));
}
}
void diluentUp(){
stepperD.moveTo(7000);
stepperD.run();
stepperD.stop();
}
void diluentDown(){
//if (stepperD.distanceToGo() > 7000 ) {
stepperD.runToNewPosition(0);
stepperD.run();
stepperD.stop();
//}
}
/*void LyseUp(){
stepperL.moveTo(7000);
stepperL.run();
stepperL.stop();
}
void LyseDown(){
//if (stepperD.distanceToGo() > 7000 ) {
stepperL.runToNewPosition(0);
stepperL.run();
stepperL.stop();
//}
}*/
void loop() {
do {
BackwardSlide();
}while(!limitSwitch1.isPressed());
if (limitSwitch1.isPressed()){
do {
ForwardSlide();
}while(!limitSwitch.isPressed());
}
//BackwardSlide();
// measuring prog
/* startRead=digitalRead(startbut);
delay(5);
startRead1=digitalRead(startbut);
delay(5);
if (startRead = startRead1 && startRead==HIGH){
do {
digitalWrite(led,HIGH); delay(1000);
do {
BackwardSlide();
}while(!limitSwitch1.isPressed());
// delay(1000);
if (limitSwitch1.isPressed()){
delay(1000);
do {
ForwardSlide();
}while(!limitSwitch.isPressed());
} delay(1000);
if (limitSwitch.isPressed()){
delay(2000);
do{
DownwardSlide();
} while(!limitSwitch2.isPressed());
//delay(1000);
}
}while(startRead=HIGH);
}*/
}
//
//}
//BackwardSlide ();
/*https://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html#a96685e0945b7cf75d5959da679cd911e*/