/*
Arduino | coding-help
Stepper seems to vibrate unnaturally
E 01 X — 6/28/24 at 9:21 AM
I am working on a project that allows a single stepper motor
to be controlled by a 3 position switch to choose the rotating
direction, a potentiometer to control the speed and 3 LEDs to
show the status of these actions
The code I ended up using works but barely
I have the feeling there is somekind of interference or
something with the motor which causes it to vibrate while
rotating and I think it has something to do with my code;
When I remove a select part of the code the stepper rotates as normal
Anyone that can help me troubleshoot?
*/
// Pin definitions
const int STEP_PIN = 11; // Pin to generate step signal
const int DIR_PIN = 10; // Pin to set direction
const int EN_PIN = 12; // Pin to enable/disable the stepper driver
const int L_DIR_PIN = 4; // Left DIRECTIONAL PIN
const int R_DIR_PIN = 3; // Right DIRECTIONAL PIN
const int EN_SW_PIN = 2; // for test - 3-way switch emulation
const int EN_LED_PIN = 5; // LED for stepper enabled
const int DIR_LED_PIN = 6; // LED for direction indication
const int PWR_LED_PIN = 7; // LED for system power
const int POT_PIN = A0; // Pin connected to potentiometer
/**
Function to generate a step signal to the stepper driver
@param delayTime - The delay between steps to control speed
*/
void stepMotor(int delayTime) {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(delayTime);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(delayTime);
}
void setup() {
Serial.begin(115200);
// Initialize pin modes
pinMode(EN_SW_PIN, INPUT_PULLUP);
pinMode(L_DIR_PIN, INPUT_PULLUP);
pinMode(R_DIR_PIN, INPUT_PULLUP);
//pinMode(POT_PIN, INPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(EN_PIN, OUTPUT);
//pinMode(PWR_LED_PIN, OUTPUT);
pinMode(EN_LED_PIN, OUTPUT);
pinMode(DIR_LED_PIN, OUTPUT);
// Disable the stepper driver on boot
digitalWrite(EN_PIN, HIGH);
// Turn on the power LED - there is one on the board...
//digitalWrite(PWR_LED_PIN, HIGH);
// Indicate Arduino has fully booted up - why?
// Turn off enabled indicator
digitalWrite(EN_LED_PIN, LOW);
// Turn off direction indicator
digitalWrite(DIR_LED_PIN, LOW);
}
void loop() {
// Read the potentiometer value
int potValue = analogRead(POT_PIN);
// Map the potentiometer value to a delay range for speed control
int delayTime = map(potValue, 0, 1023, 5000, 1000); // Adjust the range as needed
// Read the switch position
int switchL = digitalRead(L_DIR_PIN);
int switchR = digitalRead(R_DIR_PIN);
// for test in sim
int switchEn = digitalRead(EN_SW_PIN);
digitalWrite(DIR_LED_PIN, switchL ? LOW : HIGH);
if (switchEn == LOW) { // Enable switch is on
//if (switchL && switchR) {} // if switch is in the middle
digitalWrite(EN_PIN, LOW);
digitalWrite(EN_LED_PIN, HIGH);
if (switchL == LOW) {
digitalWrite(DIR_PIN, HIGH);
//digitalWrite(DIR_LED_PIN, HIGH);
}
if (switchR == LOW) {
digitalWrite(DIR_PIN, LOW);
//digitalWrite(DIR_LED_PIN, LOW);
}
stepMotor(delayTime);
} else { // Enable is off
// Switch is in the middle position (OFF)
digitalWrite(EN_PIN, HIGH);
digitalWrite(EN_LED_PIN, LOW);
}
}
Speed
ENable
CW/CCW