// Define the pins for the stepper motor control
const int stepPin = 9; // Step pin connected to A4988 STEP pin
const int dirPin = 8; // Direction pin connected to A4988 DIR pin
const int buttonPinStart = 2; // Start button pin
const int buttonPinStop = 3; // Stop button pin
const int buttonPinDirChange = 4; // Direction change button pin
const int buttonPinMode = 5; // Mode button pin
const int buttonPinAngleInc = 6; // Angle increase button pin
const int buttonPinAngleDec = 7; // Angle decrease button pin
const int buttonPinReset = 10; // Reset button pin
const int potPinSpeed = A0; // Potentiometer pin for speed control
const int potPinRest = A1;
const int buttonPinResInc = 11;
const int buttonPinResDec = 12;
// Define variables
int stepDelay = 10000; // Initial delay between steps in microseconds
int currentAngle = 0; // Current angle setting
bool motorRunning = false; // Flag to track motor state
bool angleMode = false; // Flag to indicate the current mode
int direction = HIGH; // Initial direction (clockwise)
int res=1;
int totalsteps=0;
void setup() {
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(buttonPinStart, INPUT_PULLUP); // Internal pull-up resistor
pinMode(buttonPinStop, INPUT_PULLUP); // Internal pull-up resistor
pinMode(buttonPinDirChange, INPUT_PULLUP); // Internal pull-up resistor
pinMode(buttonPinMode, INPUT_PULLUP); // Internal pull-up resistor
pinMode(buttonPinAngleInc, INPUT_PULLUP); // Internal pull-up resistor
pinMode(buttonPinAngleDec, INPUT_PULLUP); // Internal pull-up resistor
pinMode(buttonPinReset, INPUT_PULLUP); // Internal pull-up resistor
pinMode(buttonPinResInc , INPUT_PULLUP); // Internal pull-up resistor
pinMode(buttonPinResDec, INPUT_PULLUP); // Internal pull-up resistor
// Initialize Serial communication
Serial.begin(9600);
// Set initial direction (clockwise)
digitalWrite(dirPin, direction);
// Print initial instructions
Serial.println("Press Mode button to switch between Angle and Speed modes.");
Serial.println("Press Start button to start the motor.");
Serial.println("Press Stop button to stop the motor.");
Serial.println("Press Direction button to change motor direction.");
Serial.println("Use Angle increase/decrease buttons to set the rotation angle.");
Serial.println("Press Reset button to undo changes and reset.");
}
void loop() {
// Read the state of the mode button
if (digitalRead(buttonPinMode) == LOW) {
angleMode = !angleMode; // Toggle mode
Serial.print("Mode changed to: ");
Serial.println(angleMode ? "Angle" : "Speed");
delay(500); // debounce delay
}
// Read the state of the start button
if (digitalRead(buttonPinStart) == LOW && !motorRunning) {
motorRunning = true; // Start motor
Serial.println("Motor started");
delay(500); // debounce delay
}
// Read the state of the stop button
if (digitalRead(buttonPinStop) == LOW && motorRunning) {
motorRunning = false; // Stop motor
Serial.println("Motor stopped");
delay(500); // debounce delay
}
// Read the state of the direction button
if (digitalRead(buttonPinDirChange) == LOW) {
direction = !direction; // Toggle direction
digitalWrite(dirPin, direction);
Serial.println(direction ? "Clockwise" : "Anti-Clockwise");
delay(500); // debounce delay
}
if (digitalRead(buttonPinResInc) == LOW && res<100) {
res=(res*10);
Serial.print("Resolution ");
Serial.println(res);
delay(500); // debounce delay
}
if (digitalRead(buttonPinResDec) == LOW && res>1) {
res=(res/10);
Serial.print("Resolution ");
Serial.println(res);
delay(500); // debounce delay
}
// Read the state of the angle increase button
if (digitalRead(buttonPinAngleInc) == LOW && !motorRunning) {
currentAngle+=(1*res); // Increase angle
Serial.print("Angle set to: ");
Serial.println(currentAngle);
delay(500); // debounce delay
}
// Read the state of the angle decrease button
if (digitalRead(buttonPinAngleDec) == LOW && !motorRunning) {
currentAngle-=(1*res);// Decrease angle
Serial.print("Angle set to: ");
Serial.println(currentAngle);
delay(500); // debounce delay
}
// Read the state of the reset button
if (digitalRead(buttonPinReset) == LOW) {
resetSystem();
Serial.println("System reset.");
delay(500); // debounce delay
}
int PotValueSpeed = analogRead(potPinSpeed);
stepDelay = map(PotValueSpeed, 0, 1023, 2000, 700); // Adjust the range for speed
// Execute based on current mode
// Execute based on current mode
if (angleMode) {
// Angle rotation mode
if (motorRunning) {
rotateByAngle(currentAngle);
}
} else {
// Speed control mode
if (motorRunning) {
rotateBySpeed();
}
}
}
// Function to rotate the stepper motor by the specified angle
void rotateByAngle(int targetAngle) {
// Calculate the number of steps required for the specified angle
int steps = targetAngle/1.8;
if(direction==HIGH){
totalsteps+=steps;
}
else{
totalsteps-=steps;
}
// Rotate the stepper motor
for (int i = 0; i < steps; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelay); // Adjust delay for speed
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay); // Adjust delay for speed
}
// Stop the motor after reaching the target angle
motorRunning = false;
currentAngle=0;
Serial.println("Motor stopped after rotation.");
}
// Function to reset the system and return the motor to initial position
void resetSystem() {
int stepDifference = totalsteps%200; // Calculate the angle difference from current position to initial position
Serial.print("stepDifference set to: ");
Serial.println(stepDifference);
direction = !direction; // Toggle direction
digitalWrite(dirPin, direction);
rotateByAngle(stepDifference*1.8); // Rotate the motor to return to initial position (angle 0)
Serial.println("Motor returned to initial position.");
stepDelay = 10000;
currentAngle = 0;
motorRunning = false;
angleMode = false;
direction = HIGH;
res=1;
totalsteps=0;
}
// Function to rotate the stepper motor with the current speed
void rotateBySpeed() {
// Rotate the stepper motor continuously with the current speed
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelay); // Adjust delay for speed
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay); // Adjust delay for speed
}