hello, I'm having trouble here, I only have 1 pin remaining (13) and I need to have a start stop button. Im thinking that the button that will be added can both start the code to run my switch case and when pushed again, it will pause the machine, and when pushed again, it will just resume its last case state, how it can be done?
you did not explicitly write what microcontroller you are using.
From the IO-pins used it is very likely that you use an arduino uno.
As user @DaveX already mentioned you could use the pins A0, A1, A2, A3, A4, A5 as digital pins too.
So this would enable to have a separate start and a stop button and then there would be still 4 IO-pins left free for whatever you like.
You posted your sketch and a wokwi simulation.
Sure I can read the code I can watch the simulation.
Non-theless I highly prefer to know the backround of a code.
What is the purpose of your device?
Pretty often knowing the real thing enables to make new suggestions for solutions.
Sometimes much more elegant then the chosen one.
Anyway to explain what GCJR-code does
He defined the IO-pin 13 as INPUT_PULLUP
Then below your switch-case-break-statement he added the start-stop functionality.
Such a functionality works this way
You define a second variable "state_fam_old" to memorise the state your code was in in the moment of stopping.
This state can be
case 0:
case 1:
case 2:
case stop: (which is case "5"
This means if you press the button after stopping the code continues with the same state as before stopping.
Here is the re-modificated sketch as a WOKWI-Simulation
sketch
// 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);
}