needing help with stepper speed control

hi. so i have this code i'm working on for an automated metal cutting saw and i'm trying to get it finished up and ran into a small issue and don't know what i'm doing wrong.

in short the code waits for me to press a button to start going home. Once it arrives at home it will start the saw and stepper motor which will bounce between two limit switches. I installed a 10K pot to allow me to change the speed of the stepper motor, but it's giving me problems. I included it in the loop and it works to a degree.... like when i first turn it on, wherever the pot is positioned, that's what it sets the speed as, but it does not allow me to change it on the fly unless i reset the arduino and then it changes the speed to the position of the pot. can someone please tell me what i'm doing wrong..

and fyi. the stopStep() at the bottom isn't working at the moment because of the while(digitalRead(next limit switch)) is blocking me from activating the button, but that will be changed an interrupt to pause the saw and stepper in the event there's a problem, which i'm about to do, but right now i'm just concerned about the Pot and speed control

thanks

#include <Stepper.h>

                    
Stepper stepper1(200, 6, 5);


                  
   const int stepPin = 5;      // stepper1 step pin
   const int dirPin  = 6;      // stepper1 direction pin 
   const int enPin = 4;        // stepper1 enable pin
   const int relay =7;         // relay enable pin
   

   const byte front_Switch = 8;    // frontStep switch
   const byte back_Switch = 9;     // backStep switch
   const byte stop_Switch = 1;   // cutting switch
 
   
   byte switch1State;              // cutting switch state
   byte switch2State;             // frontStep switch state
   byte switch3State;            // stopstep state


   int stepSpeed;
 

void setup() {
 
  Serial.begin(115200);
  
  pinMode(enPin, OUTPUT);                // stepper1 enable output
  pinMode(relay, OUTPUT);               // stepper2 enable output
  pinMode( stop_Switch, INPUT_PULLUP);     // stop switch mode
  pinMode( front_Switch, INPUT_PULLUP);     // frontStep switch mode
  pinMode( back_Switch, INPUT_PULLUP);  // backStep switch mode
  digitalWrite(enPin, HIGH);             // stepper1 enable = high
  digitalWrite(relay, LOW);            // relay enable = low, off

 //     Serial.println(" ");
 //     Serial.println(" Press Stop To Begin");
 //     Serial.println(" ");
 
//  while (digitalRead(stop_Switch)){  }

//      Serial.println(" Going Home");
 //     Serial.println(" ");
 //     digitalWrite(enPin, HIGH);
      
   while (digitalRead(front_Switch)) {     //CCW rotation
                                         
       stepper1.step(-1);
       delayMicroseconds(400);
          
    }
                 
}
 
void loop() {

   int sensorReading= analogRead(A0);
  int stepSpeed = map(sensorReading, 5, 1023, 100, 750);
   if (stepSpeed > 0) {
    stepper1.setSpeed(stepSpeed);
   }  


  
  checkButtons();              // read buttons status
  frontStep();                 // step  forward
  backStep();                  // step  backward
  stopStep();                   // cutting stop
   
}

void checkButtons() {



  switch1State = digitalRead(front_Switch);  // reading frontStep button
  switch2State = digitalRead(back_Switch);  //reading backStep button
  switch3State = digitalRead(stop_Switch);   // reading stop button

}

void frontStep(){   // front cutting

  if (switch1State == LOW){     // if front_Switch button activated

   while(digitalRead(back_Switch)) {
   
     digitalWrite(enPin, HIGH);
     digitalWrite(relay, HIGH);
     stepper1.step(1);
     delayMicroseconds(stepSpeed);
    
   }       
  }
 }


void backStep(){         //CCW rotation step counting

   if (switch2State==LOW)  {     // if back_Switch button activated

    while(digitalRead(front_Switch)) {
    
     digitalWrite(enPin, HIGH);
     digitalWrite(relay, HIGH);
     stepper1.step(-1);
     delayMicroseconds(stepSpeed);

   }
  } 
          
} 


void stopStep() {       // stop step and cutting
      
   if (switch3State == LOW){       // if stop switch activated

          
       digitalWrite(relay, LOW);
       digitalWrite(enPin, LOW);
 }
}  
    
   

 [\code]

Your while statements in the last two functions are not going to return until you have got to one end or other. Therefore during this time your code never reads the pot to change the speed.

So read the pot and change the speed in each of those blocks.

yeah. I just discovered that myself. I just needed a cookie to figure that out.. :slight_smile:

thanks

Ok. so i made the changes and now the pot seems to be acting really funny. when i had the speed code in the loop by itself the pot seemed to work. One direction was slow, and the other direction was fast, but the changes didn't happen until it came out of the while loop. I can understand that. But now after putting the speed code within the 2 while loops, it's no longer working correctly. Both ends of the pot is slow, one slower than the other, and then ramps up speed in the middle of the pot, but not as fast as it should be going

i even changed it back to the way it was before thinking maybe the pot is bad, and it acts the same as it did so the pot is fine. any ideas why it could be doing that

the only thing i can think of is the arduino isn't capable of reading the pot and counting steps at the same time, but i've seen other code similar work fine

#include <Stepper.h>

                    
Stepper stepper1(200, 6, 5);


                  
   const int stepPin = 5;      // stepper1 step pin
   const int dirPin  = 6;      // stepper1 direction pin 
   const int enPin = 4;        // stepper1 enable pin
   const int relay =7;         // relay enable pin
   

   const byte front_Switch = 8;    // frontStep switch
   const byte back_Switch = 9;     // backStep switch
   const byte stop_Switch = 1;   // cutting switch
 
   
   byte switch1State;              // cutting switch state
   byte switch2State;             // frontStep switch state
   byte switch3State;            // homeSwitch state
//   byte switch4State;           // backStep state

//   byte lastSwitch2State=HIGH;
//   byte lastSwitch4State=HIGH;

   int long stepSpeed;
 

void setup() {
 
  Serial.begin(115200);
  
  pinMode(enPin, OUTPUT);                // stepper1 enable output
  pinMode(relay, OUTPUT);               // stepper2 enable output
  pinMode( stop_Switch, INPUT_PULLUP);     // stop switch mode
  pinMode( front_Switch, INPUT_PULLUP);     // frontStep switch mode
  pinMode( back_Switch, INPUT_PULLUP);  // backStep switch mode
  digitalWrite(enPin, HIGH);             // stepper1 enable = high
  digitalWrite(relay, LOW);            // relay enable = low, off

 //     Serial.println(" ");
 //     Serial.println(" Press Stop To Begin");
 //     Serial.println(" ");
 
//  while (digitalRead(stop_Switch)){  }

//      Serial.println(" Going Home");
 //     Serial.println(" ");
 //     digitalWrite(enPin, HIGH);
      
   while (digitalRead(front_Switch)) {     //CCW rotation
                                         
       stepper1.step(-1);
       delayMicroseconds(400);
          
    }
                 
}
 
void loop() {


 
  checkButtons();              // read buttons status
  frontStep();                 // step  forward
  backStep();                  // step  backward
  stopStep();                   // cutting stop
   
}

void checkButtons() {



  switch1State = digitalRead(front_Switch);  // reading frontStep button
  switch2State = digitalRead(back_Switch);  //reading backStep button
  switch3State = digitalRead(stop_Switch);   // reading stop button

}

void frontStep(){   // front cutting

  if (switch1State == LOW){     // if front_Switch button activated

   while(digitalRead(back_Switch)) {

  int sensorReading= analogRead(A0);
  int stepSpeed = map(sensorReading, 10, 1023, 100, 750);
   if (stepSpeed > 0) {
    stepper1.setSpeed(stepSpeed);
   }
     digitalWrite(enPin, HIGH);
     digitalWrite(relay, HIGH);
     stepper1.step(1);
     delayMicroseconds(stepSpeed);
    
   }       
  }
 }


void backStep(){         //CCW rotation step counting

   if (switch2State==LOW)  {     // if back_Switch button activated

    while(digitalRead(front_Switch)) {

  int sensorReading= analogRead(A0);
  int stepSpeed = map(sensorReading, 10, 1023, 100, 750);
   if (stepSpeed > 0) {
    stepper1.setSpeed(stepSpeed);
   }
    
     digitalWrite(enPin, HIGH);
     digitalWrite(relay, HIGH);
     stepper1.step(-1);
     delayMicroseconds(stepSpeed);

   }
  } 
          
} 


void stopStep() {       // stop step and cutting
      
   if (switch3State == LOW){       // if stop switch activated

          
       digitalWrite(relay, LOW);
       digitalWrite(enPin, LOW);
 }
}  
    
   

 [\code]

ok. well it seems my hunch was correct. my arduino didn't like reading the analog value and counting 1 step at a time at the higher speeds.. If i change it to 10 or 100 steps at a time, everything works correctly.

Glad you got it working.

I notice that your code uses a lot of int declarations a loop. This is not good because each time the code sees the int it generates a whole new variable. This will slow things down and could lead to memory problems.

Declare any variables at the start of a function only, or once outside a function if you want them to be global variables.