I've had a little play around with your code and added debounce to the ToggleCheck() loop.
Now it complies but I have not tried it as I don't have hardware for it, Others may correct me and there may be a better ways, but I find this works ok, I'm still learning myself and enjoy tinkering about.
#include <AccelStepper.h>
const byte stepPin1 = 8;
const byte stepPin2 = 9;
const byte stepPin3 = 10;
const byte stepPin4 = 11;
const byte buttonPin = 2;
const byte limitTop = 4;
const byte limitBottom = 5;
const unsigned long debounceTime = 10; // milliseconds without delay
unsigned long switchPressTime; // when the switch last changed state
byte oldSwitchState = HIGH; // assume switch open because of pull-up resistor
int Direction = HIGH;
const int topSpeed = 10000; // Set stepper max speed
boolean Initialized = false;
boolean ToggleSwitch = false;
boolean limitTpressed = false;
boolean limitBpressed = false;
boolean inMotion = false;
AccelStepper myStepper(AccelStepper::FULL4WIRE, stepPin1, stepPin2, stepPin3, stepPin4);
void setup ()
{
pinMode (limitTop, INPUT);
pinMode (limitBottom, INPUT);
pinMode (buttonPin, INPUT);
myStepper.setMinPulseWidth(20);
myStepper.setMaxSpeed(topSpeed);
}
void loop ()
{
if (!Initialized) { // System turned on, move to top and initialize
movetoTop();
}
if (Initialized) { // If it's initialized, proceed
if (!ToggleSwitch) { // If the Momentary button hasn't been pressed, check it,
ToggleCheck(); // sets Momentary button(ToggleSwitch) value.
}
if ((inMotion) && (Direction == HIGH)) {
movetoTop();
}
if ((inMotion) && (Direction == LOW)) {
movetoBottom();
} //if its not inMotion do nothing.
}
}
void ToggleCheck()
{
int val = digitalRead(buttonPin);
//##############################
// switch debounce starts here #
// #############################
if (val != oldSwitchState)
{
// debounce switch starts here
if (millis () - switchPressTime >= debounceTime)
{
switchPressTime = millis (); // when we closed the switch
oldSwitchState = val; // remember for next time
if (val == LOW)
{
ToggleSwitch = true;
inMotion = true;
Direction = (Direction == HIGH) ? LOW : HIGH; //Toggles which direction it has been moved when button is pressed
} // end if switchState is LOW
else
{
ToggleSwitch = false;
}
}
}// end of state change
}
void movetoTop()
{
int val = digitalRead(limitTop); //get top limit switch value
if (val == HIGH) {
limitTpressed = true;
}
if (val == LOW) {
limitTpressed = false;
}
if (!limitTpressed) { //if the top limit switch isn't pressed, move it up.
inMotion = true;
myStepper.moveTo(400000); //uses acceleration so motor doesnt stall
myStepper.run();
}
if (limitTpressed) { //halt movement
myStepper.setCurrentPosition(0);
Initialized = true;
inMotion = false;
ToggleSwitch = false;
Direction = HIGH;
}
}
void movetoBottom()
{
int val = digitalRead(limitBottom); //get bottom limit switch value
if (val == HIGH) {
limitBpressed = true;
}
if (val == LOW) {
limitBpressed = false;
}
if (!limitBpressed) { //if the bottom limit switch isn't pressed, move it down at full speed.
inMotion = true;
myStepper.setSpeed(-topSpeed); // Sets top speed to go in reverse (Move down)
myStepper.runSpeed();
}
if (limitBpressed) { //halt movement
myStepper.setCurrentPosition(0);
inMotion = false;
Direction = LOW;
ToggleSwitch = false;
}
}
Like said there is a lot of logic behind it and there would be a better way to do it.
Let us know how it goes