#include <Arduino.h>
#include <AccelStepper.h>
#include <U8g2lib.h>
#include <Wire.h>
#include <string.h>
#define OLED_RESET 4
#define STEPS_PER_REVOLUTION 200
#define MOTOR_PIN_STEP 5
#define MOTOR_PIN_DIR 6
#define LEFT_BUTTON_PIN A1
#define RIGHT_BUTTON_PIN A2
#define POTENTIOMETER_PIN A0
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
AccelStepper stepper(AccelStepper::DRIVER, MOTOR_PIN_STEP, MOTOR_PIN_DIR);
const char *currentDirection = "";
void displayDirection(const char *direction); // Vorwärtsdeklaration
void setup() {
pinMode(LEFT_BUTTON_PIN, INPUT_PULLUP);
pinMode(RIGHT_BUTTON_PIN, INPUT_PULLUP);
stepper.setMaxSpeed(1000);
stepper.setAcceleration(500);
u8g2.begin();
}
void loop() {
int speed = map(analogRead(POTENTIOMETER_PIN), 0, 1023, 100, 1000);
stepper.setSpeed(speed);
if (digitalRead(LEFT_BUTTON_PIN) == LOW) {
stepper.setSpeed(-speed);
if (strcmp(currentDirection, "Links") != 0) {
currentDirection = "Links";
displayDirection(currentDirection);
}
while (digitalRead(LEFT_BUTTON_PIN) == LOW) {
stepper.runSpeed();
}
stepper.stop();
currentDirection = "Stop";
displayDirection(currentDirection);
}
if (digitalRead(RIGHT_BUTTON_PIN) == LOW) {
stepper.setSpeed(speed);
if (strcmp(currentDirection, "Rechts") != 0) {
currentDirection = "Rechts";
displayDirection(currentDirection);
}
while (digitalRead(RIGHT_BUTTON_PIN) == LOW) {
stepper.runSpeed();
}
stepper.stop();
currentDirection = "Stop";
displayDirection(currentDirection);
}
}
void displayDirection(const char *direction) {
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_helvB10_tf);
u8g2.drawFrame(0, 0, u8g2.getWidth(), u8g2.getHeight());
u8g2.setFont(u8g2_font_logisoso28_tf);
u8g2.drawStr((u8g2.getWidth() - u8g2.getStrWidth(direction)) / 2, u8g2.getHeight() / 2, direction);
} while ( u8g2.nextPage() );
}