#include <LiquidCrystal.h>
#include <AccelStepper.h>
#include <Bounce2.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
AccelStepper myStepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
#define NUM_SWITCHES 2
#define NUM_BUTTONS 2
const int endSwitchPin[NUM_SWITCHES] = {A0, A1};
const int buttonPin[NUM_SWITCHES] = {A2, A3};
Bounce2::Button endSwitch[NUM_SWITCHES] = {Bounce2::Button()};
Bounce2::Button button[NUM_BUTTONS] = {Bounce2::Button()};
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
for (byte i = 0; i < NUM_SWITCHES; i++)
{
endSwitch[i].attach(endSwitchPin[i], INPUT_PULLUP);
endSwitch[i].setPressedState(LOW);
endSwitch[i].interval(5);
}
for (byte i = 0; i < NUM_BUTTONS; i++)
{
button[i].attach(buttonPin[i], INPUT_PULLUP);
button[i].setPressedState(LOW);
button[i].interval(5);
}
for (byte i = 0; i < NUM_SWITCHES; i++)
{
endSwitch[i].update();
int switchState = endSwitch[i].read();
if (switchState == HIGH) // endSwitch disconnected!
{
lcd.setCursor(0, 0);
lcd.print("End");
Serial.print("End");
lcd.print(i);
Serial.print(i);
lcd.print(" not working");
Serial.println(" not working");
while (1);
}
}
myStepper.setMaxSpeed(1000);
myStepper.setAcceleration(50);
myStepper.setSpeed(100);
lcd.setCursor(0, 0);
lcd.print("Setup Complete!");
Serial.println("Setup Complete!");
}
void loop()
{
int debouncedState[NUM_BUTTONS];
for (byte i = 0; i < NUM_BUTTONS; i++)
{
button[i].update();
debouncedState[i] = button[i].read();
if ((debouncedState[0] == LOW) && (button[0].currentDuration() > 200))
{
lcd.setCursor(0, 0);
lcd.print("Moving CCW ");
Serial.println("Moving CCW");
while (endSwitch[0].read() == LOW)
{
button[0].update();
if (button[0].pressed())
{
lcd.setCursor(0, 0);
lcd.print("Stop! ");
Serial.println("Stop!");
break;
}
endSwitch[i].update();
myStepper.move(-10000);
myStepper.run();
delay(5);
}
}
else if ((debouncedState[1] == LOW) && (button[1].currentDuration() > 200))
{
lcd.setCursor(0, 0);
lcd.print("Moving CW ");
Serial.println("Moving CW");
while (endSwitch[1].read() == LOW)
{
button[1].update();
if (button[1].pressed())
{
lcd.setCursor(0, 0);
lcd.print("Stop! ");
Serial.println("Stop!");
break;
}
endSwitch[i].update();
myStepper.move(10000);
myStepper.run();
delay(5);
}
}
}
}