#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
#include <string.h>
#define MAX8BUTTONS // spart Speicher, da nur 4 Taster benötigt werden (saves RAM)
#include <MobaTools.h>
// Pindefinitions - change to your needs
const byte dirPin = 5;
const byte stepPin = 6;
const byte enaPin = 7;
const byte button1Pin = A2;
const byte button2Pin = A1;
const byte button3Pin = A3;
const byte button4Pin = A0;
const byte potPin = A7; // must be an analog input
#define OLED_RESET A4 // Pin, an dem das OLED-Display zurückgesetzt wird
const int STEPS_REVOLUTION =800;
//Stepper einrichten ( 800 Schritte / Umdrehung - 1/4 Microstep )
MoToStepper myStepper( STEPS_REVOLUTION, STEPDIR ); // 800 Steps/ Umdrehung
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
// Taster einrichten
enum { Button1=0, Button2, Button3, Button4, } ; // Den Tasternamen die Indizes 0...3 zuordnen
const byte buttonPins[] = { button1Pin, button2Pin, button3Pin, button4Pin }; // muss als byte definiert sein, damit ein enfaches sizeof funktioniert
MoToButtons button( buttonPins, sizeof(buttonPins), 20, 500 );
MoToTimebase speedIntervall; // Zeitinterval zum Auslesen des Speedpotentiometers
// the speed pot ist read only every 'speedintervall' ms
int vspeed = 0; //Steppergeschwindigkeit in U/min*10
const char *currentDirection = "";
void displayDirection(const char *direction); // Vorwärtsdeklaration
void setup(){
myStepper.attach( stepPin, dirPin );
myStepper.attachEnable( enaPin, 10, LOW ); // Enable Pin aktivieren ( LOW=aktiv )
myStepper.setSpeed( 200 );
myStepper.setRampLen( 100); // Rampenlänge 100 Steps bei 20U/min
speedIntervall.setBasetime( 100 ); // 100ms Tickerzeit
u8g2.begin(); // Initialisiere das OLED-Display
// Ausgabe Display
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_helvB12_tf );
u8g2.drawFrame(0, 0, u8g2.getWidth(), u8g2.getHeight());
u8g2.setCursor(20, 40);
u8g2.print("Willkommen ");
} while (u8g2.nextPage());
}
void displayDirection(const char *direction){
u8g2.firstPage();
do {
u8g2.drawFrame(0, 0, u8g2.getWidth(), u8g2.getHeight());
u8g2.setFont(u8g2_font_helvB12_tf );
u8g2.drawStr((u8g2.getWidth() - u8g2.getStrWidth(direction)) / 2, u8g2.getHeight() / 2, direction);
} while ( u8g2.nextPage() );
}
void loop() {
button.processButtons(); // Taster einlesen und bearbeiten
// Speed alle 100ms neu einlesen und setzen
if ( speedIntervall.tick() ) {
// wird alle 100ms aufgerufen ( Tickerzeit = 100ms im setup() )
vspeed = map((analogRead(potPin)), 0, 1023, 20, 1800); //Poti mappen auf 2 ... 180 Umdr/Min
//min speed =2 and max speed =180 rpm
myStepper.setSpeed( vspeed );
}
// Drehen rechtsrum
if (button.pressed(Button1) ) {
//Taster1 gedrückt
myStepper.rotate( 1 ); // Stepper dreht vorwärts
//ausgabe Display
if (strcmp(currentDirection, "Rechts") != 0) {
currentDirection = "Rechts";
displayDirection(currentDirection);
}
}
if ( button.released(Button1) ){
//Taster1 losgelassen
myStepper.rotate(0); // Stepper stoppt
// Ausgabe Display
currentDirection = "Stop";
displayDirection(currentDirection);
}
//Drehen linksrum
if (button.pressed(Button2) ) {
//Taster2 gedrückt
myStepper.rotate( -1 ); // Stepper dreht rückwärts
if (strcmp(currentDirection, "Links") != 0) {
currentDirection = "Links";
displayDirection(currentDirection);
}
}
if ( button.released(Button2) ) {
//Taster2 losgelassen
myStepper.rotate(0); // Stepper stoppt
// Ausgabe Display
currentDirection = "Stop";
displayDirection(currentDirection);
}
// Reserve {
if (button.pressed(Button3) ) {
//Taster3 wurde gedrückt
//myStepper.doSteps(STEPS_REVOLUTION);
currentDirection = "Reserve";
displayDirection(currentDirection);
}
// Reserve
if (button.pressed(Button4) ) {
//Taster4 wurde gedrückt
//myStepper.doSteps(-STEPS_REVOLUTION);
currentDirection = "Reserve";
displayDirection(currentDirection);
}
}