123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- #include "TeensyStep.h"
- constexpr int stpPin = 0, dirPin = 1;
- Stepper motor(stpPin, dirPin);
- StepControl controller;
- constexpr int stopPin = 2;
- elapsedMillis displayStopwatch = 0;
- elapsedMillis blinkStopwatch = 0;
- elapsedMillis debounceTimer = 0;
- int lastPos = 0;
- void handlePins();
- void handleCommands();
- void setup()
- {
- while (!Serial);
- Serial.println("Simple Serial Stepper Example");
- Serial.println("(type h for help)");
-
- motor.setMaxSpeed(5000);
- motor.setAcceleration(50000);
- pinMode(LED_BUILTIN, OUTPUT);
- pinMode(stopPin, INPUT_PULLUP);
- }
- void loop()
- {
-
- handleCommands();
-
- handlePins();
-
- if (displayStopwatch > 20)
- {
- displayStopwatch = 0;
- int currentPos = motor.getPosition();
- if (currentPos != lastPos)
- {
- lastPos = currentPos;
- Serial.println(currentPos);
- }
- }
-
- if (blinkStopwatch > 250)
- {
- blinkStopwatch = 0;
- digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
- }
- }
- void handleCommands()
- {
- if (Serial.available() > 0)
- {
- char cmd = Serial.read();
- switch (cmd)
- {
- case 'm':
- if (!controller.isRunning())
- {
- motor.setTargetRel(20000);
- controller.moveAsync(motor);
- Serial.println("Started motor movement");
- }
- else
- {
- Serial.println("Ignored, motor is already running");
- }
- break;
- case 's':
- controller.stopAsync();
- Serial.println("Stopping motor");
- break;
- case 'e':
- controller.emergencyStop();
- Serial.println("Emergency Stop");
- break;
- case 'h':
- case 'u':
- Serial.println("\nUsage:");
- Serial.println(" m: move motor");
- Serial.println(" s: start stop sequence");
- Serial.println(" e: emergency stop");
- Serial.println(" h: display this help");
- break;
- default:
- break;
- }
- }
- }
- void handlePins()
- {
- if (controller.isRunning() && !digitalReadFast(stopPin) && debounceTimer > 200)
- {
- debounceTimer = 0;
- controller.stopAsync();
- Serial.println("Stopping motor");
- }
- }
|