Some help with this code URGENT :)

Hey guys This is a code for a pneumatic punching machine. works like a 3d printer. 2 steppers on perpendicular axis. it should move as follows: motor 1 move to X location, motor 2 Move to Y location then switch to relay to activate the cylinder and punch, motor 1 move back to the original location, motor 2 move back to original location and repeat for second coordinate and so on, 4 coordinates in total.
last night the code was working but i might have messed something up. i basically need to copy and paste those 5 motions to be able to punch 4 holes into a square sheet. i think the problem is with the copying and placing the second code. help would be appreciated i have just under 5 hours to have this ready to punch 4 holes. help would be much appreciated. Thank you. ps. i know to some of you pros this must be toxic but just let it slide.

// pins numbers
const int stepPin1 = 3;
const int dirPin1 = 4;
const int stepPin2 = 7;
const int dirPin2 = 6;

void setup() {
// Sets the two pins as Outputs
pinMode(stepPin1,OUTPUT);
pinMode(dirPin1,OUTPUT);
pinMode(stepPin2,OUTPUT);
pinMode(dirPin2,OUTPUT);
pinMode(LED_BUILTIN,OUTPUT);
}
void loop(){

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
delay(3000);
digitalWrite(4,LOW); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x <1000; x++) {
digitalWrite(3,LOW);
delayMicroseconds(1000);
digitalWrite(3,HIGH);
delayMicroseconds(1000);
}
delay(2000); // One second delay
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
digitalWrite(6,LOW); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x <1000; x++) {
digitalWrite(7,LOW);
delayMicroseconds(1000);
digitalWrite(7,HIGH);
delayMicroseconds(1000);
}
delay(2000); // One second delay
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(2000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(2000); // wait for a second

//////////////////////////////////////////////////////////////////////////////////////////////////////////
digitalWrite(4,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x <1000; x++) {
digitalWrite(3,HIGH);
delayMicroseconds(1000);
digitalWrite(3,LOW);
delayMicroseconds(1000);
}
delay(2000); // One second delay
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
digitalWrite(6,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x <1000; x++) {
digitalWrite(7,HIGH);
delayMicroseconds(1000);
digitalWrite(7,LOW);
delayMicroseconds(1000);
}
delay(2000); // One second delay

digitalWrite(4,LOW); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x <5000; x++) {
digitalWrite(3,LOW);
delayMicroseconds(1000);
digitalWrite(3,HIGH);
delayMicroseconds(1000);
}
delay(2000); // One second delay
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
digitalWrite(6,LOW); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x <5000; x++) {
digitalWrite(7,LOW);
delayMicroseconds(1000);
digitalWrite(7,HIGH);
delayMicroseconds(1000);
}
delay(2000); // One second delay
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(2000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(2000); // wait for a second

digitalWrite(4,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x <5000; x++) {
digitalWrite(3,HIGH);
delayMicroseconds(1000);
digitalWrite(3,LOW);
delayMicroseconds(1000);
}
delay(2000); // One second delay
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
digitalWrite(6,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x <5000; x++) {
digitalWrite(7,HIGH);
delayMicroseconds(1000);
digitalWrite(7,LOW);
delayMicroseconds(1000);
}
delay(2000); // One second delay

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor you will not have access to this useful tool but it's still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

Please remove unnecessary blank lines from your code before posting to the forum. One or two to separate code into logical sections is fine but large spaces for no reason or random blank lines just make for more scrolling when we're trying to read your code. Do not post double spaced code.

mech1up_forher:
last night the code was working but i might have messed something up.

Please explain exactly what the trouble is.

Maybe the fact that you're missing a } at the end of your code?

By the way, it would make your code easier to understand if you used decent names for your pin numbers (after all, one set is for X and one set is for Y) and if you consistently used those names. You use the names in pinMode but not in digitalWrite.

Alright it was the } at the end of the code and had to remove it when i copied the codes. i apologize for all the messiness in this post but i really have no background in programming. i only understand some of the logic behind it. Thanks alot boys

You have so much repeating code that it's calling for functions.

Write a function to move a motor

/*
  move a motor; motor identified by dirPin and stepPin
  IN:
    dirPin      pin to set the direction pin of the motor
    direction   direction to move (HIGH = CW, LOW = CCW)
    stepPin     pin that moves the motor
    numSteps    number of steps
*/
void moveMotor(int dirPin, int direction, int stepPin, int numSteps)
{
  digitalWrite(dirPin, direction);
  for (int x = 0; x < numSteps; x++)
  {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1000);
  }
}

Note that the comment states that HIGH equals CW and LOW equals CCW; you might have to adjust

Next you can write two functions, one for each motor.

/*
  move X motor
  IN:
    direction   direction to move (HIGH = CW, LOW = CCW)
    numSteps    number of steps to move
*/
void moveMotorX(int direction, int numSteps)
{
  moveMotor(dirPin1, direction, stepPin1, numSteps);
}

/*
  move Y motor
  IN:
    direction   direction to move (HIGH = CW, LOW = CCW)
    numSteps    number of steps to move
*/
void moveMotorY(int direction, int numSteps)
{
  moveMotor(dirPin2, direction, stepPin2, numSteps);
}

And now in loop() you can call the moveMotorX and moveMotorY functions like

void loop()
{
  moveMotorX(HIGH, 5000);
  delay(2000);
  moveMotorY(HIGH, 5000);
  delay(2000);
  ...
  ...
}

You can also write a function punch() and call that from loop().

And in the next step you can 'define' motors using structs or classes. They are like a record in a database and combine related information (in this case tthe pins that control a motor). I'm not much of an C++ (OOP) programmer and hence use a struct.

// struct to hold pins for specific motor
struct MOTOR
{
  const int stepPin;
  const int dirPin;
};

// pins for motor X
struct MOTOR motorX =
{
  3,
  4
};

// pins for motor Y
struct MOTOR motorY =
{
  7,
  6
};

This first defines a struct with the pins that control the motor and next defines two variables called motorX and motorY.

Now you can write a function to move a motor

/*
  control a motor
  IN:
    motor: motor to control
    direction: direction to move
    numSteps: number of steps
*/
void moveMotor(struct MOTOR motor, int direction, int numSteps)
{
  digitalWrite(motor.dirPin, direction);
  for (int x = 0; x < numSteps; x++)
  {
    digitalWrite(motor.stepPin, HIGH);
    delayMicroseconds(1000);
    digitalWrite(motor.stepPin, LOW);
    delayMicroseconds(1000);
  }
}

Compared to the previous version, you pass the struct that defines the pins; further it's basically the same.

And in loop(), you can now use something like

void loop()
{
  moveMotor(motorX, HIGH, 5000);
  moveMotor(motorY, LOW, 1000);
  ...
  ...
}

The only omission now is that it's not clear if HIGH indicates forward or that HIGH indicates backward. You can add two #defines as the first lines of your code.

#define FORWARD HIGH
#define BACKWARD LOW

struct MOTOR
{
...
...

Adjust to your needs (swap HIGH and LOW around if needed) and you can replace FORWARD by CW and BACKWARD by CCW if that makes more sense.

And in loop()

[code]void loop()
{
  moveMotor(motorX, FORWARD, 5000);
  moveMotor(motorY, BACKWARD, 1000);
  ...
  ...
}

Full code example

#define FORWARD HIGH
#define BACKWARD LOW

// struct to hold pins for specific motor
struct MOTOR
{
  const int stepPin;
  const int dirPin;
};

// pins for motor X
struct MOTOR motorX =
{
  3,
  4
};

// pins for motor Y
struct MOTOR motorY =
{
  7,
  6
};

void setup()
{
  pinMode(motorX.stepPin, OUTPUT);
  pinMode(motorX.dirPin, OUTPUT);

  pinMode(motorY.stepPin, OUTPUT);
  pinMode(motorY.dirPin, OUTPUT);

  pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
  moveMotor(motorX, FORWARD, 5000);
  moveMotor(motorY, BACKWARD, 1000);
}


/*
  control a motor
  IN:
    motor: motor to control
    direction: direction to move
    numSteps: number of steps
*/
void moveMotor(struct MOTOR motor, int direction, int numSteps)
{
  digitalWrite(motor.dirPin, direction);
  for (int x = 0; x < numSteps; x++)
  {
    digitalWrite(motor.stepPin, HIGH);
    delayMicroseconds(1000);
    digitalWrite(motor.stepPin, LOW);
    delayMicroseconds(1000);
  }
}

Code compiles, not tested.

Note: code provided as guide line, it does not do what yours does.

Damn that really helps making it easier to input the coordinates, i'll start working on it now but i cant really test it untill i get to plug in the device which is 30 mins before the testing. if i get it to work i'll definitely use this. but if not i'll have to jst go with my ass of a code. Thanks for your help