#include <AccelStepper.h>
#define ENCODER_CLK 2
#define ENCODER_DT 3
// Motor configuration
#define stepsPerRevolution 5
#define MOTOR_STEP_PIN 5
#define MOTOR_DIR_PIN 4
#define MOTOR2_STEP_PIN 6
#define MOTOR2_DIR_PIN 4
#define MOTOR3_STEP_PIN 7
#define MOTOR3_DIR_PIN 4
#define MOTOR4_STEP_PIN 8
#define MOTOR4_DIR_PIN 4
AccelStepper motor(AccelStepper::DRIVER, MOTOR_STEP_PIN, MOTOR_DIR_PIN);
AccelStepper motor2(AccelStepper::DRIVER, MOTOR2_STEP_PIN, MOTOR2_DIR_PIN);
AccelStepper motor3(AccelStepper::DRIVER, MOTOR3_STEP_PIN, MOTOR3_DIR_PIN);
AccelStepper motor4(AccelStepper::DRIVER, MOTOR4_STEP_PIN, MOTOR4_DIR_PIN);
void setup() {
Serial.begin(9400);
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
motor.setMaxSpeed(1000.0);
motor.setAcceleration(500.0);
}
int lastClk = HIGH;
int encoderSteps = 0;
void loop() {
int newClk = digitalRead(ENCODER_CLK);
if (newClk != lastClk) {
// There was a change on the CLK pin
lastClk = newClk;
int dtValue = digitalRead(ENCODER_DT);
if (newClk == LOW && dtValue == HIGH) {
encoderSteps++;
Serial.println(encoderSteps);
digitalWrite(MOTOR_DIR_PIN, HIGH);
digitalWrite(MOTOR2_DIR_PIN, HIGH);
digitalWrite(MOTOR3_DIR_PIN, HIGH);
digitalWrite(MOTOR4_DIR_PIN, HIGH);
if (encoderSteps >= 21) {
encoderSteps = 1; // Reset to 0 after 21 steps
}
}
if (newClk == LOW && dtValue == LOW) {
encoderSteps--;
Serial.println(encoderSteps);
digitalWrite(MOTOR_DIR_PIN, LOW);
digitalWrite(MOTOR2_DIR_PIN, LOW);
digitalWrite(MOTOR3_DIR_PIN, LOW);
digitalWrite(MOTOR4_DIR_PIN, LOW);
if (encoderSteps <= 0) {
encoderSteps = 20; // Reset to 21 after 1 step
}
}
// Control the motor based on encoder steps
if (encoderSteps >= 1 && encoderSteps <= 5) {
for (int i = 0; i < stepsPerRevolution; i++){
digitalWrite(MOTOR_STEP_PIN, LOW);
digitalWrite(MOTOR2_STEP_PIN, LOW);
digitalWrite(MOTOR3_STEP_PIN, HIGH);
digitalWrite(MOTOR4_STEP_PIN, HIGH);
delayMicroseconds(2000);
digitalWrite(MOTOR_STEP_PIN, LOW);
digitalWrite(MOTOR2_STEP_PIN, LOW);
digitalWrite(MOTOR3_STEP_PIN, LOW);
digitalWrite(MOTOR4_STEP_PIN, LOW);
delayMicroseconds(2000);
}
} else if(encoderSteps >= 5 && encoderSteps <= 10){
for (int i = 0; i < stepsPerRevolution; i++){
digitalWrite(MOTOR_STEP_PIN, LOW);
digitalWrite(MOTOR2_STEP_PIN, HIGH);
digitalWrite(MOTOR3_STEP_PIN, HIGH);
digitalWrite(MOTOR4_STEP_PIN, LOW);
delayMicroseconds(2000);
digitalWrite(MOTOR_STEP_PIN, LOW);
digitalWrite(MOTOR2_STEP_PIN, LOW);
digitalWrite(MOTOR3_STEP_PIN, LOW);
digitalWrite(MOTOR4_STEP_PIN, LOW);
delayMicroseconds(2000);
}
}if (encoderSteps >= 11 && encoderSteps <= 15) {
for (int i = 0; i < stepsPerRevolution; i++){
digitalWrite(MOTOR_STEP_PIN, HIGH);
digitalWrite(MOTOR2_STEP_PIN, HIGH);
digitalWrite(MOTOR3_STEP_PIN, LOW);
digitalWrite(MOTOR4_STEP_PIN, LOW);
delayMicroseconds(2000);
digitalWrite(MOTOR_STEP_PIN, LOW);
digitalWrite(MOTOR2_STEP_PIN, LOW);
digitalWrite(MOTOR3_STEP_PIN, LOW);
digitalWrite(MOTOR4_STEP_PIN, LOW);
delayMicroseconds(2000);
}
} if (encoderSteps >= 16 && encoderSteps <= 20) {
for (int i = 0; i < stepsPerRevolution; i++){
digitalWrite(MOTOR_STEP_PIN, HIGH);
digitalWrite(MOTOR2_STEP_PIN, LOW);
digitalWrite(MOTOR3_STEP_PIN, LOW);
digitalWrite(MOTOR4_STEP_PIN, HIGH);
delayMicroseconds(2000);
digitalWrite(MOTOR_STEP_PIN, LOW);
digitalWrite(MOTOR2_STEP_PIN, LOW);
digitalWrite(MOTOR3_STEP_PIN, LOW);
digitalWrite(MOTOR4_STEP_PIN, LOW);
delayMicroseconds(2000);
}
} else {
// Motor stops
motor.setSpeed(0);
motor.runSpeed();
}
}
}