Creating Multiple Interrupts

I am trying to create interrupts on pins 8-15. Basically making a CNC with momentary switches that will limit the range and allow for homing back to zero again.

I'm new at this and really could use someone's guidance.
Thus far, I've created the following code from a few snippets I've found online in an attempt to trigger a serial print with the interrupt.

It doesn't work unfortunately, and I think it has to dow ith the PCICR and PCMSK1 settings.
Any help is appreciated.
Thank you,
Miguel

void setup()
{
  Serial.begin(9600);
  Serial.println("test");
  InitialiseIO();
  InitialiseInterrupt();
   		// turn interrupts back on
}

void loop() {
  
  Serial.println(digitalRead(8));
  Serial.println(digitalRead(9));
  Serial.println(digitalRead(10));
  Serial.println(digitalRead(11));
  Serial.println(digitalRead(12));
  Serial.println("-------");
  delay(1000);
}






void InitialiseIO(){
  pinMode(8, INPUT);	   // Pin 8 is input to which a switch is connected
 
  pinMode(9, INPUT);	   // Pin 9 is input to which a switch is connected

  pinMode(10, INPUT);	   // Pin 10 is input to which a switch is connected

  pinMode(11, INPUT);	   // Pin 11 is input to which a switch is connected
  
  pinMode(12, INPUT);	   // Pin 12 is input to which a switch is connected
}

void InitialiseInterrupt(){
  cli();		// switch interrupts off while messing with their settings  
  PCICR =0x02;          // Enable PCINT1 interrupt
  PCMSK1 = 0b00000111;
  sei();
 
}

ISR(PCINT1_vect) {    // Interrupt service routine. Every single PCINT8..14 (=ADC0..5) change
            // will generate an interrupt: but this will always be the same interrupt routine
  if (digitalRead(8)==1)  Serial.println("8");
  if (digitalRead(9)==1)  Serial.println("9");
  if (digitalRead(10)==1)  Serial.println("10");
  if (digitalRead(11)==1)  Serial.println("11");
  if (digitalRead(12)==1)  Serial.println("12");
}

BTW. I'm using the Arduino Mega. I think this might influence the PCINT1 Interrupt.

First snippets from web are usually wrong.. be careful

to do pin change each pin goes to its own vector and each has to enabled in its group.
also you reaqlly should not try to serial printin interrupt routine it means you have system interrupts within your ISR and timing gets screwed

Set a boolean in ISR and check it in Loop and serial print if its set meaning you have been been in ISR.

I'm just going out send me a message with more details will outline some code for you..

It's not a question of "trying not" to use Serial.print inside an ISR. It's a case of NEVER use Serial.print() inside an ISR.

The serial TX buffer is emptied as it transmits, which is done by an interrupt. When inside another interrupt that Serial ISR won't ever run, so as soon as you fill up the (small) serial TX buffer the sketch will just sit and wait in an endless while loop waiting for room in the buffer that will never appear.

Sorry disagree the serial transmit buffer is long enough to handle a short message, the users interrupt will yield and the tx interrupt gets in, the problem is

  1. if the user interrupt does not yield.

  2. you try write to much

3 Any timing on system may go wrong.. I have designed an RTOS for Arduino without resorting to rewrite of serial .. most of the rest but not serial its code is efficient..

It seems I may be going about it the wrong way.
I found this sample code on the site here.
http://playground.arduino.cc/Main/PinChangeIntExample#.U0FvK62SywN

Problem is when I run it, it says PCintPort has not been declared.
I assume this is happening because I don't have the correct .h file.

Any guidance on this?

You are, of course, assuming the TX buffer is empty. What if you only want to send 3 characters (say, the number 200), yet the buffer, because of what you are already sending elsewhere, only has 2 characters remaining in it? Or worse, non?

For example, your ISR triggers while the sketch is already waiting for room in the serial buffer. Oops - crash and burn...

Remember: serial is SLOW - so slow your sketch may be spending a lot of its time waiting on that serial buffer to get some room in it.

So all in all, using Serial inside an ISR is bad, no matter which way you look at it.

Sorry but this is going way over my head.
Isn't there a simple example of making a pin interrupt out of any pin out there?

Have you installed the library?

I strongly suspect your application doesn’t need any interrupts. Only use them if you are confident you can debug the complex problems they can cause. As you started by putting Serial.print() statements in your ISR I suspect you are very new to this.

If you really want to use interrupts you should read all the details in the Atmega32b datasheet.

…R

Have you installed the library?

I'm still trying to find it.

I strongly suspect your application doesn't need any interrupts. Only use them if you are confident you can debug the complex problems they can cause. As you started by putting Serial.print() statements in your ISR I suspect you are very new to this.

I'm creating a cnc machine that uses the arduino to read g-code using code from MargianallyClever.com
The way the code is written, it waits for the stepper motors to completely go through all of the steps before reading anymore information from the serial port.
I want these momentary switches to stop the motors (which are driven by ada fruit motor shields) when triggered.
I've tried adding code into the loop but it doesn't work.
I tried using the basic interrupts on the Mega that are built in and that works but I want to shift over to different pins to trigger the interrupts.

The way the code is written, it waits for the stepper motors to completely go through all of the steps before reading anymore information from the serial port.

What has reading the serial port got to do with reading limit switches ?

Don't get side tracked by our chat on Serial your answer to a CNCv is in my view perfectly acceptable althoughmight suggest another option rather than individual pin interrupts.

Whats the rate of change you are looking at ? i.e. what timing could you accommodate between hitting switch and motor turning off?

Before I took up training/consultancy etc., I designed industrial control rairly did we try and interrupt monitor all inputs in fact only the emergency stop.

Rather we read the inputs using a time tick interrupt, meant we could debounce the limit switchs as well at a known rate.

Have got back in, so if you want to message me with an email will send you a lump of code for

a. setting up timer interrupt and read and debounce inputs.

Your main loop can then monitor this and action as required..

What has reading the serial port got to do with reading limit switches ?

I don't know. But let's say I send some Gcode to the Stepper motor and tell it to turn 1000 Steps.
And if I am running the serial monitor, it spits out a copy of the Gcode command after it's done.
Then continues updating the serial monitor.
It seems it is waiting for the stepper motor to go through all of its steps before it executes any other code.
I'm new at this so It's a little confusing to me.

Whats the rate of change you are looking at ? i.e. what timing could you accommodate between hitting switch and motor turning off?

Not sure. The Arduino is running at 57600.
Not sure I follow you about the time tick interrupt but I'm open to options.
The goal here is to run a function once those momentary switches are clicked so my machine doesn't break if it goes beyond the limits.

It seems it is waiting for the stepper motor to go through all of its steps before it executes any other code.

That’s going to depend on how the code is written.

One way would be to command the stepper to move 1000 steps then check if the limit has been reached. A better way would be to command the stepper to move 1 step, check if the limit has been reached, stop moving if has and to do that 1000 times, or at least to use that principle.

What speed are steppers doing…
Will say a 20mS delay take them beyond the limit switch and cause damage…
IF not assume the software each 10mS repetitively executes a timer driven interrupt routine to read switchs.>
We use two reads to confirm the state switchs can bounce … We set criteria of switch states into a look up table, so once you have a new configuration the look up in the table causes function call to next action

Simple example

3 inputs if I make up a sequence we could say
000
001
010
011
100
101
110
111
state 000 means start
001 never occurs
010 switch next stage
011 stop first etc…
now with the index into this table i.e entry 0,1,2,3,4,… we index into a function pointer table
this is defined as …

typedef void(*func_ptr)();
func_ptr basic[15] = {finc1,func2,fun3,func4,…}

perfectly valid c

these array entrys are function names… you may never have seen such an idea but its common to many industrial control sequence controllers. With the idea I can in fact have all standard operations in order or not in sequence in the array…

From the input pattern I can then algorithmically determine what sequence number to execute and call the function through the array… using just a mathematically derived answer,

Perhaps this is too advanced for you but if your serious about cnc or any other sequence driven system you need these sort of ideas to handle all possible error combinations as well as vaid sequencing, believe me I have broken more process lnes than I care to remember simply because I was not aware of these ideas as a younger engineer…

Well, this is why I was trying to do it with the interrupt switches built into the Arduino.
I have gotten this to work with the regular interrupt from the Arduino but I am still looking to switch the pins where I can do the interrupt from.

I think what you have written is currently beyond my level of expertise but I appreciate you putting it out there.

UKHeliBob:
A better way would be to command the stepper to move 1 step, check if the limit has been reached, stop moving if has

+1

That's what I had in mind in my earlier post.

...R