My first program and it works. What could I have done better?.

This is my first program and it works but to help me learn pound me with thing I did wrong or things I could do better. Cobe below. The inputs have pull up resistors. I didn’t want to use the internal resistors for the inputs.

#include <AccelStepper.h> // AccelStepper Library

// AccelStepper Setup
AccelStepper stepperX(1, 12, 11);
#define home_switch 22
#define cycle_start_sw 24

static int  switch_count = 1;
static int  index_count = 1;
static int loop_count = 31;
static long Next_Move = 2222;
static long wood_length = 52070;
long homing_steps = -1;

void setup()  {
//Stop bootup output trigger

pinMode(26, OUTPUT);
pinMode(28, OUTPUT);
pinMode(30, OUTPUT);

digitalWrite(26, HIGH);
digitalWrite(28, HIGH);
digitalWrite(30, HIGH);

pinMode(home_switch, INPUT);
pinMode(cycle_start_sw, INPUT);
delay(1000);

//  Set Max Speed and Acceleration for homing
stepperX.setMaxSpeed(15000.0);
stepperX.setAcceleration(15000.0);

// Start Homing procedure of Stepper Motor at startup
while (digitalRead(home_switch)) {  // Stepper CCW until the home switch is activated
  stepperX.move(-1000);  // Steps to move
  stepperX.runSpeedToPosition();
}

stepperX.setCurrentPosition(0);  // Set current position to zero

//  Set Max Speed and Acceleration for moving off home switch
stepperX.setMaxSpeed(100.0);
stepperX.setAcceleration(100.0);
homing_steps = 1;

delay(500);
while (!digitalRead(home_switch)) { // Stepper CW until off home switch
  stepperX.moveTo(homing_steps);
  stepperX.run();
  homing_steps++;
  delay(10);
}

stepperX.setCurrentPosition(0); // Set current position to zero

//  Set Max Speed and Acceleration for moving to start position
delay(500);
stepperX.setMaxSpeed(6250.0);
stepperX.setAcceleration(500.0);

// Move to start position
stepperX.move(wood_length);
stepperX.runToPosition();
//while (run());
delay(500);

stepperX.setCurrentPosition(0); // Set start position to zero

}

void loop() {

while (digitalRead(cycle_start_sw)) {
  delay(10);
}

//  Set Max Speed and Acceleration for moving in loop
stepperX.setMaxSpeed(3000.0);
stepperX.setAcceleration(300.0);
index_count = 1;

while (loop_count >= index_count) {

  stepperX.move(Next_Move);  // new move to position
  stepperX.runToPosition();
  delay(500);
  index_count ++;
  digitalWrite(30, LOW);
  digitalWrite(28, LOW);
  delay(2000);
  digitalWrite(26, LOW);
  delay(2000);
  digitalWrite(28, HIGH);
  delay(1000);
  digitalWrite(26, HIGH);
  digitalWrite(30, HIGH);
}

//  Set Max Speed and Acceleration for homing
stepperX.setMaxSpeed(15000.0);
stepperX.setAcceleration(15000.0);

// Start Homing procedure of Stepper Motor at startup
while (digitalRead(home_switch)) {  // Stepper CCW until the home switch is activated
  stepperX.move(-1000);  // Steps to move
  stepperX.runSpeedToPosition();
}

stepperX.setCurrentPosition(0);  // Set current position to zero

//  Set Max Speed and Acceleration for moving off home switch
stepperX.setMaxSpeed(100.0);
stepperX.setAcceleration(100.0);
homing_steps = 1;

delay(500);
while (!digitalRead(home_switch)) { // Stepper CW until off home switch
  stepperX.moveTo(homing_steps);
  stepperX.run();
  homing_steps++;
  delay(10);
}

stepperX.setCurrentPosition(0); // Set current position to zero

//  Set Max Speed and Acceleration for moving to start position
delay(500);
stepperX.setMaxSpeed(6250.0);
stepperX.setAcceleration(500.0);

// Move to start position
stepperX.move(wood_length);
stepperX.runToPosition();
//while (run());
delay(500);

stepperX.setCurrentPosition(0); // Set start position to zero
}

You could have read the forum instructions and posted code in code tags...

Also it is hard to improve a program without any information about what it does.

Why is the first variable declared static, and not the second?

static long wood_length = 52070;
long homing_steps = -1;
  pinMode(26, OUTPUT);
  pinMode(28, OUTPUT);
  pinMode(30, OUTPUT);

  digitalWrite(26, HIGH);
  digitalWrite(28, HIGH);
  digitalWrite(30, HIGH);

You could have used names for the pins rather than anonymous numbers.

  while (digitalRead(cycle_start_sw)) {
    delay(10);
  }

Why delay?

Now that it works, rewrite the whole thing without using delay() or while().

Study the 'State Machine' programming technique.

http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html

http://www.gammon.com.au/blink

To save SRAM, use the smallest 'type' needed.

Use 'const' instead of #define

You need to insert more comments in the code, it's not self evident what all the code blocks are for.

Many numeric constants - like : stepperX.setMaxSpeed(6250.0);

Could be done as:

const float maxSpeedToStartPosn = 6250.0;

to differentiate it from

const float maxSpeedInLoop = 3000.0;

Then use the variable names wherever needed. This has two advantages.

  1. If you later need to adjust the constant you can do it in one place rather than searching the program for all instances of say, 6250.0.

B. Descriptive variable names makes the code more self-documenting.

One of numerous [style guides](C++ Programming Style Guidelines Conventions).