#include "TeensyStep.h"
// stepper and controller
constexpr int stpPin = A8, dirPin = 1;
Stepper motor(stpPin, dirPin);
//StepControl controller;
RotateControl controller;
constexpr int stopPin = 2;
const int ALARM = A5;
const int ENA = A4;
int ENAstate=0;
long frq;
long acc;
long rpm;
// stopwatches
elapsedMillis displayStopwatch = 0; // timing the display of the current position
elapsedMillis blinkStopwatch = 0; // timing the heartbeat LED
elapsedMillis debounceTimer = 0; // debouncing input pins
int lastPos = 0;
void handlePins();
void handleCommands();
void setup()
{
Serial.println(" m: move motor Pin A8");
Serial.println(" r: RPM motor, r 1000");
Serial.println(" a: acceleration, a 1000");
Serial.println(" s: stop motor");
Serial.println(" e: ENA motor Pin A4");
Serial.println(" x: emergency stop");
Serial.println(" h: display this help");
motor.setMaxSpeed(1000);
motor.setAcceleration(1000);
pinMode(ALARM, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(stopPin, INPUT_PULLUP); // touch the pin with GND to stop the motor
}
void loop()
{
// handle incomming commands on the serial interface ------------------
handleCommands();
// handle input from pins ---------------------------------------------
handlePins();
// the usual heartbeat ------------------------------------------------
if (displayStopwatch > 2000)
{
controller.stopAsync();
displayStopwatch = 0;
}
}
void handleCommands()
{
if (Serial.available() > 0) // skip if the serial buffer is empty
{
char cmd = Serial.read();
displayStopwatch = 0;
digitalWriteFast(LED_BUILTIN, 1);
delay(10);
digitalWriteFast(LED_BUILTIN, 0);
switch (cmd) // ... and analyze it
{
case 'm': // move command
if (!controller.isRunning()) // skip move command if motor is running already
{
controller.rotateAsync(motor);
Serial.println("Started motor movement");
}
else
{
Serial.println("Ignored, motor is already running");
}
break;
case 's': // stop command
controller.stopAsync(); // initiate stopping procedure
Serial.println("Stopping motor");
break;
case 'a': // stop command
while (Serial.available()) {
String read = Serial.readStringUntil('\n');
if (read.substring(0, 1) == " ") {
acc = read.substring(1, read.length()).toInt();
motor.setAcceleration(acc);
Serial.println(acc);
}
} //end Serial.available
break;
case 'r': // stop command
while (Serial.available()) {
String read = Serial.readStringUntil('\n');
if (read.substring(0, 1) == " ") {
rpm = read.substring(1, read.length()).toInt();
frq=rpm/0.075;
motor.setMaxSpeed(frq);
Serial.println(frq);
}
} //end Serial.available
break;
case 'x': // emergency stop command
controller.emergencyStop();
Serial.println("Emergency Stop");
break;
case 'e': // ENA command
controller.emergencyStop();
ENAstate=!ENAstate;
digitalWrite(ENA,ENAstate);
delay(100);
Serial.println("ENA");
break;
case 'h': // help / usage command
case 'u':
Serial.println("\nUsage:");
Serial.println(" m: move motor Pin A8");
Serial.println(" r: RPM motor, r 1000");
Serial.println(" a: acceleration, a 1000");
Serial.println(" s: stop motor");
Serial.println(" e: ENA motor Pin A4");
Serial.println(" x: emergency stop");
Serial.println(" h: display this help");
break;
default:
break;
}
}
}
void handlePins()
{
if (controller.isRunning() && !digitalReadFast(ALARM) && debounceTimer > 200)
{
debounceTimer = 0;
controller.stopAsync(); // initiate stopping procedure
Serial.println("Stopping motor");
ENAstate=!ENAstate;
digitalWrite(ENA,ENAstate);
delay(1000);
ENAstate=!ENAstate;
digitalWrite(ENA,ENAstate);
delay(1000);
controller.rotateAsync(motor);
}
}