/* Demo of A4988 micro-stepping motor driver
Arduino Uno
Arduino IDE 1.8.10
Uses button debounce library https://github.com/thomasfredericks/Bounce2
Gadget Reboot
viedeo dazu: https://www.youtube.com/watch?v=V7amtlMKsXU
*/
/* Motor fährt bis auf Endschalter, wenn dieser erreicht ist
fährt er 35 mm nach unten um Laserpunkt scharf zu stellen
*/
#include <Bounce2.h>
//DISPLAY
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4 // not used
Adafruit_SSD1306 display(OLED_RESET);
// Drehschalter
int CLK = 10;
int DT = 11;
int SW = 12;
int Position = 0;
int Letzte_Position = LOW;
int n = LOW;
int Taster = LOW;
int Letzte_Taster = LOW;
//Variablen
String text1 = "";
String text2 = "";
int SOLL = 0; // zu fahrende Strecke
int IST = 0; // tatsächliche Position
int um = 400 / 8; // umrechnungsfaktor 400 Schritte = 8 mm
int fahren = 0;
// Laser entfernung = 69 mm ; bis Tischplatte = 34 mm differenz = 35 mm
// nach Referenzfahrt 35 runter fahren = 1750 Schritte
// neue Maße am 22.5.24 Laser entfernung = 78 mm ; bis Tischplatte = 34 mm differenz = 44 mm
// nach Referenzfahrt 44 runter fahren = 2200 Schritte
int laser = 2200;
// stepper control outputs
#define motStepPin 2
#define motDirPin 3
#define motMS1Pin 4
#define motMS2Pin 5
#define motMS3Pin 6
/*/ button inputs
#define buttonDir 7
#define buttonRun 8
*/
int Ends = 13;
/*/ create debounce instances for 2 buttons
Bounce dirIn = Bounce(); // motor drehrichtung ändern
Bounce runIn = Bounce(); // motor fährt die angegebenen Schritte (Position)
*/
void setup() {
// Drehschalter
pinMode (CLK, INPUT_PULLUP);
pinMode (DT, INPUT_PULLUP);
pinMode (SW, INPUT_PULLUP);
//DISPLAY
Serial.begin (9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // I2C-Adresse 0x3c initialisieren
display.clearDisplay();
display.setTextColor(WHITE); // set text color / Textfarbe setzen
display.setTextSize(2);
text1 = String(Position)+ String(" mm");
display.setCursor(1, 0);
display.println(text1);
display.display();
#define DRAW_DELAY 118 //DISPLAY
#define D_NUM 47
/*/ Zeitraum Abfrage der Taster
dirIn.attach(buttonDir, INPUT_PULLUP);
dirIn.interval(10);
runIn.attach(buttonRun, INPUT_PULLUP);
runIn.interval(10);
*/
// Konfigurieren anfänglichen Motoreinstellungen
digitalWrite(motStepPin, LOW);
digitalWrite(motDirPin, HIGH); // Drehrichtung nach unten
digitalWrite(motMS1Pin, LOW);
digitalWrite(motMS2Pin, LOW);
digitalWrite(motMS3Pin, LOW); // HIGH = 200 Schritte = 1 Spindelumdrehung
pinMode(motStepPin, OUTPUT);
pinMode(motDirPin, OUTPUT);
pinMode(motMS1Pin, OUTPUT);
pinMode(motMS2Pin, OUTPUT);
pinMode(motMS3Pin, OUTPUT);
// Schalter abfragen 0 Position anfahren
// Display
display.clearDisplay(); // set text color / Textfarbe setzen
display.setTextColor(WHITE);
display.setTextSize(2);
text2 = String("0 Anfahren");
display.setCursor(1, 10);
display.println(text2);
display.display();
delay(10);
pinMode(Ends, INPUT);
if (digitalRead(Ends) == false) {
while (digitalRead(Ends) == false) // Wenn Stepper bereits auf entschalter steht
{
//motor lauf
for (int i = 0; i < 10 ; i++) { // Stepper fährt von 0 Position weg
int motSpeed = map( 900 , 0, 1023, 5000, 200); // erster Wert = Speed
digitalWrite(motDirPin, HIGH);
digitalWrite(motStepPin, HIGH);
delayMicroseconds(motSpeed);
digitalWrite(motStepPin, LOW);
delayMicroseconds(motSpeed);
}
}
for (int i = 0; i < 100 ; i++) { // Stepper fährt von 0 Position weg
int motSpeed = map( 900 , 0, 1023, 5000, 200); // erster Wert = Speed
digitalWrite(motDirPin, HIGH);
digitalWrite(motStepPin, HIGH);
delayMicroseconds(motSpeed);
digitalWrite(motStepPin, LOW);
delayMicroseconds(motSpeed);
}
}
while (digitalRead(Ends) == true) { // Stepper fährt bis endschalter erreicht
//motor lauf
for (int i = 0; i < 5 ; i++) {
int motSpeed = map( 600 , 0, 1023, 5000, 200); // erster Wert = Speed
digitalWrite(motDirPin, LOW);
digitalWrite(motStepPin, HIGH);
delayMicroseconds(motSpeed);
digitalWrite(motStepPin, LOW);
delayMicroseconds(motSpeed);
}
}
digitalWrite(motMS3Pin, HIGH); // Auf 400 Schritte pro Umdrehung einstellen
delay(800);
// Anfahren Laserpunkt
for (int i = 0; i < laser ; i++) { // laser, schaft stellen = Zeile 44
int motSpeed = map( 600 , 0, 1023, 5000, 200); // erster Wert = Speed
digitalWrite(motDirPin, HIGH);
digitalWrite(motStepPin, HIGH);
delayMicroseconds(motSpeed);
digitalWrite(motStepPin, LOW);
delayMicroseconds(motSpeed);
}
// Display
display.clearDisplay(); // set text color / Textfarbe setzen
display.setTextColor(WHITE);
display.setTextSize(2);
text2 = String("0 Position");
display.setCursor(1, 10);
display.println(text2);
display.display();
delay(1000);
IST= 0; // referenzfahrt abgeschlossen somit IST Position = 0
}
void loop() {
SOLL = IST; // SOLL = IST Position
/*/ Status der Taster aktualisieren
dirIn.update();
runIn.update();
*/
// Schritte einstellen
n = digitalRead(CLK);
Taster = !digitalRead(SW); // Taster gedrückt ?
if (Taster != Letzte_Taster){
display.clearDisplay(); // set text color / Textfarbe setzen
display.setTextColor(WHITE);
display.setTextSize(2);
text1 = String(Position)+ String(" mm");
display.setCursor(1, 0);
display.println(text1);
text2 = String("Fahre ")+ (Position); // Anzeige der gefahrenen Schritte
display.setCursor(1, 18);
display.println(text2);
display.display();
delay(10);
}
// Schritte einstellen
if ((Letzte_Position == LOW) && (n == HIGH)) {
if (digitalRead(DT) == LOW) {
Position = Position + 1;
}
else {
Position = Position - 1;
}
//DISPLAY
display.clearDisplay();
display.setTextColor(WHITE); // set text color / Textfarbe setzen
display.setTextSize(2);
text1 = String(Position)+ String(" mm");
display.setCursor(1, 0);
display.println(text1);
text2 = String("ist ")+IST + String(" mm") ; // Anzeige tatsächlich gefahren insgesamt
display.setCursor(1, 18);
display.println(text2);
display.display();
delay(10);
}
Letzte_Position = n;
fahren = Position * um;
// Motor fährt die eingestellten Schritte (Position)
digitalWrite(motDirPin, HIGH); // Drehrichtung des Steppers nach unten
Taster = !digitalRead(SW); // Taster gedrückt ?
if (Taster != Letzte_Taster){
for (int i = 0; i < fahren ; i++) {
int motSpeed = map( 900 , 0, 1023, 5000, 200); //erster Wert ist Geschwindigkeit
// Stepper um einen Schritt bewegen
digitalWrite(motStepPin, HIGH);
delayMicroseconds(motSpeed);
digitalWrite(motStepPin, LOW);
delayMicroseconds(motSpeed);
IST = SOLL + Position; // rechne gefahrene Schritte zusammen
}
Position = 0; // Variable für Einstellung zurücksetzen
}
}