// AccelStepper_AccelToConstantSpeed
// https://wokwi.com/projects/422896273363206145
// based on:
// https://wokwi.com/projects/419849124942020609
// Code from https://github.com/waspinator/AccelStepper/blob/master/examples/ConstantSpeed/ConstantSpeed.pde
// Other example simulations https://forum.arduino.cc/t/wokwi-simulations-for-arduino-built-in-examples/1304754
// You should not drive a stepper direcly from the controller
// Use a modern H-bridge driver like a TB6612FNG, or an old,
// inefficient L298N, or a modern STEP+DIR driver.
// ConstantSpeed.pde
// -*- mode: C++ -*-
//
// Shows how to run AccelStepper in the simplest,
// fixed speed mode with no accelerations
/// \author Mike McCauley ([email protected])
// Copyright (C) 2009 Mike McCauley
// $Id: ConstantSpeed.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $
#include <AccelStepper.h>
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
enum State {INIT, ACCEL, CRUISING, STOPPING} state = INIT;
const int StopPin = A0;
const int GoPin = A1;
const long MyMax = 4000;
const long MyAcc = 500;
void setup()
{
pinMode(StopPin, INPUT_PULLUP);
pinMode(GoPin, INPUT_PULLUP);
}
void loop()
{
switch (state) {
case INIT:
stepper.setMaxSpeed(MyMax);
stepper.setAcceleration(MyAcc);
stepper.move(11ULL * MyMax * MyMax / MyAcc / 10); // needs to be far enough to ramp up
// so, per p_ramp = 1/2at^2 & v=at, 2*p_ramp = v^2/a
// +10% for integer math edges
state = ACCEL;
break;
case ACCEL:
if (stepper.speed() == stepper.maxSpeed()) {
state = CRUISING;
}
if ( digitalRead(StopPin) == LOW) {
stepper.stop();
state = STOPPING;
}
stepper.run();
break;
case CRUISING:
stepper.runSpeed();
if ( digitalRead(StopPin) == LOW) {
stepper.stop();
state = STOPPING;
}
break;
case STOPPING:
stepper.run();
if (stepper.speed() == 0 && digitalRead(GoPin) == LOW) {
state = INIT;
}
}
}