// Date: 19.12.2024
// Changes: -
// Version: 01
// initialize the Button
#include <Button.h>
Button button1(4);
#define stepsPerRevolution 200
const int stepDelay = 4000;
const int buttonPin = 4;
int oldValue = HIGH; // default/idle value for pin 4 is high.
// initialize the Stepper Modul A4988
#define dirPin 2
#define stepPin 3
#define dirPin2 5
#define stepPin2 6
// initialize the LCD-Display
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 10, d5 = 9, d6 = 8, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// initialize important variables
int M1_current_position = 0;
int M1_soll_position = 0;
int Time_Start = 0;
int Time_End = 0;
int Time = 0;
void setup()
{
// Setup LCD-Display
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
lcd.print("Hello World!"); // Print a message to the LCD.
delay(2000); // Set delay to 1 Seconde
lcd.clear(); // Clear the LCD-Display
//Setup Motor1 & Motor2
// Declare pins as output:
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(dirPin2, OUTPUT);
// Initialize the pin for reading the button.
pinMode(buttonPin, INPUT_PULLUP);
button1.begin();
}
void loop()
{
Display_Time();
Button_Time();
if(M1_soll_position!=0)
{
Motor2_Pull_back();
Motor1_Change_position();
Motor2_Pull_forwad();
}
}
void Display_Time()
{
lcd.setCursor(0, 0);
lcd.print("Time:");
lcd.setCursor(11, 0);
lcd.print("Pos.:");
if(M1_current_position==200)
{
M1_current_position=0;
lcd.clear();
}
lcd.setCursor(11, 1);
lcd.print(M1_current_position);
}
void Button_Time()
{
int newValue = digitalRead(buttonPin);
if(newValue != oldValue) // Check if the value was changed, by comparing it with the previous value.
{
if(newValue == LOW)
{
Time_Start = millis()/1000;
}
else
{
Time_End = millis()/1000;
Time = Time_End - Time_Start;
lcd.setCursor(0, 1);
lcd.print(Time);
if(Time == 3) {M1_soll_position=1;}
if(Time == 4) {M1_soll_position=2;}
if(Time == 5) {M1_soll_position=3;}
if(Time == 6) {M1_soll_position=4;}
}
// Remember the value for the next time.
oldValue = newValue;
}
// Slow down the sketch.
// Also for debouncing the button.
delay(100);
}
// Motor für die Auswahl der Richtung (Gerade, Rechts, Zurück und Links)
void Motor1_Change_position()
{
int Soll = 50*M1_soll_position;
int Steps_Schleife = 0;
if(Soll<M1_current_position)
{
// Set the spinning direction clockwise:
digitalWrite(dirPin, LOW);
Steps_Schleife = M1_current_position - Soll;
for (int i = 0; i < Steps_Schleife; i++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay);
M1_current_position--;
}
}
if(Soll>M1_current_position)
{
// Set the spinning direction clockwise:
digitalWrite(dirPin, HIGH);
Steps_Schleife = Soll - M1_current_position;
for (int i = 0; i < Steps_Schleife; i++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay);
M1_current_position++;
}
}
M1_soll_position = 0;
delay(500);
}
void Motor2_Pull_back()
{
// Set the spinning direction clockwise:
digitalWrite(dirPin2, LOW);
for (int i = 0; i < 600; i++)
{
digitalWrite(stepPin2, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepPin2, LOW);
delayMicroseconds(stepDelay);
}
delay(500);
}
void Motor2_Pull_forwad()
{
// Set the spinning direction clockwise:
digitalWrite(dirPin2, HIGH);
for (int i = 0; i < 600; i++)
{
digitalWrite(stepPin2, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepPin2, LOW);
delayMicroseconds(stepDelay);
}
delay(500);
}