#include <AccelStepper.h>
#include <MultiStepper.h>
// Completed 20January2026 at 2:36 p.m.
// Input switch connections
const byte Switch_Red1 = A13; //  Red1 Shuttle stepper
// Small 5v Steppers
MultiStepper steppers;
AccelStepper Heddle1(AccelStepper::FULL4WIRE, 22, 24, 23, 25); 
AccelStepper Heddle2(AccelStepper::FULL4WIRE, 26, 28, 27, 29); 
AccelStepper Heddle3(AccelStepper::FULL4WIRE, 30, 32, 31, 33); 
AccelStepper Heddle4(AccelStepper::FULL4WIRE, 34, 36, 35, 37); 
AccelStepper Heddle5(AccelStepper::FULL4WIRE, 38, 40, 39, 41); 
AccelStepper Heddle6(AccelStepper::FULL4WIRE, 42, 44, 43, 45); 
AccelStepper Heddle7(AccelStepper::FULL4WIRE, 46, 48, 47, 49); 
AccelStepper Heddle8(AccelStepper::FULL4WIRE, 50, 52, 51, 53); 
// MultiStepper steppers;
const int Heddles_1_4_Up = 1600; //  5v Heddles 1 - 4
const int Heddles_1_4_Down = -1600; //  5v Heddles 1 - 4
const int Heddles_5_8_Up = -1600; //   5v Heddles 5 - 8
const int Heddles_5_8_Down = 1600; //   5v Heddles 5 - 8
const int Heddles_Center = 0;    // 5v Stepper motors
const int timedelay =100;
// Small motors sequence array
int j = 1;    // Start with Heddles 1, 3, 5, 7 Up
int D_P[11] = {0,85,170,85,170,85,170,85,170,85,170,};
long positions[8]; // Array of desired stepper positions

void setup() {
  Serial.begin(9600);
  //  Start positions
  pinMode(Switch_Red1, INPUT);
  // Configure each 5v stepper
  Heddle1.setMaxSpeed(500);   
  Heddle2.setMaxSpeed(500);
  Heddle3.setMaxSpeed(500);
  Heddle4.setMaxSpeed(500);
  Heddle5.setMaxSpeed(500);
  Heddle6.setMaxSpeed(500);
  Heddle7.setMaxSpeed(500);
  Heddle8.setMaxSpeed(500);
// Then give them to MultiStepper to manage
  steppers.addStepper(Heddle1);
  steppers.addStepper(Heddle2);
  steppers.addStepper(Heddle3);
  steppers.addStepper(Heddle4);
  steppers.addStepper(Heddle5);
  steppers.addStepper(Heddle6);
  steppers.addStepper(Heddle7);
  steppers.addStepper(Heddle8);

Serial.println("Ready to begin cycle, Push Red1 to start");
while (digitalRead(Switch_Red1) == LOW) {
  Serial.println("Push Switch_Red1 already");
  delay(1000);   }
Serial.println("Red1 pushed - going!");
}
void loop() {

Serial.println("Ready to position heddles. Push red button");
while (digitalRead(Switch_Red1) == LOW) {
  delay(100);   }

Serial.print("cycle = ");
Serial.print(j);
Serial.print("  Decimal pattern = ");
Serial.print(D_P[j]);
Serial.println();
    Heddle1.enableOutputs();
    Heddle2.enableOutputs();
    Heddle3.enableOutputs();
    Heddle4.enableOutputs();  
    Heddle5.enableOutputs();    
    Heddle6.enableOutputs();
    Heddle7.enableOutputs();
    Heddle8.enableOutputs();
   for (int i = 7; i >= 4; i--) { // Iterate through bits from MSB to LSB
       if (bitRead(D_P[j], i) == 1) {
        positions[i] = Heddles_5_8_Up;
         } else {
        positions[i] = Heddles_5_8_Down;
          }
      }  
    for (int i = 3; i >= 0; i--) { // Iterate through bits from MSB to LSB
        if (bitRead(D_P[j], i) == 1) {
          positions[i] = Heddles_1_4_Up;
        } else {
          positions[i] = Heddles_1_4_Down;
        }
    }   
          for (int i = 0; i <= 7; i++) { 
          Serial.print(positions[i]);
          Serial.print(",  ");   
          }
      Serial.println();

    steppers.moveTo(positions);
    steppers.runSpeedToPosition(); // Blocks until all are in position
    delay(timedelay);
    Heddle1.disableOutputs();
    Heddle2.disableOutputs();
    Heddle3.disableOutputs();
    Heddle4.disableOutputs();
    Heddle5.disableOutputs();
    Heddle6.disableOutputs();
    Heddle7.disableOutputs();
    Heddle8.disableOutputs();

Serial.println("Heddles in position. Ready to center. Push red button");
while (digitalRead(Switch_Red1) == LOW) {
  delay(100);   }

//  Heddles to center
    Heddle1.enableOutputs();
    Heddle2.enableOutputs();
    Heddle3.enableOutputs();
    Heddle4.enableOutputs();  
    Heddle5.enableOutputs();    
    Heddle6.enableOutputs();
    Heddle7.enableOutputs();
    Heddle8.enableOutputs();
   for (int i = 7; i >= 4; i--) { // Iterate through bits from MSB to LSB
       if (bitRead(D_P[j], i) == 1) {
        positions[i] = 0; // Heddles_5_8_Down;
         } else {
        positions[i] = 0; //Heddles_5_8_Up;
          }
      }  
    for (int i = 3; i >= 0; i--) { // Iterate through bits from MSB to LSB
        if (bitRead(D_P[j], i) == 1) {
          positions[i] = 0; //Heddles_1_4_Down;
        } else {
          positions[i] = 0; //Heddles_1_4_Up;
        }
    }    
          for (int i = 0; i <= 7; i++) { 
          Serial.print(positions[i]);
          Serial.print(",  ");   
          }
      Serial.println();
    steppers.moveTo(positions);
    steppers.runSpeedToPosition(); // Blocks until all are in position
    delay(timedelay);
    Heddle1.disableOutputs();
    Heddle2.disableOutputs();
    Heddle3.disableOutputs();
    Heddle4.disableOutputs();
    Heddle5.disableOutputs();
    Heddle6.disableOutputs();
    Heddle7.disableOutputs();
    Heddle8.disableOutputs();

j = j + 1;

}
