/** Short push = inch Long push = middle of rail, Second long = end of rail */ #include AccelStepper stepper(1, A5, A4); // 1 = Easy Driver, A5 STEP pin, A4 DIR pin const int LIMIT = A1; // Limit switch connected to pin A1 const int TOGG = 12; // Toggle switch connected to pin 12 const int INCH = 13; // Inch Push buton connected to pin 13 // *** Inch_Man variables *** int SteppInch = false; // logic condition to run manual int TargetCal = false; // logic condition to calculate target long curPos; //current position long target; //move to position variable long targetInch; //set new target position for inch // *** Enter here variable depending on rail length *** long homePos = 0; //home position long midPos = 10500; //half way: depends on rail's length long endPos = 21000; //End of rail: depends on rail's length int inchMoveval = 200; //When pushbutton short value of one move //Variable for Dual push button function float pressLength_milliSeconds = 0; int optionOne_milliSeconds = 100; //Short push int optionTwo_milliSeconds = 1000; //Long push void setup() { Serial.begin(9600); pinMode(LIMIT, INPUT_PULLUP); //Pin mode for the Limit switch pinMode(TOGG,INPUT); //Pin mode for the direction toggle switch pinMode(INCH,INPUT); //Pin mode for the inch push buttom } void loop() { Inch_Man(); } void Inch_Man() { stepper.setMaxSpeed(800); stepper.setAcceleration(200); while (digitalRead(INCH) == HIGH && SteppInch == false && digitalRead(LIMIT)== HIGH){ delay(100); //if you want more resolution, lower this number pressLength_milliSeconds = pressLength_milliSeconds + 100; Serial.println(pressLength_milliSeconds); } // Lond Push Option if (pressLength_milliSeconds >= optionTwo_milliSeconds){ SteppInch = true; // Calculate Targets TargetCal = true; if (TargetCal == true && digitalRead(TOGG) == HIGH){ if (stepper.currentPosition() >= homePos && stepper.currentPosition() < midPos) { target = midPos; } else if (stepper.currentPosition() >= midPos && stepper.currentPosition() < endPos) { target = endPos; } } else if (TargetCal == true && digitalRead(TOGG) == LOW){ if (stepper.currentPosition() <= endPos && stepper.currentPosition() > midPos) { target = midPos; } else if (stepper.currentPosition() <= midPos && stepper.currentPosition() > homePos) { target = homePos; } } TargetCal = false; if (SteppInch = true) { stepper.moveTo(target); while (stepper.currentPosition() != target) { if (digitalRead(LIMIT)== HIGH){ stepper.run(); } else { break; } } SteppInch = false; } } //Short Push option else if(pressLength_milliSeconds >= optionOne_milliSeconds){ if (digitalRead(TOGG)== HIGH){ curPos = stepper.currentPosition (); targetInch = curPos + inchMoveval; stepper.moveTo (target); while (stepper.currentPosition () != targetInch){ if (digitalRead(LIMIT)== HIGH){ stepper.setSpeed(400); stepper.runSpeed(); } } } if ( digitalRead(TOGG)== LOW){ curPos = stepper.currentPosition (); targetInch = curPos - inchMoveval; stepper.moveTo (target); while (stepper.currentPosition () != targetInch){ if (digitalRead(LIMIT)== HIGH){ stepper.setSpeed(-400); stepper.runSpeed(); } } } } pressLength_milliSeconds = 0; }