Hi. I made a coil winder with a x axis stepper motor turning a ball screw linear stage to move a wire guide left and right. The Y axis is a motor that spins a bobbin on its shaft. The X moves the wire back and forth while the Y axis spins the bobbin. The X axis homes when powered up as it should. The buttons, buttonPin and buttonPin2, work as they should but one time only. After that the buttons do nothing when pushed. I have set the layers to low numbers for quick testing. There are no errors when verifying. I’m not sure if I’m over looking something or maybe I have somehow messed up my Arduino UNO. Any help would be appreciated.
// Coil Winder Sketch 39 AWG, 150 winds per layer. 1:10 ratio, 1:1:10 ratio.
//Define pin connections & motor's steps per revolution
const int stepPinX = 2; // motor X step pin
const int dirPinX = 3; // motor X direction pin
const int enableX = 4; // motor X enable pin
const int stepPinY = 5; // motor Y step pin
const int dirPinY = 6; // motor Y direction pin
const int enableY = 7; // motor Y enable pin
const int buttonPin = 8; // pin connected to the buttonPin switch
const int homeSwitch = 9; // Pin connected to Home Switch to ground
const int buttonPin2 = 10; // pin connected to the buttonPin2 switch
const int stepsPerRevolution = 200; //number of steps per revolution in motorloop
//creates variable integers for layers
int turnsPerLayer = 15; //Change this number for the number of turns in layer
int Layers = 0; //Total layers
int LayersTotal = 5; //Total layers for coil one in 1:10 ratio
int LayersTotal2 = 10; //Total layers for coil ten in 1:10 ratio
void setup()
{
// Declare pins
pinMode(stepPinX, OUTPUT);
pinMode(dirPinX, OUTPUT);
pinMode(stepPinY, OUTPUT);
pinMode(dirPinY, OUTPUT);
pinMode(enableX, OUTPUT);
pinMode(enableY, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(homeSwitch, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
// Start Homing procedure of X axis Stepper Motor at startup
while (digitalRead(homeSwitch) == LOW) // Do this until the switch is not activated
{
digitalWrite(dirPinX, LOW); // (HIGH = anti-clockwise / LOW = clockwise)
digitalWrite(stepPinX, HIGH);
delay(.2); // Delay to slow down speed of Stepper
digitalWrite(stepPinX, LOW);
delay(.2);
}
while (digitalRead(homeSwitch) == HIGH) // Do this until the switch is activated
{
digitalWrite(dirPinX, HIGH);
digitalWrite(stepPinX, HIGH);
delay(2); // More delay to slow even more while moving away from switch
digitalWrite(stepPinX, LOW);
delay(2);
}
}
void loop()
{
ButtonloopA(); // Call function
}
void ButtonloopA() //Function called to pause winding
{
if (digitalRead(buttonPin) == LOW) // check if the pushbutton is pressed. If it is, the buttonState is LOW
{
MotorloopA(); // call function after button is pressed
}
else if (digitalRead(buttonPin2) == LOW) // check if the pushbutton2 is pressed. If it is, the buttonState is LOW
{
MotorMainLoopA(); // call function after button is pressed
}
else
{
ButtonloopA(); // call ButtonloopA function loop until a button is pressed
}
}
void MotorloopA() //Function called to wind layer right to left
{
digitalWrite(dirPinX, HIGH); // Set motor X direction clockwise
digitalWrite(dirPinY, LOW); // Set motor Y direction clockwise
for(; Layers < LayersTotal;) // Count layers and stop at LayersTotal amount
{
for(int stepTurns = 0; stepTurns < turnsPerLayer; stepTurns++) //Count winds per layer and calls MotorloopA to switch direction at turnsPerLayer amount
{
for(int x = 0; x < stepsPerRevolution; x++) // Spin motor Y one rotation
{
TurnRatio(); //Call function
}
}
Layers++; //Adds 1 to Layers count
MotorloopB(); //call function pause loop waiting for button push
}
ButtonloopA(); //call function pause loop waiting for button push
}
void ButtonloopB() //Function called to pause winding
{
if (digitalRead(buttonPin) == LOW) // check if the pushbutton is pressed. If it is, the buttonState is LOW
{
MotorloopB(); // call function after button is pressed
}
else if (digitalRead(buttonPin2) == LOW)
{
MotorMainLoopB(); // call function after button is pressed
}
else
{
ButtonloopB(); // call function loop until button is pressed
}
}
void MotorloopB() //Function called to wind layer left to right
{
digitalWrite(dirPinX, LOW); // Set motor X direction clockwise
for(; Layers < LayersTotal;) // Count layers and stop at LayersTotal amount
{
for(int stepTurns = 0; stepTurns < turnsPerLayer; stepTurns++) //Count winds per layer and calls MotorloopA to switch direction at turnsPerLayer amount
{
for(int x = 0; x < stepsPerRevolution; x++) // Spin motor Y one rotation
{
TurnRatio(); //Call function
}
}
Layers++; //Adds 1 to Layers count
MotorloopA(); //Call function to wind right to left
}
ButtonloopB(); //call function pause loop waiting for button push
}
void MotorMainLoopA() //Function called to wind ten coil in 1:10 right to left
{
digitalWrite(dirPinX, HIGH); // Set motor X direction clockwise
for(; Layers < LayersTotal2;) // Count layers and stop at LayersTotal amount
{
for(int stepTurns = 0; stepTurns < turnsPerLayer; stepTurns++) //Count winds per layer and calls MotorloopA to switch direction at turnsPerLayer amount
{
for(int x = 0; x < stepsPerRevolution; x++) // Spin motor Y one rotation
{
TurnRatio(); //Call function
}
}
Layers++; //Adds 1 to Layers count
MotorMainLoopB(); //call function pause loop waiting for button push
}
ButtonloopA(); //call function pause loop waiting for button push
}
void MotorMainLoopB() //Function called to wind coil ten in 1:10 left to right
{
digitalWrite(dirPinX, LOW); // Set motor X direction clockwise
for(; Layers < LayersTotal2;) // Count layers and stop at LayersTotal amount
{
for(int stepTurns = 0; stepTurns < turnsPerLayer; stepTurns++) //Count winds per layer and calls MotorloopA to switch direction at turnsPerLayer amount
{
for(int x = 0; x < stepsPerRevolution; x++) // Spin motor Y one rotation
{
TurnRatio(); //Call function
}
}
Layers++; //Adds 1 to Layers count
MotorMainLoopA(); //call function pause loop waiting for button push
}
ButtonloopB(); //call function pause loop waiting for button push
}
void StepMotorX()
{
digitalWrite(stepPinX, HIGH); //One step X motor
delayMicroseconds(100);
digitalWrite(stepPinX, LOW);
delayMicroseconds(100);
}
void StepMotorY()
{
digitalWrite(stepPinY, HIGH); //One step Y motor
delayMicroseconds(100);
digitalWrite(stepPinY, LOW);
delayMicroseconds(100);
}
void TurnRatio()
{
StepMotorX(); //One X step
StepMotorY(); //to three Y steps
StepMotorY();
StepMotorY();
StepMotorX(); //One X step
StepMotorY(); //to three Y steps
StepMotorY();
StepMotorY();
StepMotorX(); //One X step
StepMotorY(); //to two Y steps
StepMotorY();
}