I am having a problem with my second for loop locking up the program. the first for loop runs 3 times like it should but the second does not. the second is used to stop the program to load a new part. This is my first try at programming for a cnc like machine.
#include <Stepper.h>
#define eocS 12 //sensor pin for adlen allows you adjust cut lenght with sensor
// initialize the stepper library on pins 8 and 10 on mystepper0.
// initialize the stepper library on pins 3 and 5 on mystepper1
Stepper myStepper0 = Stepper(1440, 8, 10);
Stepper myStepper1 = Stepper(200, 3, 5);
int cycle = 0;
const int PSTOP = 7; //var used for estop
const int mill = 2;
void setup() {
pinMode(eocS, INPUT);
pinMode(PSTOP, INPUT);
pinMode(mill, INPUT);
pinMode(13, OUTPUT);
// initialize the serial port:
//Serial.begin(9600);
}
void loop() {
cycle = 0; //resets cycle counter back to 0
myStepper1.setSpeed(800);
myStepper1.step(19557); //advance the table
delay(500);
digitalWrite (13, HIGH); //turn led on
for (int c = 0; c <= 2; c++) { //repeats three times cuts a hex
cycle++;
myStepper1.setSpeed(60); // cut speed
myStepper1.step (1200); // set cut lengh plus andlen set with sensor
myStepper1.setSpeed(600);
myStepper1.step (-1200); //move ccw to back out so to index
myStepper0.setSpeed(60);
myStepper0.step (960); //index 60 degrees
}
if (cycle == 3) { //starts the end of the cut
digitalWrite (13, LOW); //led off to know the cycle is finished
delay(100);
myStepper1.setSpeed(800);
myStepper1.step (-19557); //back table out to start position
}
for (int j = 0; j <= 3000; j++){ //ESTOP loop
digitalWrite (mill, LOW);
digitalWrite (13, HIGH); //led blinks shows ESTOP loop
delay(1000); //delay 1 second for blink
digitalWrite (13, LOW);
if (digitalRead(PSTOP) == LOW && digitalRead(mill) == HIGH) { //estop off mill on to start
break;
}
}
}
for (int j = 0; j <= 3000; j++)
{ //ESTOP loop
digitalWrite (mill, LOW);
digitalWrite (13, HIGH); //led blinks shows ESTOP loop
delay(1000); //delay 1 second for blink
digitalWrite (13, LOW);
if (digitalRead(PSTOP) == LOW && digitalRead(mill) == HIGH)
{ //estop off mill on to start
break;
}
}
The 'if' statement is waiting for the PSTOP input to be LOW and the 'mill' input to be HIGH. Do you have the inputs set that way? REMEMBER: You need a pull-up or pull-down resistor for each switch.
The rest of the loop will set the LED on for a second and then blink it off too quickly to see, 3001 times. If you wait about five minutes, everything will start over.
It is very unlikely that a CNC program will work properly using delay()s all over the place. The functions delay() and delayMicroseconds() block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.
FOR and WHILE loops also block the Arduino and may not be appropriate. It is usually better to use a variable to keep track of position and allow loop() to deal with the repetition. Have a look at the second example in this Simple Stepper Code
if (digitalRead(PSTOP) == LOW && digitalRead(mill) == HIGH)
{ //estop off mill on to start
break;
}
}
The 'if' statement is waiting for the PSTOP input to be LOW and the 'mill' input to be HIGH. Do you have the inputs set that way? REMEMBER: You need a pull-up or pull-down resistor for each switch.
The rest of the loop will set the LED on for a second and then blink it off too quickly to see, 3001 times. If you wait about five minutes, everything will start over.
Yes john that loop. This is all new to me and having a hard time following all the different responses. the pstop button goes to ground and the mill button passes 5 volts. The led is there just as a way to see where the program was hanging, they are not needed to do what I want to do. Just a quick run down of what I am trying to do. the mill has 2 cutters spaced 1/4 apart and the slide has a index head with the part in it. the slide goes into the cutters a set distance to cut both sides of the part. It backs up and indexes 60 degrees it does this a total of 3 times to cut a hex shaft. That part of the program works fine. it s getting it to stop so I can load in a new part that does not. I need to read a little more about the pullup and pull down resistor i think. I do not get the pullup part. thank you for the the help and anything else you can think of.
You say "the mill button passes 5 volts". Does that mean the button is wired between +5V and your input pin? If so, you need a pull-down resistor to keep the pin LOW when the button is not closed. The pull-down resistor is typically 10k and wired between the input pin and ground. That means you have two things connected to your input pin: one side of the button and one side of the pull-down resistor. Without the pull-down, the input could read HIGH whether the button is pressed or not.
You say "the pstop button goes to ground". Does that mean the button is wired between Ground and your input pin? If so, you need a pull-up resistor to keep the pin HIGH when the button is not closed. You can use the internal pull-up by setting the pinMode() to INPUT_PULLUP instead of just INPUT. You could use an external pull-up resistor (typically 10k) wired between the input pin and +5V. That means you would have two things connected to your input pin: one side of the button and one side of the pull-up resistor. Without the pull-up, the input could read LOW whether the button is pressed or not
Thanks John I just finished reading about inputs and resistors I came to the same answer as you gave me . Thanks for pointing me in the right direction. Still have a few things to do but at least I am closer to the end.