#include <DRV8434S.h>
#include <Accessories.h>
#include <LocoStepper.h>
#include <VarSpeedServo.h>
#include <Adafruit_BusIO_Register.h>
#include <Adafruit_I2CDevice.h>
#include <Adafruit_I2CRegister.h>
#include <Adafruit_SPIDevice.h>
#include <AccelStepper.h>
#include <MultiStepper.h>
#include <Stepper.h>
#include <Servo.h>
#include <AFMotor.h>
/*
- 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(A0); // create ezButton object that attach to pin A0;
AccelStepper stepper(AccelStepper::FULL4WIRE, 3, 6, 5, 4);
bool isStopped = false;
void setup() {
Serial.begin(9600);
stepper.setSpeed(120);
limitSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds
stepper.setMaxSpeed(500.0); // set the maximum speed
stepper.setAcceleration(50.0); // set acceleration
stepper.setSpeed(500); // set initial speed
stepper.setCurrentPosition(0); // set position
stepper.moveTo(MAX_POSITION);{
}
limitSwitch.loop(); // MUST call the loop() function first
if (limitSwitch.isPressed()); {
Serial.println(F("The limit switch: TOUCHED"));
isStopped = true;
}
if (isStopped == 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); // 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"));
ezButton limitSwitch(7); // create ezButton object that attach to pin 7;
if(limitSwitch.isPressed())
Serial.println("The limit switch: UNTOUCHED -> TOUCHED");
if(limitSwitch.isReleased())
Serial.println("The limit switch: TOUCHED -> UNTOUCHED");
int state = limitSwitch.getState();
if(state == HIGH)
Serial.println("The limit switch: UNTOUCHED");
else
Serial.println("The limit switch: TOUCHED");
}