// Pins for Stepper 1
const int stepPin = 2; // Stepper 1 Step pin
const int dirPin = 5; // Stepper 1 Direction pin
// Pins for Stepper 2
const int stepPin2 = 3; // Stepper 2 Step pin
const int dirPin2 = 6; // Stepper 2 Direction pin
const int enPin = 8; // Enable pin for both motors
// Variables for Rotatry Encoder
int clkPin = 9; // Rotary Encoder Clock Pin
int dtPin = 10; // Rotary Encoder DT pin
int buttonPin = 11; // Rotary Encoder Button pin
int encPosition = 0; // Variable to store encoder postion
int clkPinLast = LOW; // Variable to store last clk state
int currentClk = LOW; // variable to store current clck state
int maxKnob = 20; // Max knob rotations it needs to work
bool rotDir = HIGH; // Motor Direction High Clockwise, LOW CCW
int maxSpeed = 1000; // Max speed of motor
int minSpeed = 2400; // Min speed of motor
int stepperSpeed = minSpeed; // Inital speed of motor
unsigned long lastButtonPress = 0; // Variable to store last button press time
void setup() {
Serial.begin(9600);
pinMode (clkPin, INPUT); // Set pin mode of clk pin as input
pinMode (dtPin, INPUT); // Set pin mode of dt as input
pinMode(buttonPin, INPUT_PULLUP); // Set pin mode of button as input with pullup
// Initialize all motor pins as output
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(dirPin2, OUTPUT);
pinMode(enPin, OUTPUT);
// Enable the motor
digitalWrite(enPin, HIGH);
// Set initial direction of motors as clockwise
digitalWrite(dirPin, rotDir); // Rotate motor in Clockwise direction
digitalWrite(dirPin2, rotDir); // Rotate motor in Clockwise direction
}
void loop() {
takeEncoderInput(); // Check the input of rotatry encoder
motorStep(1); // Call the motor step function and take 1 step
}
void motorStep(int MAX) {
for (int i = 0; i < MAX; i++) {
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(stepperSpeed);
digitalWrite(stepPin, LOW);
digitalWrite(stepPin2, LOW);
delayMicroseconds(stepperSpeed);
}
}
void takeEncoderInput() {
currentClk = digitalRead(clkPin);
if (stepperSpeed > minSpeed - 200) {
Serial.println("Stop");
digitalWrite(enPin, HIGH);
}
else {
digitalWrite(enPin, LOW);
} if (digitalRead(buttonPin) == LOW) {
if (millis() - lastButtonPress > 50) {
rotDir = (rotDir == LOW) ? HIGH : LOW; // check previos direction of motor and change it
digitalWrite(dirPin, rotDir); // Change Motor Direction of rotation
digitalWrite(dirPin2, rotDir); // Change Motor Direction of rotation
Serial.println(rotDir);
Serial.println("Button pressed!");
}
// Remember last button press time
lastButtonPress = millis();
}
if ((clkPinLast == LOW) && (currentClk == HIGH)) {
if (digitalRead(dtPin) == LOW) {
encPosition--;
} else {
encPosition++;
}
if ( encPosition > maxKnob ) {
encPosition = maxKnob;
}
else if ( encPosition < 0 ) {
encPosition = 0;
}
int x = encPosition * 100;
x = minSpeed - x;
if (x < maxSpeed){
x = maxSpeed;
}
// x = (x < maxSpeed) ? maxSpeed : x;
stepperSpeed = x;
Serial.println ( x );
}
clkPinLast = currentClk;
}