// Learn about the ESP32 WiFi simulation in
// https://docs.wokwi.com/guides/esp32-wifi
#include <Wire.h>
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>
#include <AccelStepper.h>
// LCD-Objekt
hd44780_I2Cexp lcd;
// Encoder-Pins
#define CLK 13
#define DT 14
#define SW 12
AccelStepper Xaxis(1, 17, 16); // pin 3 = step, pin 6 = direction
#define EN 12
int selectedMotor = 0; // Aktuell gewählter Motor (M1–M6)
float motorSpeed[6] = {0, 0, 0, 0, 0, 0}; // Drehzahl für M1–M6 (0–255)
bool motorState[6] = {false, false, false, false, false, false}; // Motor an/aus
int lastStateCLK;
void setup() {
Serial.begin(115200);
Wire.begin(21, 22);
// LCD initialisieren
lcd.begin(16, 2);
lcd.backlight();
lcd.clear();
// Encoder-Pins als Eingänge setzen
pinMode(CLK, INPUT_PULLUP);
pinMode(DT, INPUT_PULLUP);
pinMode(SW, INPUT_PULLUP);
lastStateCLK = digitalRead(CLK);
lcd.setCursor(0, 0);
lcd.print("Motor 1:");
updateLCD();
Xaxis.setMaxSpeed(400*200);
Xaxis.setSpeed(motorSpeed[selectedMotor]);
}
void loop() {
Xaxis.runSpeed();
int stateCLK = digitalRead(CLK);
// Prüfen, ob sich der Encoder bewegt hat
if (stateCLK != lastStateCLK) {
if (digitalRead(DT) != stateCLK) {
motorSpeed[selectedMotor] += 0.05;
if (motorSpeed[selectedMotor] > 255) motorSpeed[selectedMotor] = 255; // Begrenzung
} else {
motorSpeed[selectedMotor] -= 0.05;
if (motorSpeed[selectedMotor] < 0) motorSpeed[selectedMotor] = 0; // Begrenzung
}
Serial.print("M");
Serial.print(selectedMotor + 1);
Serial.print(" Speed: ");
Serial.println(motorSpeed[selectedMotor]);
Xaxis.setSpeed(motorSpeed[selectedMotor]*200);
updateLCD();
}
lastStateCLK = stateCLK;
// Prüfen, ob der Knopf gedrückt wurde
if (digitalRead(SW) == LOW) {
delay(200); // Entprellen
if (motorSpeed[selectedMotor] == 0) {
// Falls Drehzahl 0 ist, wechsel zum nächsten Motor
selectedMotor = (selectedMotor + 1) % 6;
Serial.print("Wechsle zu Motor ");
Serial.println(selectedMotor + 1);
} else {
// Motor toggeln (an/aus)
motorState[selectedMotor] = !motorState[selectedMotor];
Serial.print("Motor ");
Serial.print(selectedMotor + 1);
Serial.print(motorState[selectedMotor] ? " EIN" : " AUS");
Serial.println();
}
updateLCD();
}
}
void updateLCD() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("M");
lcd.print(selectedMotor + 1);
lcd.print(": ");
lcd.print(motorSpeed[selectedMotor]);
lcd.setCursor(0, 1);
lcd.print(motorState[selectedMotor] ? "AN " : "AUS");
}