#include <Stepper.h>
#include <LiquidCrystal_I2C.h>
const int stepsPerRevolution = 200;
Stepper stepper(stepsPerRevolution, 3, 4);
const int up = 2; // Change this to the pin where your button is connected
const int down = 1;
int motorSpeed = 60; // Desired RPM
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change the I2C address if necessary
void setup() {
pinMode(up, INPUT_PULLUP);
pinMode(down, INPUT_PULLUP);
stepper.setSpeed(map(motorSpeed, 0, 60, 0, stepsPerRevolution));
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("RPM: ");
}
void loop() {
// Check if the button is pressed
if (digitalRead(up) == LOW) {
// Debounce delay
motorSpeed += 30; // Increase speed by 1 RPM
// Update the motor speed
stepper.setSpeed(map(motorSpeed, 0, 60, 0, stepsPerRevolution));
} else if (digitalRead(down) == LOW) {
motorSpeed -= 30;
if (motorSpeed < 1) {
motorSpeed = 60;
}
stepper.setSpeed(map(motorSpeed, 0, 60, 0, stepsPerRevolution));
}
// Set the direction to clockwise
digitalWrite(4, LOW);
// Rotate the motor one full revolution
stepper.step(stepsPerRevolution);
// Set the direction back to counterclockwise
digitalWrite(4, LOW);
// Calculate and display RPM
float rpm = motorSpeed;
lcd.setCursor(5, 0);
lcd.print(rpm, 1);
lcd.print(" "); // Clear any previous characters
}