#include <AccelStepper.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int directionPin = 8;
const int stepPin = 9;
const int buttonPin1 = 2; // Start/Stop button
const int buttonPin2 = 3; // Toggle direction button
const int numberOfSteps = 360;
const int pulseWidthMicros = 20;
const int millisBetweenSteps = 10;
const int potentiometerPin = A3;
const int SpeedDownButton = 6;
const int SpeedUpButton = 7;
bool clockwise = true; // Variable to track the direction of the stepper motor
bool motorRunning = false; // Variable to track if the motor is currently running
AccelStepper stepper(1, stepPin, directionPin); // Using interface 1 (step/direction) and assigning pins
LiquidCrystal_I2C lcd(0x27, 16, 2);
float angleOfRotation = 1.8;
int q = 1;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(3, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(5, OUTPUT);
pinMode(buttonPin1, INPUT); // Internal pull-up resistor enabled for button 1 pin
pinMode(buttonPin2, INPUT); // Internal pull-up resistor enabled for button 2 pin
pinMode(potentiometerPin, INPUT);
pinMode(SpeedUpButton, INPUT);
pinMode(SpeedDownButton, INPUT);
stepper.setMaxSpeed(1); // Set the maximum speed in steps per second
stepper.setAcceleration(1); // Set the acceleration in steps per second per second
lcd.print(angleOfRotation);
lcd.setCursor(0, 1);
if(clockwise) lcd.print("Clockwise");
else lcd.print("Anticlockwise");
}
void loop() {
int a = map(analogRead(potentiometerPin),0,1023,0,100);
// if(digitalRead(SpeedDownButton)) {
// lcd.print("Hi");
// delay(1000);
// }
if(digitalRead(SpeedDownButton)) {
q -= 1;
digitalWrite(LED_BUILTIN, HIGH);
angleOfRotation -= 1.8;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(angleOfRotation);
lcd.setCursor(0, 1);
if(clockwise) lcd.print("Clockwise");
else lcd.print("Anticlockwise");
}
else digitalWrite(LED_BUILTIN, LOW);
if(digitalRead(SpeedUpButton)) {
q += 1;
digitalWrite(LED_BUILTIN, HIGH);
angleOfRotation += 1.8;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(angleOfRotation);
lcd.setCursor(0, 1);
if(clockwise) lcd.print("Clockwise");
else lcd.print("Anticlockwise");
}
else digitalWrite(LED_BUILTIN, LOW);
stepper.setMaxSpeed(a); // Set the maximum speed in steps per second
stepper.setAcceleration(a); // Set the acceleration in steps per second per second
// Serial.println(a);
if(digitalRead(3)) digitalWrite(LED_BUILTIN, HIGH);
else digitalWrite(LED_BUILTIN, LOW);
if(digitalRead(2)) digitalWrite(5, HIGH);
else digitalWrite(5, LOW);
if (digitalRead(buttonPin2) == HIGH) {
clockwise = !clockwise;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(angleOfRotation);
lcd.setCursor(0, 1);
if(clockwise) lcd.print("Clockwise");
else lcd.print("Anticlockwise");
}
if (digitalRead(buttonPin1) == HIGH) {
if (!motorRunning) {
if (clockwise) {
Serial.println("Clockwise");
stepper.moveTo(q); // Move the motor a fixed number of steps in one direction
stepper.runToPosition();
} else {
Serial.println("Anticlockwise");
stepper.moveTo(-q); // Move the motor a fixed number of steps in the opposite direction
stepper.runToPosition();
}
stepper.run(); // Start the motor movement
motorRunning = true; // Set motorRunning to true
} else {
stepper.stop(); // Immediately stop the motor
stepper.setCurrentPosition(0); // Reset the current position to zero
motorRunning = false; // Set motorRunning to false
}
}
}