/*
https://wokwi.com/projects/359372678957818881
Nachbau spruenki für
https://forum.arduino.cc/t/steppermotor-startet-nicht/1102814
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);
// Variablen für die Schrittzählung und die Schrittrichtung
int stepsCount = 0;
int stepsDirection = 1;
// Variablen für die Tasterabfrage
int buttonState = 0;
int lastButtonState = 0;
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() {
// Tasterabfrage
buttonState = digitalRead(BUTTON_PIN);
if (buttonState != lastButtonState) {
if (buttonState == LOW) {
// Taster wurde gedrückt
stepsCount += stepsDirection * 1000;
Serial.print("Schritte: ");
Serial.println(stepsCount);
}
lastButtonState = buttonState;
}
// Schrittmotor ansteuern
if (stepsCount != 0) {
myStepper.step(stepsDirection);
stepsCount -= stepsDirection;
}
}