// https://wokwi.com/projects/369635724513089537
// https://forum.arduino.cc/t/creation-of-a-routine-with-a-button-that-triggers-relays-and-2-stepper-motors-arduino-uno/1145630
#include <Stepper.h>
const int stepsPerRevolution = 200; // adjust for your motor
const int PRESST = LOW;
// initialize two steppers
Stepper motor1(stepsPerRevolution, 6, 7, 8, 9);
Stepper motor2(stepsPerRevolution, 10, 11, 12, 13);
void setup() {
Serial.begin(115200);
Serial.println("Whirled Peas!\n"); // xprintf works like printf. no floats
// set the speed at 3 rpm: (20 seconds / revolution)
motor1.setSpeed(18); // 18 takes 5/6 second. life too short!
motor2.setSpeed(18);
pinMode(2, INPUT_PULLUP);
}
void loop() {
motor1.step(50);
motor2.step(-50);
waitOnButton();
motor1.step(-50);
motor2.step(50);
waitOnButton();
relayControl(3, HIGH);
waitOnButton();
relayControl(4, HIGH);
waitOnButton();
relayControl(3, LOW);
delay(1000);
relayControl(4, LOW);
delay(1000);
relayControl(5, HIGH);
delay(1000);
relayControl(5, LOW);
waitOnButton();
}
void waitOnButton()
{
Serial.println("waiting on the button");
do delay(15); while (digitalRead(2) != PRESST);
while (digitalRead(2) == PRESST) delay(15);
delay(15);
}
void relayControl(unsigned char thePin, unsigned char theValue)
{
digitalWrite(thePin, theValue);
}
# define N15 111 // number of 15 ms ticks button must be down 333 = 5 seconds
void waitOnLongPress()
{
int count = 0;
Serial.println("waiting for a long press");
delay(15);
while (digitalRead(2) == PRESST) delay(15);
while (1) {
do delay(15); while (digitalRead(2) != PRESST);
count = 0;
do {
delay(15);
count++;
if (count > N15) return;
} while (digitalRead(2) == PRESST);
}
}
// alternate wait on button - allows "instant" response
void waitOnButton0()
{
static unsigned long lastTime;
static unsigned char lastButton = HIGH;
unsigned long now;
Serial.println("a different waiting on the button");
for (; ; ) {
now = millis();
if (now - lastTime < 50) continue;
unsigned char thisButton = digitalRead(2);
if (thisButton != lastButton) {
lastButton = thisButton;
lastTime = now;
if (thisButton == PRESST) break;
}
}
}