Or you could just have simply posted the code
/*
Astronomia Motorized Stepper code
Using the Arduino Serial Monitor I was able to see that 22.00
was the optimum initial Speed value for my build.
Astro_Motorized_Stepper.ino
*/
//Include the Arduino Stepper Library
#include <Stepper.h>
#include <EEPROM.h>
// Define Constants
const int MAX_SPEED = 100;
const int STEPS_PER_REV = 32;
// Define 7-segment display variables
int num_array[10][7] = { { 1,1,1,1,1,1,0 }, // 0
{ 0,1,1,0,0,0,0 }, // 1
{ 1,1,0,1,1,0,1 }, // 2
{ 1,1,1,1,0,0,1 }, // 3
{ 0,1,1,0,0,1,1 }, // 4
{ 1,0,1,1,0,1,1 }, // 5
{ 1,0,1,1,1,1,1 }, // 6
{ 1,1,1,0,0,0,0 }, // 7
{ 1,1,1,1,1,1,1 }, // 8
{ 1,1,1,0,0,1,1 }}; // 9
int segmentPins[7] = {13, 12, 7, 6, 5, A4, A3};
int pwrLED = A5;
// Define Variables
// Create Instance of Stepper Class
// Specify Pins used for motor coils
// The pins used are 8,9,10,11
// Connected to ULN2003 Motor Driver In1, In2, In3, In4
// Pins entered in sequence 1-3-2-4 for proper step sequencing
Stepper steppermotor(STEPS_PER_REV, 8, 10, 9, 11);
int pValue; //knob value
int idxDigit;
int digitDisplayCount;
float speed;
float savedEepromSpeed;
bool isOn;
bool canToggle;
bool isStartup;
bool flashBlank;
unsigned long time;
unsigned long lastTime;
unsigned long releaseTime;
unsigned long lastDigitDisplayTime;
unsigned long lastTurnDetected;
// Rotary Encoder
volatile boolean TurnDetected;
volatile boolean up;
const int PinCLK=2; // Used for generating interrupts using CLK signal
const int PinDT=3; // Used for reading DT signal
const int PinSW=4; // Used for the push button switch
const int DEFAULT_SPEED = 22;
const int DEBOUNCE_TIME = 250;
void isr () { // Interrupt service routine is executed when a HIGH to LOW transition is detected on CLK
if (digitalRead(PinCLK))
up = digitalRead(PinDT);
else
up = !digitalRead(PinDT);
TurnDetected = true;
}
void setup()
{
pinMode(PinCLK,INPUT);
pinMode(PinDT,INPUT);
pinMode(PinSW,INPUT_PULLUP);
attachInterrupt (0,isr,FALLING); // interrupt 0 is always connected to pin 2 on Arduino UNO
Serial.begin(115200);
EEPROM.get(0, speed);
savedEepromSpeed = speed;
Serial.println(speed);
if(isnan(speed)){
speed = DEFAULT_SPEED;
EEPROM.put(0, speed);
savedEepromSpeed = speed;
}
pinMode(pwrLED, OUTPUT);
for(int i = 0; i < 7; i++){
pinMode(segmentPins[i], OUTPUT);
}
isOn = true;
canToggle = true;
isStartup = true;
lastTurnDetected = 0;
resetDigitDisplay();
}
void loop()
{
digitalWrite(pwrLED,isOn);
if (isOn) {
steppermotor.setSpeed(speed);
steppermotor.step(1);
}
else {
steppermotor.setSpeed(0);
}
static long virtualPosition=0; // without STATIC it does not count correctly!!!
if (digitalRead(PinSW))
{
releaseTime = millis();
canToggle = true;
}
if (!(digitalRead(PinSW))) { // check if pushbutton is pressed
time = millis();
if (time > releaseTime + 5000) {
Serial.println("Speed Reset");
speed = DEFAULT_SPEED;
releaseTime = millis();
resetDigitDisplay();
blink_led(5);
}
if(canToggle and time > (lastTime + DEBOUNCE_TIME))
{
isOn = !isOn;
canToggle = false;
lastTime = millis();
if(isOn)
{
Serial.println("Motor On");
resetDigitDisplay();
}
else
{
Serial.println("Motor Off");
}
}
}
if (TurnDetected and isOn) { // do this only if rotation was detected
if (up)
speed -= 1;
else
speed += 1;
if (speed < 10){
speed = 5;
}
else if (speed > MAX_SPEED){
speed = MAX_SPEED;
}
TurnDetected = false; // do NOT repeat IF loop until new rotation detected
Serial.println (speed);
lastTurnDetected = millis();
resetDigitDisplay();
}
else if (TurnDetected) {
TurnDetected = false;
}
writeSpeedToDisplay();
//write to EEPROM if turn was detected
//and has not been touched for 5 seconds
if(lastTurnDetected > 0 && millis() > lastTurnDetected + 5000 && savedEepromSpeed != speed)
{
Serial.println("Speed Saved");
EEPROM.put(0, speed);
savedEepromSpeed = speed;
}
}
void blink_led(int len){
for (int i = 0; i < len; i++) {
digitalWrite(pwrLED,1);
delay(50);
digitalWrite(pwrLED,0);
delay(50);
}
}
void writeNumber(int number)
{
for (int j=0; j < 7; j++) {
digitalWrite(segmentPins[j], num_array[number][j]);
}
}
void writeBlank()
{
for (int j=0; j < 7; j++) {
digitalWrite(segmentPins[j], LOW);
}
}
void writeSpeedToDisplay()
{
time = millis();
if(time > lastDigitDisplayTime + 1000 && digitDisplayCount < 3 && isOn && time > lastTurnDetected + 1000){
char b[5];
String str;
str=String(speed);
str.toCharArray(b,5);
if(b[idxDigit] == '.' || b[idxDigit] == NULL || flashBlank){
writeBlank();
flashBlank = false;
}
else{
writeNumber(b[idxDigit] - '0');
flashBlank = true;
}
//display next digit
if(!flashBlank) idxDigit += 1;
//if next index is outside of array bounds, reset to zero
if(idxDigit == 5){
idxDigit = 0;
digitDisplayCount++; //increment display count to only show 3 times
}
lastDigitDisplayTime = time;
}
else if(!isOn || time <= lastTurnDetected + 1000)
{
writeBlank();
}
}
void resetDigitDisplay(){
idxDigit = 0;
digitDisplayCount = 0;
flashBlank = false;
}