Help Needed for GRBL/CNC alteration

Programming help
I am building a machine and need a couple of additions to the GRBL code, firstly I need to alter the interrupt routine so it will Start and Pause the operation using a single switch on the nano. I have tried to alter this myself and it is too unreliable.

The next part is to add a Custom M Code that will Pause the operation of all the CNC tasks until another switch on another pin has cycled a set number of times that has been passed to GRBL using the P parameter. eg.. M667 P5 would pause all execution until the switch has been triggered exactly 5 times then the code resumes.

Essentially what is being built is a single driven axis CNC machine, The gantry for the X axis allows the head to travel in the X axis and the material is feed manually on the Y axis with the distance being read by the switch. Each switched input equates to a distance.

So when the machine starts it sets its position to Zero, runs the X axis until it comes to an M code then pauses until the clicks on the switch = the distance the Y requires to move, once there the X axis resumes.

I have picked my way through the GRBL kind of know where things should be however my experance in programming is a little light and I think it needs someone who is well versed in C to work on this with me.

I have limited funds available and am happy to re-emberse someone who can assist with this or shout beers :slight_smile: I dont expect such alterations to take up too much time for someone who has a great deal of experience and suspect that what is missing is some kind of denounce code on the interrupt's and a few additions to the control structure of the in the gcode handler.

and suspect that what is missing is some kind of denounce code on the interrupt's

Why do you want to denounce interrupts? They are most likely not needed in your case, but they DO have their place.

Reliably reading switch states is easy. That you are having difficulties means that you should post your code and explain what problems you are seeing.

Thank you PaulS, Here is what I have altered in the code so far.

If the machine is running and the button is triggered it should HALT, and if its triggered again it should RESUME. However I am getting the HALT but when i try to RESUME it resumes for a few seconds then halts again. sometimes multiple clicks cause it to resume properly but it almost seems to be random.

ISR(CONTROL_INT_vect)
{
uint8_t pin = (CONTROL_PIN & CONTROL_MASK);
#ifndef INVERT_CONTROL_PIN
pin ^= CONTROL_MASK;
#endif
// Enter only if any CONTROL pin is detected as active.
if (pin) {
// Set the timer overflow from when the first click was
if (bit_istrue(pin,bit(RESET_BIT))) {
mc_reset();
// One click should start the cycle Needs some form of Debounce
} else if (bit_istrue(pin,bit(CYCLE_START_BIT)) && SUSPEND_FLAG == 1 ) {
bit_true(sys.rt_exec_state, EXEC_FEED_HOLD);
SUSPEND_FLAG = 0;
}
//another click should pause the cycle Needs some form of Debounce
else if (bit_istrue(pin,bit(CYCLE_START_BIT)) && SUSPEND_FLAG == 0) {
bit_true(sys.rt_exec_state, EXEC_CYCLE_START);
SUSPEND_FLAG = 1;
}
}
}

FIXED Part 1. Pin 2 now acts as a Start/Stop pin with no debounce circuit. :slight_smile:

I checked the way the limit switches work and added the WDT delay the same as they use. Seems to work OK however the real test will be when it goes in to the machine.

ISR(CONTROL_INT_vect) { if (!(WDTCSR & (1<<WDIE))) { WDTCSR |= (1<<WDIE); } }
ISR(WDT_vect) // Watchdog timer ISR
{
WDTCSR &= ~(1<<WDIE); // Disable watchdog timer.
uint8_t pin = (CONTROL_PIN & CONTROL_MASK);
#ifndef INVERT_CONTROL_PIN
pin ^= CONTROL_MASK;
#endif
// Enter only if any CONTROL pin is detected as active.
if (pin) {
// Set the timer overflow from when the first click was : vangalvin
if (bit_istrue(pin,bit(RESET_BIT))) {
mc_reset();
// One click should start the cycle Needs some form of Debounce
}
if (bit_istrue(pin,bit(CYCLE_START_BIT)) && SUSPEND_FLAG == 1 ) {
bit_true(sys.rt_exec_state, EXEC_FEED_HOLD);
SUSPEND_FLAG = 0;
}
//another click should pause the cycle Needs some form of Debounce
else if (bit_istrue(pin,bit(CYCLE_START_BIT)) && SUSPEND_FLAG == 0) {
bit_true(sys.rt_exec_state, EXEC_CYCLE_START);
SUSPEND_FLAG = 1;
}
}
}

I can not understand someone building this kind of program, for this kind of machine, that thinks buttons are useful for more than keeping shirts closed. The proper term is, and has always been SWITCH!.

The proper term is momentary contact N.O. push switch. button/pushbutton is less of a mouthful.

MarkT:
The proper term is momentary contact N.O. push switch. button/pushbutton is less of a mouthful.

The type of switch doesn't really matter.

perhaps then a SP/ST/NO momentary will describe it fully and remember that a switch does NOT have to be push button . It could just as easily be a toggle action with a return spring. So Push button only describes the mechanical action and not the electrical action.