/*
  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?
*/

const int MIN_SPEED = 5000; // larger values are slower
const int MAX_SPEED = 1000;

// Pin definitions
const int DIR_PIN =    10;  // stepper direction pin
const int ENA_PIN =    12;  // stepper enable pin
const int STEP_PIN =   11;  // stepper step pin
const int DIR_LED_PIN = 8;  // LED for direction indication
const int ENA_LED_PIN = 9;  // LED for stepper enabled
const int L_DIR_PIN =   7;  // CCW direction switch
const int R_DIR_PIN =   6;  // CW direction switch
const int POT_PIN =    A0;  // potentiometer pin

/**
   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(DIR_PIN, OUTPUT);
  pinMode(ENA_PIN, OUTPUT);
  pinMode(STEP_PIN, OUTPUT);
  pinMode(ENA_LED_PIN, OUTPUT);
  pinMode(DIR_LED_PIN, OUTPUT);
  pinMode(L_DIR_PIN, INPUT_PULLUP);
  pinMode(R_DIR_PIN, INPUT_PULLUP);
  // Disable the stepper driver on boot
  digitalWrite(ENA_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, MIN_SPEED, MAX_SPEED); // Adjust the range as needed

  // Read the switch position
  int cwSw = digitalRead(R_DIR_PIN);
  int ccwSw = digitalRead(L_DIR_PIN);

  if (!cwSw || !ccwSw)  {  // switch is not in the middle
    if (ccwSw == LOW) { // switch is to the left
      digitalWrite(DIR_PIN, HIGH);
      digitalWrite(DIR_LED_PIN, HIGH);
    } else {            // else it's to the right
      digitalWrite(DIR_PIN, LOW);
      digitalWrite(DIR_LED_PIN, LOW);
    }
    // now that direction is set...
    digitalWrite(ENA_PIN, LOW);
    digitalWrite(ENA_LED_PIN, HIGH);
    stepMotor(delayTime);
  } else  {               // switch is in the middle (OFF)
    digitalWrite(ENA_PIN, HIGH);
    digitalWrite(ENA_LED_PIN, LOW);
    digitalWrite(DIR_LED_PIN, LOW);
  }
}

/*
// ORIGINAL loop
void loop() {
  // Read the potentiometer value
  int potValue = analogRead(potPin);
  // Map the potentiometer value to a delay range for speed control
  int delayTime = map(potValue, 0, 1023, 1000, 200); // Adjust the range as needed

  // Read the switch position
  int switchL = digitalRead(switchPinL);
  int switchR = digitalRead(switchPinR);
  
  if (switchL == LOW) {

    digitalWrite(enablePin, HIGH);
    digitalWrite(ledDirection, LOW);

  }

  if (switchR == LOW) {

    digitalWrite(enablePin, HIGH);
    digitalWrite(ledDirection, LOW);
  }

  // Control motor direction and speed based on switch position LEFT
  if (switchL == HIGH) {
    // Move motor to the right
    digitalWrite(enablePin, LOW);
    digitalWrite(dirPin, HIGH);
    digitalWrite(ledDirection, HIGH); // Turn on direction LED
    stepMotor(delayTime);
    } if (switchL == LOW) {
    // Switch is in the middle position (OFF)
      digitalWrite(enablePin, HIGH);
    digitalWrite(ledDirection, LOW); // Turn off direction LED
  }

  // Control motor direction and speed based on switch position RIGHT
    if (switchR == HIGH) {
    // Move motor to the right
    digitalWrite(enablePin, LOW);
    digitalWrite(dirPin, LOW);
    digitalWrite(ledDirection, HIGH); // Turn on direction LED
    stepMotor(delayTime);
  } if (switchR == LOW) {
    // Switch is in the middle position (OFF)
      digitalWrite(enablePin, HIGH);
    digitalWrite(ledDirection, LOW); // Turn off direction LED
  }

}
*/
$abcdeabcde151015202530fghijfghij
A4988
Speed
L <> R
Vm En Dir