help needed by amateur programmer

Hello there,

As a project I'm trying to build a CNC machine driven by arduino stepper motors using AccelStepper.

However, after making (in my opinion) a very simple program, it won't work.
Sometimes simply by resetting the shield and rerunning the program a certain stepper motor will all of a sudden do something instead of previously standing still.

Basically what I'm trying to do is move a drill from it's start position to 3 different coordinates, drilling a hole at each of them. The machine moves the drillable object first to the corresponding x and y coordinates and then the drill goes down and back (z axis).

Does anyone know what the problem is?

Arduino code:

#include <AccelStepper.h>
#include <math.h>
#include <Stepper.h>

#define XSTEP 2
#define YSTEP 3
#define ZSTEP 4

#define XDIR 5
#define YDIR 6
#define ZDIR 7

#define ENABLEPIN 8

#define LEDPIN 13

#define STEPS (float)200
#define DEGREES (float)360

float C = 5.3052;

float XEind1 = (float)(2000 * (float)C);
float YEind1 = (float)(4000 * (float)C);
float XEind2 = (float)(4000 * (float)C);
float YEind2 = (float)(2000 * (float)C);

float XEind3 = (float)(2000 * (float)C);
float YEind3 = (float)(4000 * (float)C);

float ZEind = (float)(1500 * (float)C);
int Move = 1;

AccelStepper stepperX(1, XSTEP, XDIR);
AccelStepper stepperY(1, YSTEP, YDIR);
AccelStepper stepperZ(1, ZSTEP, ZDIR);

void setup()
{

pinMode(LEDPIN, OUTPUT);
pinMode(ENABLEPIN, OUTPUT);

Serial.begin(9600);
Serial.println("Opstarten");

digitalWrite(ENABLEPIN,LOW);

digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);

stepperX.setSpeed(400);
stepperY.setSpeed(400);
stepperZ.setSpeed(400);

stepperX.setAcceleration(400);
stepperY.setAcceleration(400);
stepperZ.setAcceleration(400);

stepperX.setCurrentPosition(0);
stepperY.setCurrentPosition(0);
stepperZ.setCurrentPosition(0);
}

void loop ()
{

// Process 1# --------------------------------------------------------------------------

while (Move==1) // X1 & Y1
{
stepperX.moveTo(XEind1);
stepperY.moveTo(YEind1);
if (stepperX.currentPosition() != XEind1 + stepperY.currentPosition() != YEind1)
{
stepperX.run();
stepperY.run();
}
else
{
Move = 2;
}
}

while (Move==2) // Z1 heen
{
stepperZ.moveTo(2000);
if (stepperZ.currentPosition() != 2000)
{
stepperZ.run();
}
else
{
Move=3;
}
}

while (Move==3) // Z1 terug
{
stepperZ.moveTo(4000);
if (stepperZ.currentPosition() != 4000 )
{
stepperZ.run();
}
else
{
Move=4;
}
}

// Process 2# --------------------------------------------------------------------------

while (Move==4) // X2 & Y2
{
stepperX.moveTo(XEind2);
stepperY.moveTo(YEind2);
if (stepperX.currentPosition() != XEind2 & stepperY.currentPosition() != YEind2)
{
stepperX.run();
stepperY.run();
}
else
{
Move = 5;
delay(500);
}
}

while (Move==5) // Z2 heen
{
stepperZ.moveTo(ZEind);
if (stepperZ.currentPosition() != ZEind)
{
stepperZ.run();
}
else
{
Move=6;
delay(500);
}
}

while (Move==6) // Z2 terug
{
stepperZ.moveTo(0);
if (stepperZ.currentPosition() != 0)
{
stepperZ.run();
}
else
{
Move=7;
delay(500);
}
}

// Process 3# --------------------------------------------------------------------------

while (Move==7) // X3 & Y3
{
stepperX.moveTo(XEind3);
stepperY.moveTo(YEind3);
if (stepperX.currentPosition() != XEind3 & stepperY.currentPosition() != YEind3)
{
stepperX.run();
stepperY.run();
}
else
{
Move = 8;
delay(500);
}
}

while (Move==8) // Z3
{
stepperZ.moveTo(ZEind);
if (stepperZ.currentPosition() != ZEind)
{
stepperZ.run();
}
else
{
Move=9;
delay(500);
}
}

while (Move==9) // Z3
{
stepperZ.moveTo(0);
if (stepperZ.currentPosition() != 0)
{
stepperZ.run();
}
else
{
Move=10;
delay(500);
}
}

while (Move==10)
{
stepperX.moveTo(0);
stepperY.moveTo(0);
if (stepperX.currentPosition() != 0 & stepperY.currentPosition() != 0)
{
stepperX.run();
stepperY.run();
}
else
{
stepperX.stop();
stepperY.stop();
}
}
}

Thanks in advance

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum
Your code is much too long for me to study quickly without copying to a text editor.

I don't see any sign of Serial.print() statements in the program to allow you to monitor what is happening.

...R
Stepper Motor Basics
Simple Stepper Code

float XEind1 = (float)(2000 * (float)C);
float YEind1 = (float)(4000 * (float)C);
float XEind2 = (float)(4000 * (float)C);
float YEind2 = (float)(2000 * (float)C);

Why do you feel it necessary to cast a float to a float? Maybe you need to float a few more floats into that code.

Why are those variables floats? You don't REALLY expect to be able to take 21220.8 steps, do you?

I haven't used this library, but I did notice something strange in your code so I checked it and my theory seems to be valid.

Start by realizing that the Loop will be executed over and over again very fast.
The first time it is called, Move = 1 and that Portion of the code runs and you execute two moves. The run command attempts to move, starting slow because of the desired acceleration.
The code stays in a Loop but also keeps reissuing the move commands. With acceleration enabled, each move reinitializes the acceleration. This could be causing Problems.
I think it would be wiser to issue the move commands BEFORE starting the while Loop.

Also, once MOVE = 10, it never gets changed. The Loop will end, restart and continually move to Position 0.

I understand that you are an "Amateur" as you put it, but you Need to work on the program structure.

The code stays in a Loop but also keeps reissuing the move commands.

If I told you to walk to the end of the street, by taking one step at a time, and, each time that you were not at the end of the street, I told you to go to the end of the street and take a step, would it matter that I had told you 22000 times to go to the end of the street?

The repeated calls to moveTo() are useless, but they are not a problem.

The code COULD use some improvement, but the real problem is that we, and OP, don't have enough data to know what the real problem is.

    if (stepperX.currentPosition() != XEind1 + stepperY.currentPosition() != YEind1)

I think that your problem is in comparing integers to floats. I would start by changing all positions from float to long.

johnwasser:

    if (stepperX.currentPosition() != XEind1 + stepperY.currentPosition() != YEind1)

I think that your problem is in comparing integers to floats. I would start by changing all positions from float to long.

The + in the if statement is likely not right, either. Nor are the & in the rest of the code. The proper operator is &&.