// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298
#define dbg(myFixedText, variableName) \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName);
#define dbgi(myFixedText, variableName,timeInterval) \
{ \
static unsigned long intervalStartTime; \
if ( millis() - intervalStartTime >= timeInterval ){ \
intervalStartTime = millis(); \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName); \
} \
}
#define dbgc(myFixedText, variableName) \
{ \
static long lastState; \
if ( lastState != variableName ){ \
Serial.print( F(#myFixedText " " #variableName" changed from ") ); \
Serial.print(lastState); \
Serial.print( F(" to ") ); \
Serial.println(variableName); \
lastState = variableName; \
} \
}
#define dbgcf(myFixedText, variableName) \
{ \
static float lastState; \
if ( lastState != variableName ){ \
Serial.print( F(#myFixedText " " #variableName" changed from ") ); \
Serial.print(lastState); \
Serial.print( F(" to ") ); \
Serial.println(variableName); \
lastState = variableName; \
} \
}
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *
#include <Stepper.h>
int stepsPerRevolution = 2048;
int motSpeed = 50;
int dt = 500;
int state_fam = 0;
int state_fam_old;
const byte button1Pin = 10;
const byte button2Pin = 11;
const byte button3Pin = 12;
const byte StartStopPin = 13;
const byte pressed = LOW; // define a self-explaining constant
byte lastBut4State;
int motDir = 1;
Stepper myStepper1(stepsPerRevolution, 2, 4, 3, 5);
Stepper myStepper2(stepsPerRevolution, 6, 8, 7, 9);
void setup() {
Serial.begin(115200);
Serial.println("Setup-Start");
myStepper1.setSpeed(motSpeed);
myStepper2.setSpeed(motSpeed);
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
pinMode(button3Pin, INPUT_PULLUP);
pinMode(StartStopPin, INPUT_PULLUP);
lastBut4State = digitalRead(StartStopPin);
myStepper1.setSpeed(motSpeed);
myStepper2.setSpeed(motSpeed);
Serial.println("print button 2 to start");
while (digitalRead(button2Pin) == HIGH) {
delay(50);
}
Serial.println("button2Pin pressed => start loop()");
}
byte runStepper1 = false;
byte runStepper2 = false;
const int Stopped = 5;
void loop() {
//Serial.print("state: ");
//Serial.println(state_fam);
dbgc("ToL",state_fam);
switch (state_fam) {
case 0:
runStepper1 = true;
myStepper1.step(motDir * 1);
if (digitalRead(button1Pin) == pressed) {
Serial.println("button1Pin pressed => stop stepper1");
Serial.println("rotating stepper2 CW");
runStepper1 = false;
state_fam = 1;
}
break;
case 1:
runStepper2 = true;
myStepper2.step(motDir * 1);
if (digitalRead(button2Pin) == pressed) {
Serial.println("button2Pin pressed change to state 2=rotate stepper2 CCW");
runStepper2 = false;
state_fam = 2;
}
break;
case 2:
runStepper2 = true;
myStepper2.step(motDir * -1);
if (digitalRead(button3Pin) == pressed) {
Serial.println("button3Pin pressed changing to state 0");
runStepper2 = false;
state_fam = 0;
}
break;
case Stopped:
break;
}
byte actualButState = digitalRead (StartStopPin); // read in button for start/stop
if (lastBut4State != actualButState) { // check if logic state has changed
lastBut4State = actualButState; // update variable lastBut4State
delay (50); // wait a short moment for debouncing
// check if Start-Stop-Button is pressed
if (actualButState == pressed) {
// when Start-Stop-Button is pressed
// check if code is in mode "Stopped"
if (state_fam == Stopped)
// when code IS in mode Stopped
state_fam = state_fam_old; // continue through switching to the mode before stopping
else { // = a stepper is running
state_fam_old = state_fam; // memorise the actual state
state_fam = Stopped; // and change to mode Stopped
}
}
}
delay(50);
}