Go Down

### Topic: Moving motors to a reset (0,0) point (Read 3244 times)previous topic - next topic

#### breegeek

##### Apr 10, 2012, 05:10 am
Hi all,

Been getting a lot of help here, and I really appreciate it!

I am building a project using an XY plotter bed to move around a magnet under a table, which will be moving a corresponding magnet-attached object on the top of the table.

I have no way besides mathematically to determine a coordinate system for the header. So I have installed 4 limit switches (0, 0, xMax, yMax), and want to have a way to keep track of its position and create a "reset/recalibrate" function that sends the head back to 0,0 and resets the XY values accordingly.

I am not 100% sure how to do this; to make the motors alternate pulses (so that I get a more diagonal-looking interpolation) until they reach 00? Do I do a some kind of while statement? Or is there a smarter way to do this?

CODE:
Code: [Select]
`/* Stepper Motor Controller ;  language: Wiring/Arduino This program drives a unipolar or bipolar stepper motor.  by Tom Igoe and the Spline library by Kerinin *//* significant help from arduino forum superstars: sirbow2, stimmer */#include <spline.h>#include <Stepper.h>#define motorSteps 200     // change this depending on the number of steps#define motor2Steps 200     // change this depending on the number of steps// per revolution of your motor#define motorPin1 2#define motorPin2 3#define motorPin3 4#define motorPin4 5#define motorPin6 8#define motorPin7 9#define motorPin8 10#define motorPin9 11int reading = 0;int previous = LOW;long time = 0;         // the last time the output pin was toggledlong debounce = 50;   // the debounce time, increase if the output flickersint state = HIGH;    //set pin numbersint xStopMin = A0;int xStopMax = A1;int yStopMin = A2;int yStopMax = A3;int xPos;int yPos;boolean xIs0;boolean xIsMax;boolean yIs0;boolean yIsMax;// initialize of the Stepper library:Stepper yAxis(motorSteps, motorPin1,motorPin2, motorPin3, motorPin4); Stepper xAxis(motor2Steps, motorPin6,motorPin7, motorPin8, motorPin9); void setup() {  xAxis.setSpeed(60); //x  yAxis.setSpeed(60); //y  //set limit switches to analog inputs  pinMode(xStopMin, INPUT);  pinMode(xStopMax, INPUT);  pinMode(yStopMin, INPUT);   pinMode(yStopMax, INPUT);  xPos = 0;  yPos = 0;  // Initialize the Serial port:  Serial.begin(9600);}void loop() {  //check limit switchesif(digitalRead(xStopMin) == HIGH){    xIs0 = true;      xPos = 0;    Serial.println("xMin");  }  else xIs0 = false;  if(digitalRead(xStopMax) == HIGH){    xIsMax = true;      Serial.println("xMax");  }  else xIsMax = false;  if(digitalRead(yStopMin) == HIGH){    yIs0 = true;      Serial.println("yMin");    yPos = 0;  }  else yIs0 = false;  if(digitalRead(yStopMax) == HIGH){    yIsMax = true;      Serial.println("yMax");  }  else yIsMax = false;  char val=0;  if(Serial.available()) val = Serial.read();  //keyboard control    switch(val){  case 'N':    Serial.read();    moveN(200);    break;  case 'S':    Serial.read();    moveS(200);    break;  case 'E':    Serial.read();    moveE(200);    break;  case 'W':    Serial.read();    moveW(200);    break;     //diagonal (moving motors almost at the same time)        case 'J':     Serial.read();    moveNE(400);    Serial.println("I am moving North East");    break;      case 'H': //NW    Serial.read();    moveNW(400);    break;  case 'X': //SE    Serial.read();    moveSE(400);    break;  case 'Z': //SW    Serial.read();    moveSW(400);    break;  case 'C': //SW    Serial.read();    moveCirc();    break;    //resets  case 'Q': //reset to 0,0     Serial.read();    moveN(100);    moveE(100);    break;    //debugs  case 'P':    Serial.read();    Serial.print("x position: ");    Serial.println(xPos);    Serial.println("y position: ");    Serial.println(yPos);    break;  default:    Serial.read();    delay(10);    break;  }}//movement functions//first, str  aight up down left rightvoid moveE(int numSteps){  int s = 0;  while(!digitalRead(xStopMax)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps  {    xAxis.step(1);    s++;  }    //delay(100);      xPos += numSteps;  //check where we are  Serial.print("x position: ");  Serial.println(xPos);  Serial.print("y position: ");  Serial.println(yPos);}void moveW(int numSteps){  int s = 0;  while(!digitalRead(xStopMin)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps  {    xAxis.step(-1);    s++;  }    //delay(100);      xPos -= numSteps;  //check where we are  Serial.print("x position: ");  Serial.println(xPos);  Serial.print("y position: ");  Serial.println(yPos);}void moveN(int numSteps){  int s = 0;  while(!digitalRead(yStopMax)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps  {    yAxis.step(-1);    s++;  }    //delay(100);      yPos += numSteps;  //check where we are  Serial.print("x position: ");  Serial.println(xPos);  Serial.print("y position: ");  Serial.println(yPos);}  void moveS(int numSteps){  int s = 0;  while(!digitalRead(yStopMin)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps  {    yAxis.step(1);    s++;  }    //delay(100);      yPos -= numSteps;  //check where we are  Serial.print("x position: ");  Serial.println(xPos);  Serial.print("y position: ");  Serial.println(yPos);}    //diagonalsvoid moveNE(int numSteps){      int s=0;   if (!digitalRead(xStopMax) && !digitalRead(yStopMin) && s<numSteps){   for(int s=0; s<numSteps; s++)  {    yAxis.step(-1);    xAxis.step(1);  }  xPos += numSteps;  yPos -= numSteps;  //check where we are  Serial.print("x position: ");  Serial.println(xPos);  Serial.print("y position: ");  Serial.println(yPos);  }}  void moveSE(int numSteps){  int s=0;   if(!digitalRead(xStopMax) && !digitalRead(yStopMax) && s<numSteps){   for(int s=0; s<numSteps; s++)  {    yAxis.step(1);    xAxis.step(1);  }  xPos += numSteps;  yPos += numSteps;  //check where we are  Serial.print("x position: ");  Serial.println(xPos);  Serial.print("y position: ");  Serial.println(yPos);  }  }void moveSW(int numSteps){  int s=0;   if(!digitalRead(xStopMin) && !digitalRead(yStopMax) && s<numSteps){   for(int s=0; s<numSteps; s++)  {    yAxis.step(1);    xAxis.step(-1);  }  xPos -= numSteps;  yPos += numSteps;  //check where we are  Serial.print("x position: ");  Serial.println(xPos);  Serial.print("y position: ");  Serial.println(yPos);  }}  void moveNW(int numSteps){  int s=0;   if(!digitalRead(xStopMin) && !digitalRead(yStopMin) && s<numSteps){   for(int s=0; s<numSteps; s++)  {    yAxis.step(-1);    xAxis.step(-1);  }  xPos -= numSteps;  yPos -= numSteps;  //check where we are  Serial.print("x position: ");  Serial.println(xPos);  Serial.print("y position: ");  Serial.println(yPos);  }}  void moveCirc(){  //draw circle at x,y 50,50  int CircleXCenter = 2;  int CircleYCenter = 2;   int CurXPos = xPos; //where the platform currently is in X  int CurYPos = yPos; //where the platform currently is in Y  int Rad = 1;  for (int i = 0; i < 360; i++)  {    //it does this for each point of the circle, so you dont have to run them at the same time    float       angle = i*2*3.14/360;    xPos = CircleXCenter + (cos(angle) * Rad);    yPos = CircleYCenter + (sin(angle) * Rad);    if(CurXPos < xPos)    {      xAxis.step(xPos - CurXPos);      //movexnow = Xpos - CurXPos;    }    else    {      xAxis.step(xPos - CurXPos);       //MoveXNow = CurXPos - Xpos;     }    if(CurYPos < yPos)    {      yAxis.step(yPos - CurYPos);       //   MoveYNow = Ypos - CurYPos;    }    else    {      yAxis.step(yPos - CurYPos        );       //MoveYNow = CurYPos - Ypos;     }    // step(xAxis);    //step(yAxis);    //<100, so -1     if(i == (360-1)) //we are at end of for loop, save current position.    {      CurYPos = yPos;            CurXPos = xPos ;     }  }}`

#### PeterH

#1
##### Apr 10, 2012, 12:20 pmLast Edit: Apr 10, 2012, 12:21 pm by PeterH Reason: 1
Presumably your sketch provides functions to move in X and Y axes. So you just call functions repeatedly to move in the negative direction on both axes until the limit switch is hit on each axis. If you have got your head around writing code to move to move to an arbitrary position, then the 'reset' part ought to be easy.

#### breegeek

#2
##### Apr 10, 2012, 05:59 pm
Hmmmmmm I tried a few things, but I can't seem to get it to stop when it hits the limit switches; it keeps going. What I need is for it to move diagonally until it hits one limit switch and then move in the other direction till both switches have been activated...

Here's what I have so far, the function is called "reset"

Code: [Select]
`/* Stepper Motor Controller ;  language: Wiring/Arduino This program drives a unipolar or bipolar stepper motor.  by Tom Igoe and the Spline library by Kerinin *//* significant help from arduino forum superstars: sirbow2, stimmer */#include <spline.h>#include <Stepper.h>#define motorSteps 200     // change this depending on the number of steps#define motor2Steps 200     // change this depending on the number of steps// per revolution of your motor#define motorPin1 2#define motorPin2 3#define motorPin3 4#define motorPin4 5#define motorPin6 8#define motorPin7 9#define motorPin8 10#define motorPin9 11int reading = 0;int previous = LOW;long time = 0;         // the last time the output pin was toggledlong debounce = 50;   // the debounce time, increase if the output flickersint state = HIGH;    //set pin numbersint xStopMin = A0;int xStopMax = A1;int yStopMin = A2;int yStopMax = A3;int xPos;int yPos;boolean xIs0;boolean xIsMax;boolean yIs0;boolean yIsMax;// initialize of the Stepper library:Stepper yAxis(motorSteps, motorPin1,motorPin2, motorPin3, motorPin4); Stepper xAxis(motor2Steps, motorPin6,motorPin7, motorPin8, motorPin9); void setup() {  xAxis.setSpeed(60); //x  yAxis.setSpeed(60); //y  //set limit switches to analog inputs  pinMode(xStopMin, INPUT);  pinMode(xStopMax, INPUT);  pinMode(yStopMin, INPUT);   pinMode(yStopMax, INPUT);  xPos = 0;  yPos = 0;  // Initialize the Serial port:  Serial.begin(9600);}void loop() {  //check limit switchesif(digitalRead(xStopMin) == HIGH){    xIs0 = true;      xPos = 0;    Serial.println("xMin");  }  else xIs0 = false;  if(digitalRead(xStopMax) == HIGH){    xIsMax = true;      Serial.println("xMax");  }  else xIsMax = false;  if(digitalRead(yStopMin) == HIGH){    yIs0 = true;      Serial.println("yMin");    yPos = 0;  }  else yIs0 = false;  if(digitalRead(yStopMax) == HIGH){    yIsMax = true;      Serial.println("yMax");  }  else yIsMax = false;  char val=0;  if(Serial.available()) val = Serial.read();  //keyboard control    switch(val){  case 'N':    Serial.read();    moveN(200);    break;  case 'S':    Serial.read();    moveS(200);    break;  case 'E':    Serial.read();    moveE(200);    break;  case 'W':    Serial.read();    moveW(200);    break;     //diagonal (moving motors almost at the same time)        case 'J':     Serial.read();    moveNE(400);    Serial.println("I am moving North East");    break;      case 'H': //NW    Serial.read();    moveNW(400);    break;  case 'X': //SE    Serial.read();    moveSE(400);    break;  case 'Z': //SW    Serial.read();    moveSW(400);    break;  case 'C': //SW    Serial.read();    moveCirc();    break;    //resets  case 'Q': //reset to 0,0     Serial.read();          reset(8000);    break;    //debugs  case 'P':    Serial.read();    Serial.print("x position: ");    Serial.println(xPos);    Serial.println("y position: ");    Serial.println(yPos);    break;  default:    Serial.read();    delay(10);    break;  }}//movement functions//first, str  aight up down left rightvoid moveE(int numSteps){  int s = 0;  while(!digitalRead(xStopMax)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps  {    xAxis.step(1);    s++;  }    //delay(100);      xPos += numSteps;  //check where we are  Serial.print("x position: ");  Serial.println(xPos);  Serial.print("y position: ");  Serial.println(yPos);}void moveW(int numSteps){  int s = 0;  while(!digitalRead(xStopMin)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps  {    xAxis.step(-1);    s++;  }    //delay(100);      xPos -= numSteps;  //check where we are  Serial.print("x position: ");  Serial.println(xPos);  Serial.print("y position: ");  Serial.println(yPos);}void moveN(int numSteps){  int s = 0;  while(!digitalRead(yStopMax)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps  {    yAxis.step(-1);    s++;  }    //delay(100);      yPos += numSteps;  //check where we are  Serial.print("x position: ");  Serial.println(xPos);  Serial.print("y position: ");  Serial.println(yPos);}  void moveS(int numSteps){  int s = 0;  while(!digitalRead(yStopMin)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps  {    yAxis.step(1);    s++;  }    //delay(100);      yPos -= numSteps;  //check where we are  Serial.print("x position: ");  Serial.println(xPos);  Serial.print("y position: ");  Serial.println(yPos);}    //diagonalsvoid moveNE(int numSteps){      int s=0;   if (!digitalRead(xStopMax) && !digitalRead(yStopMin) && s<numSteps){   for(int s=0; s<numSteps; s++)  {    yAxis.step(-1);    xAxis.step(1);  }  xPos += numSteps;  yPos -= numSteps;  //check where we are  Serial.print("x position: ");  Serial.println(xPos);  Serial.print("y position: ");  Serial.println(yPos);  }}  void moveSE(int numSteps){  int s=0;   if(!digitalRead(xStopMax) && !digitalRead(yStopMax) && s<numSteps){   for(int s=0; s<numSteps; s++)  {    yAxis.step(1);    xAxis.step(1);  }  xPos += numSteps;  yPos += numSteps;  //check where we are  Serial.print("x position: ");  Serial.println(xPos);  Serial.print("y position: ");  Serial.println(yPos);  }  }void moveSW(int numSteps){  int s=0;   if(!digitalRead(xStopMin) && !digitalRead(yStopMax) && s<numSteps){   for(int s=0; s<numSteps; s++)  {    yAxis.step(1);    xAxis.step(-1);  }  xPos -= numSteps;  yPos += numSteps;  //check where we are  Serial.print("x position: ");  Serial.println(xPos);  Serial.print("y position: ");  Serial.println(yPos);  }}  void moveNW(int numSteps){  int s=0;   if(!digitalRead(xStopMin) && !digitalRead(yStopMax) && s<numSteps){   for(int s=0; s<numSteps; s++)  {    yAxis.step(1);    xAxis.step(-1);  }  xPos -= numSteps;  yPos -= numSteps;  //check where we are  Serial.print("x position: ");  Serial.println(xPos);  Serial.print("y position: ");  Serial.println(yPos);  }} void reset(int numSteps){  int s=0;   for(int s=0; s<numSteps; s++){   if(!digitalRead(xStopMin)){     xAxis.step(-1);  }  if(!digitalRead(yStopMin) && s<numSteps){     yAxis.step(-1);  }}   xPos = 0;  yPos = 0;  //check where we are  Serial.print("x position: ");  Serial.println(xPos);  Serial.print("y position: ");  Serial.println(yPos);  }  void moveCirc(){  //draw circle at x,y 50,50  int CircleXCenter = 2;  int CircleYCenter = 2;   int CurXPos = xPos; //where the platform currently is in X  int CurYPos = yPos; //where the platform currently is in Y  int Rad = 1;  for (int i = 0; i < 360; i++)  {    //it does this for each point of the circle, so you dont have to run them at the same time    float       angle = i*2*3.14/360;    xPos = CircleXCenter + (cos(angle) * Rad);    yPos = CircleYCenter + (sin(angle) * Rad);    if(CurXPos < xPos)    {      xAxis.step(xPos - CurXPos);      //movexnow = Xpos - CurXPos;    }    else    {      xAxis.step(xPos - CurXPos);       //MoveXNow = CurXPos - Xpos;     }    if(CurYPos < yPos)    {      yAxis.step(yPos - CurYPos);       //   MoveYNow = Ypos - CurYPos;    }    else    {      yAxis.step(yPos - CurYPos        );       //MoveYNow = CurYPos - Ypos;     }    // step(xAxis);    //step(yAxis);    //<100, so -1     if(i == (360-1)) //we are at end of for loop, save current position.    {      CurYPos = yPos;            CurXPos = xPos ;     }  }}`

#### PeterH

#3
##### Apr 10, 2012, 07:39 pm
Are you able to read all the limit switches in your sketch, and get the correct value as the switches are opened and closed?

#### breegeek

#4
##### Apr 10, 2012, 08:16 pm
Yes, they're all working as far as I know (tested them and they all do their println functions). Also, with normal movement, they stop the movement. I think it's a logic issue, but I can't figure it out :\

Thanks btw

#### stimmer

#5
##### Apr 10, 2012, 10:19 pm
Double check that the steppers are stepping in the correct direction and you have the correct limit switch for each axis.

If you were to call xAxis.step(-1) repeatedly would it eventually trigger the xStopMin switch or the xStopMax switch? Similarly if you repeatedly call yAxis.step(-1) will it eventually hit yStopMin or yStopMax?

In moveN you have yAxis.step(-1) associated with the yStopMax switch, but in reset yAxis.step(-1) is associated with the yStopMin switch. One or the other must be wrong.
Due VGA library - http://arduino.cc/forum/index.php/topic,150517.0.html

#### sirbow2

#6
##### Apr 10, 2012, 11:39 pm
you could put the switches on the head that moves around on the plane instead of putting the switches at 0,0 on the frame. that way, you can move in -x direction until you hit the x switch on the frame, then move -y until you hit the y switch on the frame.
http://dduino.blogspot.com all my Arduino/electronic projects!!!

{NEW} Getting Started, Learning, Reference + FAQ PDF!!:
[url]http://ar

#### krazatchu

#7
##### Apr 11, 2012, 03:33 am
In most CNC equipment, there is only one limit/home switch per axis.
Once it's calibrated to home, the machine software (MACH3 for ex) "knows" how far it can go in each axis...

You might try the pin change interrupt library to signal when a limit/home switch is triggered.
It will be more repeatable than polling as the interrupt will fire regardless of what the main loop is doing.

When my CNC mill homes, it pulls the Z up first for safety, then does X and Y.
When it finds the home switch, it then backs up and slow approaches again to negate switch bounce.
Michael
----------------------
http://www.krazatchu.ca

#### breegeek

#8
##### Apr 11, 2012, 03:45 pm
Ok I double checked my mins and maxes (dyslexia yay).

However, my reset function still doesn't work as expected :\
The switches don't stop it.

I am not sure I want to use interrupt pins, but I might have to. Is there a way to make this logic work?

Quote
Code: [Select]
`/* Stepper Motor Controller ;  language: Wiring/Arduino This program drives a unipolar or bipolar stepper motor.  by Tom Igoe and the Spline library by Kerinin *//* significant help from arduino forum superstars: sirbow2, stimmer */#include <spline.h>#include <Stepper.h>#define motorSteps 200     // change this depending on the number of steps#define motor2Steps 200     // change this depending on the number of steps// per revolution of your motor#define motorPin1 2#define motorPin2 3#define motorPin3 4#define motorPin4 5#define motorPin6 8#define motorPin7 9#define motorPin8 10#define motorPin9 11int reading = 0;int previous = LOW;long time = 0;         // the last time the output pin was toggledlong debounce = 50;   // the debounce time, increase if the output flickersint state = HIGH;    //set pin numbersint xStopMin = A0;int xStopMax = A1;int yStopMin = A2;int yStopMax = A3;int xPos;int yPos;boolean xIs0;boolean xIsMax;boolean yIs0;boolean yIsMax;// initialize of the Stepper library:Stepper yAxis(motorSteps, motorPin1,motorPin2, motorPin3, motorPin4); Stepper xAxis(motor2Steps, motorPin6,motorPin7, motorPin8, motorPin9); void setup() {  xAxis.setSpeed(60); //x HAS APPX 2200 STEPS FROM 0 TO MAX  yAxis.setSpeed(60); //y HAS APPX 6200 STEPS FROM 0 TO MAX  //set limit switches to analog inputs  pinMode(xStopMin, INPUT);  pinMode(xStopMax, INPUT);  pinMode(yStopMin, INPUT);   pinMode(yStopMax, INPUT);  xPos = 0;  yPos = 0;  // Initialize the Serial port:  Serial.begin(9600);}void loop() {  //check limit switches  if(digitalRead(xStopMin) == HIGH){    xIs0 = true;      xPos = 0;    Serial.println("xMin");  }  else xIs0 = false;  if(digitalRead(xStopMax) == HIGH){    xIsMax = true;      Serial.println("xMax");  }  else xIsMax = false;  if(digitalRead(yStopMin) == HIGH){    yIs0 = true;      Serial.println("yMin");    yPos = 0;  }  else yIs0 = false;  if(digitalRead(yStopMax) == HIGH){    yIsMax = true;      Serial.println("yMax");  }  else yIsMax = false;  char val=0;  if(Serial.available()) val = Serial.read();  //keyboard control    switch(val){  case 'N':    Serial.read();    moveN(200);    break;  case 'S':    Serial.read();    moveS(200);    break;  case 'E':    Serial.read();    moveE(200);    break;  case 'W':    Serial.read();    moveW(200);    break;     //diagonal (moving motors almost at the same time)        case 'J':     Serial.read();    moveNE(400);    Serial.println("I am moving North East");    break;  case 'H': //NW    Serial.read();    moveNW(400);    break;  case 'X': //SE    Serial.read();    moveSE(400);    break;  case 'Z': //SW    Serial.read();    moveSW(400);    break;  case 'C': //SW    Serial.read();    moveCirc();    break;    //resets  case 'Q': //reset to 0,0     Serial.read();    reset(8000);    break;    //debugs  case 'P':    Serial.read();    Serial.print("x position: ");    Serial.println(xPos);    Serial.println("y position: ");    Serial.println(yPos);    break;  default:    Serial.read();    delay(10);    break;  }}//movement functions//first, str  aight up down left rightvoid moveE(int numSteps){  int s = 0;  while(!digitalRead(xStopMax)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps  {    xAxis.step(1);    s++;  }    //delay(100);      xPos += numSteps;  //check where we are  Serial.print("x position: ");  Serial.println(xPos);  Serial.print("y position: ");  Serial.println(yPos);}void moveW(int numSteps){  int s = 0;  while(!digitalRead(xStopMin)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps  {    xAxis.step(-1);    s++;  }    //delay(100);      xPos -= numSteps;  //check where we are  Serial.print("x position: ");  Serial.println(xPos);  Serial.print("y position: ");  Serial.println(yPos);}void moveN(int numSteps){  int s = 0;  while(!digitalRead(yStopMin)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps  {    yAxis.step(-1);    s++;  }    //delay(100);      yPos -= numSteps;  //check where we are  Serial.print("x position: ");  Serial.println(xPos);  Serial.print("y position: ");  Serial.println(yPos);}  void moveS(int numSteps){  int s = 0;  while(!digitalRead(yStopMax)&& s<numSteps)//checks the button each step, also checks to see if it has moved s amount of steps  {    yAxis.step(1);    s++;  }    //delay(100);      yPos += numSteps;  //check where we are  Serial.print("x position: ");  Serial.println(xPos);  Serial.print("y position: ");  Serial.println(yPos);}    //diagonalsvoid moveNE(int numSteps){      int s=0;   if (!digitalRead(xStopMax) && !digitalRead(yStopMin) && s<numSteps){     for(int s=0; s<numSteps; s++)    {      yAxis.step(-1);      xAxis.step(1);    }    xPos += numSteps;    yPos -= numSteps;    //check where we are    Serial.print("x position: ");    Serial.println(xPos);    Serial.print("y position: ");    Serial.println(yPos);  }}  void moveSE(int numSteps){  int s=0;   if(!digitalRead(xStopMax) && !digitalRead(yStopMax) && s<numSteps){     for(int s=0; s<numSteps; s++)    {      yAxis.step(1);      xAxis.step(1);    }    xPos += numSteps;    yPos += numSteps;    //check where we are    Serial.print("x position: ");    Serial.println(xPos);    Serial.print("y position: ");    Serial.println(yPos);  }  }void moveSW(int numSteps){  int s=0;   if(!digitalRead(xStopMin) && !digitalRead(yStopMax) && s<numSteps){     for(int s=0; s<numSteps; s++)    {      yAxis.step(1);      xAxis.step(-1);    }    xPos -= numSteps;    yPos += numSteps;    //check where we are    Serial.print("x position: ");    Serial.println(xPos);    Serial.print("y position: ");    Serial.println(yPos);  }}  void moveNW(int numSteps){  int s=0;   if(!digitalRead(xStopMin) && !digitalRead(yStopMin) && s<numSteps){     for(int s=0; s<numSteps; s++)    {      yAxis.step(1);      xAxis.step(-1);    }    xPos -= numSteps;    yPos -= numSteps;    //check where we are    Serial.print("x position: ");    Serial.println(xPos);    Serial.print("y position: ");    Serial.println(yPos);  }} void reset(int numSteps){  for(int s=0; s<numSteps; s++){     if(!digitalRead(xStopMin)){       xAxis.step(-1);    }    if(!digitalRead(yStopMin) && s<numSteps){       yAxis.step(-1);    }  }   xPos = 0;  yPos = 0;  //check where we are  Serial.print("x position: ");  Serial.println(xPos);  Serial.print("y position: ");  Serial.println(yPos);}  void moveCirc(){  //draw circle at x,y 50,50  int CircleXCenter = 2;  int CircleYCenter = 2;   int CurXPos = xPos; //where the platform currently is in X  int CurYPos = yPos; //where the platform currently is in Y  int Rad = 1;  for (int i = 0; i < 360; i++)  {    //it does this for each point of the circle, so you dont have to run them at the same time    float       angle = i*2*3.14/360;    xPos = CircleXCenter + (cos(angle) * Rad);    yPos = CircleYCenter + (sin(angle) * Rad);    if(CurXPos < xPos)    {      xAxis.step(xPos - CurXPos);      //movexnow = Xpos - CurXPos;    }    else    {      xAxis.step(xPos - CurXPos);       //MoveXNow = CurXPos - Xpos;     }    if(CurYPos < yPos)    {      yAxis.step(yPos - CurYPos);       //   MoveYNow = Ypos - CurYPos;    }    else    {      yAxis.step(yPos - CurYPos        );       //MoveYNow = CurYPos - Ypos;     }    // step(xAxis);    //step(yAxis);    //<100, so -1     if(i == (360-1)) //we are at end of for loop, save current position.    {      CurYPos = yPos;      CurXPos = xPos ;     }  }}`

#### wildbill

#9
##### Apr 11, 2012, 04:15 pm
The reset routine looks like it should work, assuming that your limit switches do. If you move the plotter head to trigger the limit switches by hand, do you get the xMin and yMin messages on the serial monitor?

Try putting some serial debug inside the for loop in reset, for one thing, to see what the digitalRead is returning when the head hits the stop.

#### breegeek

#10
##### Apr 11, 2012, 05:24 pm
It seems I can only get the switches to actually function if I handle them separately, but then I can't get that nice diagonal interpolation that I am looking for.

Any thoughts?

Code: [Select]
`void reset(int numSteps){  for(int s=0; s<numSteps/2; s++){     if(!digitalRead(xStopMin)){       xAxis.step(-1);      }    else {      Serial.println("STOP x");      xPos = 0;      break;    }  }for(int s=0; s<numSteps/2; s++){     if(!digitalRead(yStopMin) && s<numSteps){       yAxis.step(-1);    }    else {       Serial.println("STOP y");      yPos = 0;      break;    }  } }//END RESET FUNCTION`

#### PeterH

#11
##### Apr 11, 2012, 05:55 pm
When you tested your limit switches, did you also test the case where both the 'zero' position switches operated at the same time?

#12
##### Apr 11, 2012, 06:12 pm
Is there a timing issue when reading the pins? Maybe you need a short delay between digitalRead calls (don't see why)?

I'd be tempted to re-write your function thus:

Code: [Select]
`void reset(int numSteps) {    int xMove = -1;    int yMove = -1;    for(int s=0; s<numSteps; s++){         if(digitalRead(xStopMin)) xMove = 0;        if(digitalRead(yStopMin)) yMove = 0;        xAxis.step(xMove);        yAxis.step(yMove);    }     xPos = 0;    yPos = 0;    }`

Stick a couple of serial.Write calls in there to show the values in xMove and yMove so you should see exactly when they are (or aren't) being reset.

#### wildbill

#13
##### Apr 11, 2012, 06:47 pm
Your NW and SW functions both have the same step instructions:
Code: [Select]
`      yAxis.step(1);      xAxis.step(-1);`

Once corrected, what does the version that needs -1 -1 do?

I think PeterH is on to something though, it looks like a wiring issue, not code.

#### breegeek

#14
##### Apr 12, 2012, 09:48 pm
Hey everyone, thanks for the help!

Tried every suggestion, then a friend of mine volunteered to help and wrote a new function for me. I think I understand, and it works!

Will keep y'all posted on new issues sure to arise!

Code (if you're curious):

Code: [Select]
`void resetFully(){  bool bMoveX = true;  bool bMoveY = true;  bool doReset = true;  int count = 0;  while (doReset == true){         if (digitalRead(xStopMin)) bMoveX = false;     if (digitalRead(yStopMin)) bMoveY = false;         count ++;         if (bMoveX == true) xAxis.step(-1);     if (bMoveY == true) yAxis.step(-1);         if (bMoveX == false && bMoveY == false){       doReset = false;     }     if (count == 8000){       doReset = false;     }  }  Serial.println("done!");  xPos = 0;  yPos = 0;  }void moveTo(int newX, int newY){    int xSteps = newX - xPos;  int ySteps = newY - yPos;  int stepDirectionX = (newX > xPos) ? 1 : -1;  int stepDirectionY = (newY > yPos) ? 1 : -1;  int nStepsX = abs(xSteps);  int nStepsY = abs(ySteps);      int maxSteps = abs(xSteps) > abs(ySteps) ? abs(xSteps) : abs(ySteps);    for (int i = 0; i < maxSteps; i++){    if (i < nStepsX){       bool bMoveX = true;       if (digitalRead(xStopMin)) bMoveX = false;       if (digitalRead(xStopMax)) bMoveX = false;       if (bMoveX == true){           xAxis.step(stepDirectionX);           xPos += stepDirectionX;       }    }        if (i < nStepsY){      bool bMoveY = true;       if (digitalRead(yStopMin)) bMoveY = false;       if (digitalRead(yStopMax)) bMoveY = false;       if (bMoveY == true){           yAxis.step(stepDirectionY);           yPos += stepDirectionY;       }    }    }    }`

Go Up