Coil Winder version2

A while back I started and finished a coil winder project that worked but I ended up disliking it because it took almost an hour to wind a coil with only 2000 turns. The reason the the lengthy time was due to the cycling of two motors. One motor would rotate the core and another motor would move the wire positioning head one wire width. This back and forth stuff just took too darn long. I decided to try a method that would continuously run both motors. I've run into a real small snag that I can't resolve. I've fought with this simple thing for a couple of hours at least. I'd love to discover the solution myself but enough is enough. I just wanna get it fixed and move on to other parts of the code that need to be modified.

The Machine_Setup function (moving coil positioning (Transverse) head left or right) works as desired, verifying that the motors, L293D, wiring are good.

The function of the loop is to turn on the coil winding motor while keeping track of the number of rotations via the motor's built in encoder and some encoder pulse to rotation simple math. The number of turns per coil layer and total turns are determined outside the code. Rigth off the bat the head positioning (Transverse) motor is turned on. When the desired number of layer turns is reached a flag 'Right' is toggled to change the direction that the wire positioning head moves (at least it's supposed to change - this is where the bug is.) and the number of layer turns is set to zero. Everything else is functioning correctly. Via print statements I can see that the counts for the layer turns, total turns and the Right flag are updated correctly. The problem is that the head positioning motor only turns when the Right flag is false, 0. As intended, all motors stop when the desired number of total turns is reached.

Below is a screen shot of the serial monitor showing the Right flag change.

const int Wind_Motor_Pin_A = 6; // motor drive pin to L293 pin 7
const int Wind_Motor_Pin_B = 7; // motor drive pin to L293 pin 2
const int Wind_Motor_EncoderPin_A = 2; // One quadrature pin of encoder (interrupt #0, pin3 of Arduino)
const int Wind_Motor_EncoderPin_B = 4; //reading HIGH or LOW here gives direction
const int Traverse_Motor_Pin_A = 12; // motor drive pin to L293 pin 10 (untested)
const int Traverse_Motor_Pin_B = 13; // motor drive pin to L293 15 (untested)

const int left_switchPin = A0;  //momentary contact
const int right_switchPin = A1;  //momentary contact
const int done_switchPin = A3;  //momentary contact start_switchPin = 17; //momentary contact
const int start_switchPin = A2; //momentary contact
const int left_LEDPin = 11; //value to LEFT LED indicator
const int right_LEDPin = 10; //value to RIGHT LED indicator
const int done_LEDPin = 9; //value to DONE LED indicator
const int start_LEDPin = 8; //value to START LED indicator

byte left = HIGH; //value after digital.Read of left switch
byte right = HIGH; //value after digital.Read of right switch
byte done = HIGH; //value after digital.Read of done switch
byte start = HIGH; //value after digital.Read of start switch
boolean Right = true; // value used to change Transverse Motor direction
int Machine_Setup_Done; // 
int Start;
int Desired_Layer_Turns = 10; //value entered here based on coil length and wire diameter
int Layer_Turns = 0; //this value will be incremented every rotation of the Wind Motor. zeroed on direction change
int Total_Turns = 0;
int Desired_Total_Turns = 40; //manually input by user
int Wind_Encoder_Count = 0;
boolean New_Wind = true; //to be used later after code is modified
boolean Wind_Done = false;
int Transverse_Motor_Speed = 0;
//boolean Encoder_Reset = true; //This value used for gathering encoder count per inch of traverse travel


void setup() {

  //INITIALIZE PINS
  pinMode(left_switchPin, INPUT); //LEFT pushbutton switch
  pinMode(right_switchPin, INPUT); //RIGHT pushbutton switch
  pinMode(done_switchPin, INPUT); //DONE pushbutton switch
  pinMode(start_switchPin, INPUT); //START pushbutton switch
  pinMode(Wind_Motor_Pin_A, OUTPUT); //to pin 2 of L293
  pinMode(Wind_Motor_Pin_B, OUTPUT); //to pin 7 of L293
  pinMode(Traverse_Motor_Pin_A, OUTPUT); //to pin 2 of L293
  pinMode(Traverse_Motor_Pin_B, OUTPUT); //to pin 7 of L293
  pinMode(left_LEDPin, OUTPUT); //LEFT pushbutton indicator
  pinMode(right_LEDPin, OUTPUT); //RIGHT pushbutton indicator
  pinMode(done_LEDPin, OUTPUT); //DONE pushbutton indicator
  pinMode(start_LEDPin, OUTPUT); //SRART pushbutton indicator
  digitalWrite (left_LEDPin, LOW); //initiates LED to off
  digitalWrite (right_LEDPin, LOW); //initiates LED to off
  digitalWrite (done_LEDPin, LOW); //initiates LED to off
  digitalWrite (start_LEDPin, LOW); //initiates LED to off


  //TURN ON INTERNAL PULLUPS FOR PUSHBUTTONS
  digitalWrite (left_switchPin, HIGH); //initiates internal pullup resitor
  digitalWrite (right_switchPin, HIGH); //initiates internal pullup resitor
  digitalWrite (done_switchPin, HIGH); //initiates internal pullup resitor
  digitalWrite (start_switchPin, HIGH); //initiates internal pullup resitor
  //digitalWrite (Encoder_Reset_Switch LOW); //initiates internal pullup resitor //This value used for gathering encoder count per inch
  //of traverse travel
  Serial.begin(9600); //Use serial port to keep user informed status

  attachInterrupt(0, Wind_Rotation, FALLING); // Attach interrupt to encoder pin A

}
//ISR//
void Wind_Rotation() {
  // This is the subroutine that runs every time pin 2
  // switches from high to low.
  Wind_Encoder_Count++;
}      



/*Machine_Setup function's purpose is to allow user (using left & right pushbuttons) to move wire positioning head so that
 the wire is at a far left position in reference to the spool. This is the start position. When the DONE push button is
 pressed a flag Machine_Position_Done is set. That value is checked first thing in the loop. It is meant that the rest
 of the loop (actual winding functions) is not entered into until the wire positioning is completed.*/

void Machine_Setup() {
  //Serial.println("      GOT HERE!"); //debugging

  do{
    left = digitalRead(left_switchPin);
    right = digitalRead(right_switchPin);
    done = digitalRead(done_switchPin);
    start = digitalRead(start_switchPin);
    if (left == LOW){
      digitalWrite (Traverse_Motor_Pin_A, LOW); //Drive pin of 293
      digitalWrite (Traverse_Motor_Pin_B, HIGH); //Other drive pin of 293
      digitalWrite (left_LEDPin, HIGH);  //left indicator led
    }
    else if (right == LOW){
      digitalWrite (Traverse_Motor_Pin_A, HIGH);
      digitalWrite (Traverse_Motor_Pin_B, LOW);
      digitalWrite (right_LEDPin, HIGH);
    }
    else if (done == LOW){
      digitalWrite (done_LEDPin, HIGH);
      Machine_Setup_Done = true; //Machine_Setup_Done is checked by loop
    }
    else if (start == LOW){
      digitalWrite (start_LEDPin, HIGH);
      Start = true; //Start checked by loop. If true allows the held up loop to continue
    }
    else { //ensures motor is not driven
      digitalWrite (Traverse_Motor_Pin_A, LOW);
      digitalWrite (Traverse_Motor_Pin_B, LOW);
      digitalWrite (right_LEDPin, LOW);
      digitalWrite (left_LEDPin, LOW);
    } 
  }
  while (start); //Start button has not been pushed. Stay in this function
}


void loop(){
  if (Machine_Setup_Done == false){ //checks to see if wire position is set. If not goes to Machine_Setup
    Machine_Setup();
    Serial.println ("               MACHINE SETUP DONE");
    Serial.println (Machine_Setup_Done);
    delay(1000);
  }

  Serial.print ("IN WIND MOTOR ROTATION");
  if (!Wind_Done){ //only allows remaining code to run if this is a new wind
    digitalWrite(Wind_Motor_Pin_A, LOW);
    digitalWrite(Wind_Motor_Pin_B, HIGH);
  }

  if (Right == true && !Wind_Done){ //if Right is true this moves wire positioning head right
    digitalWrite(Traverse_Motor_Pin_A, HIGH);
    digitalWrite(Traverse_Motor_Pin_B, LOW);
  }

  { 
    if (Right == false && !Wind_Done)//if Right is false this moves wire positioning head left
    digitalWrite(Traverse_Motor_Pin_A, LOW);
    digitalWrite(Traverse_Motor_Pin_B, HIGH);
  }

  if (Wind_Done){
    digitalWrite(Wind_Motor_Pin_A, LOW);
    digitalWrite(Wind_Motor_Pin_B, LOW);
    digitalWrite(Traverse_Motor_Pin_B, LOW);
    digitalWrite(Traverse_Motor_Pin_A, LOW);
  }
  //noInterrupts();
  if (Wind_Encoder_Count >139){
    Layer_Turns++;
    Total_Turns++;
    Wind_Encoder_Count = 0;
  }
  if (Layer_Turns == Desired_Layer_Turns){
    Right = !Right;
    Layer_Turns = 0;
  }
  if (Total_Turns == Desired_Total_Turns){
    Wind_Done = true;
  }
  //interrupts();
  Serial.print ("WIND_ENCODER = ");
  Serial.print (Wind_Encoder_Count);
  Serial.print ("   LAYER_TURNS = ");
  Serial.print (Layer_Turns);
  Serial.print ("   TOTAL_TURNS = ");
  Serial.print (Total_Turns);
  Serial.print ("   RIGHT = ");
  Serial.print (Right);
  Serial.print ("   WIND_DONE = ");
  Serial.println (Wind_Done);
}

I've try changing Right to a byte to read 1 or 0 or HIGH or LOW but had no success. I'd appreciate any help. I'm gonna kick myself when it's revealed.

Thanks - Scotty

Haven't studied your code in detail, but this doesn't look right:

  { 
    if (Right == false && !Wind_Done)//if Right is false this moves wire positioning head left
    digitalWrite(Traverse_Motor_Pin_A, LOW);
    digitalWrite(Traverse_Motor_Pin_B, HIGH);
  }

I assume you meant:

    if (Right == false && !Wind_Done)//if Right is false this moves wire positioning head left
  { 
    digitalWrite(Traverse_Motor_Pin_A, LOW);
    digitalWrite(Traverse_Motor_Pin_B, HIGH);
  }

Good eye, Sir. Thank You. Too much wasted time over such a simple, stupid mistake. I hope you're around when I make my next one. - Scotty

How did the project turn out?

I am doing a similar project and wondered how you had done.

KR Marshall68