Stepper motor - stepping increment question

Hi all,

I am very new to the Arduino world.
Helping a friend on a stepper motor project, that is how I discovered Arduino.
Have some programming background but not in C/C++
I am stuck with a probably very easy problem but since the time is short I had to ask you guys for help.

Currently I have an Arduino mega, Nema23 stepper and TB6600 controller.
Controller is set to 3200 pulses/rev.
I found some codes online and trying to figure out how they work and how to adapt to the task I need.

The task should do the following:

  • When the power is on, the stepper motor homes using the limit switch (that portion works fine) and waits.
  • There is a button which after it’s pressed will turn the stepper 40 degrees. (this portion works as well)
  • After the stepper is at 40deg, it should wait 5 seconds, turn on the LED for 10 seconds, turn off the LED, wait 5 seconds and move another 40 degrees. This should be repeated 8 more times until the stepper makes a full revolution.
    So technically after the stepper motor homes and you press the start button, it should turn 40 deg each time and do the LED ON/OFF sequence until it makes the full revolution.

I can’t figure out how to do the LED and the next stepping sequence.

Please help.

The code I found and modified:

/*  Motor Homing code using AccelStepper and the Serial Monitor
 
Created by Yvan / https://Brainy-Bits.com
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!

*/

#include "AccelStepper.h" 
// Library created by Mike McCauley at http://www.airspayce.com/mikem/arduino/AccelStepper/

// AccelStepper Setup
AccelStepper stepperX(1, 2, 3);   // 1 = Easy Driver interface
                                  // NANO Pin 2 connected to STEP pin of Easy Driver
                                  // NANO Pin 3 connected to DIR pin of Easy Driver

// Define the Pins used
#define home_switch 8 // Pin 8 connected to Home Switch (MicroSwitch)
#define StartButton 9 // Start the rotating sequence
#define LED 11


// Stepper Travel Variables
long TravelX;  // Used to store the X value entered in the Serial Monitor
int move_finished=1;  // Used to check if move is completed
long initial_homing=-1;  // Used to Home Stepper at startup
int PPR=3200;  // Pulse Per Revolution. Value based on the controller setup
const float resolution = PPR/36;  // Calculating 1 rotational degree
const float deg = resolution*4+3;
int curPosition = 0;

void setup() {
   Serial.begin(9600);  // Start the Serial monitor with speed of 9600 Bauds
   
   pinMode(home_switch, INPUT_PULLUP);
   pinMode(StartButton, INPUT_PULLUP); 
   pinMode (LED, OUTPUT) ;
 
   
   delay(5);  // Wait for EasyDriver wake up

   //  Set Max Speed and Acceleration of each Steppers at startup for homing
  stepperX.setMaxSpeed(100.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepperX.setAcceleration(100.0);  // Set Acceleration of Stepper
 

// Start Homing procedure of Stepper Motor at startup

  Serial.print("Stepper is Homing . . . . . . . . . . . ");
Serial.println(deg);
  while (digitalRead(home_switch)) {  // Make the Stepper move CCW until the switch is activated   
    stepperX.moveTo(initial_homing);  // Set the position to move to
    initial_homing--;  // Decrease by 1 for next move if needed
    stepperX.run();  // Start moving the stepper
    delay(5);
}

  stepperX.setCurrentPosition(0);  // Set the current position as zero for now
  stepperX.setMaxSpeed(100.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepperX.setAcceleration(100.0);  // Set Acceleration of Stepper
  initial_homing=1;

  while (!digitalRead(home_switch)) { // Make the Stepper move CW until the switch is deactivated
    stepperX.moveTo(initial_homing);  
    stepperX.run();
    initial_homing++;
    delay(5);
  }
  
  stepperX.setCurrentPosition(0);
  Serial.println("Homing Completed");
  Serial.println("");
  stepperX.setMaxSpeed(1000.0);      // Set Max Speed of Stepper (Faster for regular movements)
  stepperX.setAcceleration(1000.0);  // Set Acceleration of Stepper

// Print out Instructions on the Serial Monitor at Start
  Serial.println("Enter Travel distance (Positive for CW / Negative for CCW and Zero for back to Home): ");
}

void loop() {

  
if (digitalRead(StartButton) == LOW){
stepperX.moveTo(deg); //rotate 40 degree

  digitalWrite(LED, HIGH);
  delay (5000);
  digitalWrite(LED, LOW);  
  }
 while (Serial.available()>0)  { // Check if values are available in the Serial Buffer

  move_finished=0;  // Set variable for checking move of the Stepper
   TravelX= Serial.parseInt();  // Put numeric value from buffer in TravelX variable
  if (TravelX < 0 || TravelX > 3200) {  // Make sure the position entered is not beyond the HOME or MAX position
    Serial.println("");
    Serial.println("Please enter a value greater than zero and smaller or equal to 3200.....");
    Serial.println("");
  } else {
    Serial.print("Moving stepper into position: ");
    Serial.println(TravelX);
  
  stepperX.moveTo(TravelX);  // Set new moveto position of Stepper
  
  delay(1000);  // Wait 1 seconds before moving the Stepper
  }
 } 
 if (TravelX >= 0 && TravelX <= 3200) {

// Check if the Stepper has reached desired position
  if ((stepperX.distanceToGo() != 0)) {
    
    stepperX.run();  // Move Stepper into position
    
  }

// If move is completed display message on Serial Monitor
  if ((move_finished == 0) && (stepperX.distanceToGo() == 0)) {
    Serial.println("COMPLETED!");
    Serial.println("");
     Serial.println("Enter Travel distance (Positive for CW / Negative for CCW and Zero for back to Home): ");
    move_finished=1;  // Reset move variable
  }
  }

 
}

Thank you.

You kept tons of code (Serial stuff) from an example that is not part of your requirements. Get rid of it.

Basically the setup() should configure the stepper, go to home position and set position as 0, wait for the button click.

You could have a global counter variable set to 0

The loop should just be checking
if the counter is less than 9 then
take one 40° trip (set the number of steps as target destination, run to that destination)
Then do the led dance
Then set position as 0 (fake new home)
Then increase the counter,.
Else
Reset counter to 0
Wait until button is pressed again

Just turn your description into code one phrase at a time. If you don’t like the results, fix your description and try again.

void loop()
{
  // There is a button which after it's pressed...
  if (digitalRead(StartButton) == LOW)
  {
    // will turn the stepper [to] 40 degrees.
    deg = 40;
    stepperX.moveTo(deg); //rotate [to] 40 degree
    // After the stepper is at 40deg, ...


    // ... it should wait 5 seconds, ...
    delay (5000);


    // ... turn on the LED for 10 seconds...
    digitalWrite(LED, HIGH);
    delay (10000);


    // ...turn off the LED, ...
    digitalWrite(LED, LOW);


    // ... wait 5 seconds, ...
    delay (5000);


    // ...move another 40 degrees.
    deg += 40;
    stepperX.moveTo(deg); //rotate another 40 degree


    // This should be repeated 8 more times until the stepper makes a full revolution.
    for (int i = 0; i < 8; i++)
    {
      // ...it should wait 5 seconds,...
      delay (5000);


      // ...turn on the LED for 10 seconds...
      digitalWrite(LED, HIGH);
      delay (10000);


      // ...turn off the LED, ...
      digitalWrite(LED, LOW);


      // ... wait 5 seconds, ...
      delay (5000);


      // ...move another 40 degrees.
      deg += 40;
      stepperX.moveTo(deg); //rotate another 40 degree
    }
  }
}

Thank you so much all for help.
I will test the code today and let you know.
A lot to learn though. Thanks again

I finally uploaded the code and cleaned it.

After I deleted everything I had under void loop and replaced with the code from johnwasser, I got the following error: ‘deg’ was not declared in this scope.
I added the int deg at the beginning and it started homing.
After it hit the home it stopped and waited. I pressed the button but the stepper didn’t move 40degrees. However the LED did the sequence 9 times.
What am I missing?
I am not sure if this is important, but after the homing is done, the limit switch is still pressed (high)

Here is the cleared code:

/*  Motor Homing code using AccelStepper and the Serial Monitor
 
Created by Yvan / https://Brainy-Bits.com
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!

*/

#include "AccelStepper.h" 
// Library created by Mike McCauley at http://www.airspayce.com/mikem/arduino/AccelStepper/

// AccelStepper Setup
AccelStepper stepperX(1, 2, 3);   // 1 = Easy Driver interface
                                  // NANO Pin 2 connected to STEP pin of Easy Driver
                                  // NANO Pin 3 connected to DIR pin of Easy Driver

// Define the Pins used
#define home_switch 8 // Pin 8 connected to Home Switch (MicroSwitch)
#define StartButton 9 // Start the rotating sequence
#define LED 11


// Stepper Travel Variables
long TravelX;  // Used to store the X value entered in the Serial Monitor
int move_finished=1;  // Used to check if move is completed
long initial_homing=-1;  // Used to Home Stepper at startup
//int PPR=3200;  // Pulse Per Revolution. Value based on the controller setup
//const float resolution = PPR/36;  // Calculating 1 rotational degree
//const float deg = resolution*4+3;
//int curPosition = 0;

int deg;

void setup() {
   Serial.begin(9600);  // Start the Serial monitor with speed of 9600 Bauds
   
   pinMode(home_switch, INPUT_PULLUP);
   pinMode(StartButton, INPUT_PULLUP); 
   pinMode (LED, OUTPUT) ;
 
   
   delay(5);  // Wait for EasyDriver wake up

   //  Set Max Speed and Acceleration of each Steppers at startup for homing
  stepperX.setMaxSpeed(100.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepperX.setAcceleration(100.0);  // Set Acceleration of Stepper
 

// Start Homing procedure of Stepper Motor at startup

  Serial.print("Stepper is Homing . . . . . . . . . . . ");
//Serial.println(deg);
  while (digitalRead(home_switch)) {  // Make the Stepper move CCW until the switch is activated   
    stepperX.moveTo(initial_homing);  // Set the position to move to
    initial_homing--;  // Decrease by 1 for next move if needed
    stepperX.run();  // Start moving the stepper
    delay(5);
}

  stepperX.setCurrentPosition(0);  // Set the current position as zero for now
  stepperX.setMaxSpeed(100.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepperX.setAcceleration(100.0);  // Set Acceleration of Stepper
  initial_homing=1;

  while (!digitalRead(home_switch)) { // Make the Stepper move CW until the switch is deactivated
    stepperX.moveTo(initial_homing);  
    stepperX.run();
    initial_homing++;
    delay(5);
  }
  
  stepperX.setCurrentPosition(0);
  Serial.println("Homing Completed");
  Serial.println("");
  stepperX.setMaxSpeed(1000.0);      // Set Max Speed of Stepper (Faster for regular movements)
  stepperX.setAcceleration(1000.0);  // Set Acceleration of Stepper
}
void loop(){
   if (digitalRead(StartButton) == LOW)
  {
       deg = 40;
    stepperX.moveTo(deg); //rotate [to] 40 degree
    delay (5000);
    digitalWrite(LED, HIGH);
    delay (10000);
    digitalWrite(LED, LOW);
    delay (5000);
    deg += 40;
    stepperX.moveTo(deg); //rotate another 40 degree
    for (int i = 0; i < 8; i++)
    {
      delay (1000);
      digitalWrite(LED, HIGH);
      delay (2000);
      digitalWrite(LED, LOW);
      delay (1000);
      deg += 40;
      stepperX.moveTo(deg); //rotate another 40 degree
    }
  }
}

Thank you.

This line
stepperX.moveTo(deg);does not move 40 degrees but registers a destination for 40 micro steps out of your 3200... so some math are needed 400 would be a good value (400x9=3600) if you want to complete the circle in 9 equal movements.

But this is the wrong call / that sets a target position but does not move. The method to call is runToNewPosition()

So set deg to 400 (and increment by 400) and try out This line

 stepperX. runToNewPosition(deg);

Thanks J-M-L.

Changed the deg to 355 and replaced with your recommendation and works like a charm.
Thanks guys for the help.
I hope you had a great New Years.

Great

Now if you understood the code instead of hard coding one move and then use a for loop to complete the 8 other movements , you can use the for loop for the 9 movements