/*
https://wokwi.com/projects/359373581053041665
Nachbau spruenki für
https://forum.arduino.cc/t/steppermotor-startet-nicht/1102814
Mit Änderungen, um das gewünschte Verhalten zu erzeugen
ec2021
*/
#include <Stepper.h>
// Anzahl der Schritte pro Umdrehung des Motors
const int STEPS1 = 200;
// Pinbelegungen für den Schrittmotor
const int MOTOR_PIN1 = 8;
const int MOTOR_PIN2 = 9;
const int MOTOR_PIN3 = 10;
const int MOTOR_PIN4 = 11;
// Pinbelegung für den Taster
const int BUTTON_PIN = 2;
// Schrittmotor-Objekt
// Stepper myStepper(STEPS1, MOTOR_PIN1, MOTOR_PIN3, MOTOR_PIN2, MOTOR_PIN4);
// ACHTUNG: Pin Initialisierung an die Gegebenheiten hier bei Wokwi angepasst!!
Stepper myStepper(STEPS1, MOTOR_PIN1, MOTOR_PIN2, MOTOR_PIN3, MOTOR_PIN4);
// Variablen für die Schrittzählung und die Schrittrichtung
int stepsCount = 0;
int stepsDirection = 1;
// Variablen für die Tasterabfrage
boolean buttonReleased(){
static unsigned long lastChange = 0;
static int lastButtonState = HIGH;
static int bState = HIGH;
boolean Released = false;
int state = digitalRead(BUTTON_PIN);
if (state != lastButtonState) {
lastChange = millis();
lastButtonState = state;
}
if (millis()-lastChange > 30 && lastButtonState != bState){
bState = lastButtonState;
Released = bState;
}
return Released;
}
void setup() {
// Initialisierung der seriellen Schnittstelle
Serial.begin(9600);
// Initialisierung des Schrittmotors
myStepper.setSpeed(300);
// Initialisierung des Tasters
pinMode(BUTTON_PIN, INPUT_PULLUP);
digitalWrite(BUTTON_PIN, HIGH); // interner Pull-Up-Widerstand aktivieren
}
void loop() {
if (buttonReleased()) {
// Taster wurde gedrückt und wieder losgelassen
stepsCount = stepsDirection * 1000;
Serial.print("Schritte: ");
Serial.println(stepsCount);
}
// Schrittmotor ansteuern
if (stepsCount != 0) {
myStepper.step(stepsCount);
stepsCount = 0;
stepsDirection = -stepsDirection;
delay(500);
}
}