#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Stepper.h>
unsigned long startTime = 0;
unsigned long elapsedTime = 0;
int buttonPin1 = 3; // Use pin 2 for the first button (Number of Steps)
int buttonPin2 = 2; // Use pin 3 for the second button (Speed)
int buttonPin3 = 4; // Use pin 4 for the third button (Duration)
int buttonPin4 = 5; // Use pin 5 for the fourth button (Start)
int buttonPin5 = 6; // Use pin 6 for the fifth button (Stop)
LiquidCrystal_I2C lcd(0x27, 20, 4);
Stepper stepper(200, 8, 7); // Change the pin numbers as per your wiring
int selectedSteps = 0; // Number of steps (0 to 200)
int selectedSpeed = 0; // Speed in ms (0 to 100)
int selectedDuration = 0; // Duration in seconds (0 to 60)
void setup() {
Serial.begin(9600);
stepper.setSpeed(30); // Set the initial stepper motor speed (you can adjust this)
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
// Display initial messages
lcd.print(" AUTOMATED ");
lcd.setCursor(0, 1);
lcd.print(" BAG-VALVE-MASK ");
lcd.setCursor(0, 2);
lcd.print(" (BVM) ");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" PLEASE CHOOSE YOUR ");
lcd.setCursor(0, 1);
lcd.print(" SETTING ");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Speed: 0ms ");
lcd.setCursor(0, 1);
lcd.print("Steps: 0 ");
lcd.setCursor(0, 2);
lcd.print("Duration: 0s ");
}
void loop() {
if (digitalRead(buttonPin1) == HIGH) {
selectedSteps = (selectedSteps + 1) % 201; // Toggle the number of steps (0 to 200)
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Steps: ");
lcd.print(selectedSteps);
lcd.print(" ");
delay(500); // Debounce delay
}
if (digitalRead(buttonPin2) == HIGH) {
selectedSpeed = (selectedSpeed + 10) % 101; // Toggle the speed (0 to 100 ms)
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Speed: ");
lcd.print(selectedSpeed);
lcd.print("ms ");
delay(500); // Debounce delay
}
if (digitalRead(buttonPin3) == HIGH) {
selectedDuration = (selectedDuration + 10) % 61; // Toggle the duration (0 to 60 seconds)
lcd.clear();
lcd.setCursor(0, 2);
lcd.print("Duration: ");
lcd.print(selectedDuration);
lcd.print("s ");
delay(500); // Debounce delay
}
if (digitalRead(buttonPin4) == HIGH) {
// Start the stepper motor based on the selected parameters
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Speed: ");
lcd.print(selectedSpeed);
lcd.print("ms ");
lcd.setCursor(0, 1);
lcd.print("Steps: ");
lcd.print(selectedSteps);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print("Duration: ");
lcd.print(selectedDuration);
lcd.print("s ");
if (selectedSpeed == 0) {
moveStepperSlow(selectedSteps);
} else {
moveStepperFast(selectedSteps);
}
}
if (digitalRead(buttonPin5) == HIGH) {
// Stop the stepper motor immediately
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Stopping");
stepper.step(0);
}
}
void moveStepperSlow(int targetPos) {
unsigned long startTime = millis();
while (elapsedTime < selectedDuration * 1000) {
for (int steps = 0; steps < targetPos * 20; steps += 5) {
if (digitalRead(buttonPin5) == HIGH) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Stopping");
stepper.step(0);
return; // Exit the function early
}
stepper.step(1);
delay(selectedSpeed);
elapsedTime = millis() - startTime;
}
for (int steps = targetPos * 20; steps > 0; steps -= 5) {
if (digitalRead(buttonPin5) == HIGH) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Stopping");
stepper.step(0);
return; // Exit the function early
}
stepper.step(-1);
delay(selectedSpeed);
elapsedTime = millis() - startTime;
}
}
}
void moveStepperFast(int targetPos) {
unsigned long startTime = millis();
while (elapsedTime < selectedDuration * 1000) {
for (int steps = 0; steps < targetPos * 20; steps += 5) {
if (digitalRead(buttonPin5) == HIGH) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Stopping");
stepper.step(0);
return; // Exit the function early
}
stepper.step(1);
delay(selectedSpeed);
elapsedTime = millis() - startTime;
}
for (int steps = targetPos * 20; steps > 0; steps -= 5) {
if (digitalRead(buttonPin5) == HIGH) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Stopping");
stepper.step(0);
return; // Exit the function early
}
stepper.step(-1);
delay(selectedSpeed);
elapsedTime = millis() - startTime;
}
}
}