// 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 buttonPin = 2; // Start button pin
const int buttonPinOff = 4; // Stop button pin
const int potPinSpeed = A0; // Potentiometer pin for speed control
const int potPinRest = A1; // Potentiometer pin for rest time control
const int dirChange = 7; // Direction change button pin
// Define variables
int stepDelay = 1000; // Initial delay between steps in microseconds
int restTime = 0; // Initial rest time between rotations in milliseconds
bool motorRunning = false; // Flag to track motor state
void setup() {
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Internal pull-up resistor
pinMode(buttonPinOff, INPUT_PULLUP); // Internal pull-up resistor
pinMode(dirChange, INPUT_PULLUP);
// Set initial direction (clockwise)
digitalWrite(dirPin, HIGH);
// Initialize Serial communication
Serial.begin(9600);
Serial.println("{ \"bounce\": \"0\" }");
Serial.println("{ \"arrow\": \"orange\" }");
Serial.println("Enter the speed and rest time if you want to change digitally: ");
}
void loop() {
// Read the state of the start button
const int dirState = digitalRead(dirChange);
if (digitalRead(buttonPin) == LOW && !motorRunning) {
// Start the motor
motorRunning = true;
Serial.println("Motor started");
digitalWrite(dirPin, HIGH); // Set direction (clockwise)
}
// Read the state of the stop button
if (digitalRead(buttonPinOff) == LOW && motorRunning) {
// Stop the motor
motorRunning = false;
Serial.println("Motor stopped");
}
// Change direction if the button is pressed
if (dirState == LOW) {
if (dirState == LOW) {
const int previousDirection = digitalRead(dirPin);
digitalWrite(dirPin, previousDirection == HIGH ? LOW : HIGH);
Serial.println("Direction Changed");
}
}
// If motor is running, rotate the stepper motor
if (motorRunning) {
// Read analog input from potentiometer to control speed
int potValueSpeed = analogRead(potPinSpeed);
stepDelay = map(potValueSpeed, 0, 1023, 1000, 100); // Adjust the range for speed
// Read analog input from potentiometer to control rest time
// int potValueRest = analogRead(potPinRest);
// restTime = map(potValueRest, 0, 1023, 1000, 5000); // Adjust the range for rest time
// Check for digital input to change speed or rest time
while (Serial.available() > 0) {
int temp = Serial.parseInt();
Serial.print("Received input: ");
Serial.println(temp);
if (temp != 0) {
restTime=temp;
}
}
// Execute the motor steps with the current speed
for (int i = 0; i < 200; i++) { // 200 steps for one revolution (adjust as needed)
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelay); // Adjust delay for speed
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay); // Adjust delay for speed
}
Serial.print("Speed delay: ");
Serial.println(stepDelay);
// Pause between rotations
Serial.print("rest time: ");
Serial.println(restTime);
delay(restTime);
}
}