Interrupt won't get Enabled

Well, I have this code that works perfectly, it uses three pins to control some Shift Registers that are for reading some buttons, and also has a midi output port using AltSoftserial, the thing is that I am using another Atmega328 to generate a clock signal for the Midi Port, so it can be more accurate, I'm have tried to interrupt pin 2 and 3, on Rising and Falling Edge, but the code crashes when I do so, maybe it has something to do with the Libraries I am using.

This is the code that crashes (I think), Interrupt gets Enabled, but the ISR never runs...

void SetClockMode(bool clock_mode)
{
  switch(clock_mode)
  {
    case INTERNAL_CLOCK: detachInterrupt( digitalPinToInterrupt(3) );                  break;
    case EXTERNAL_CLOCK: attachInterrupt( digitalPinToInterrupt(3), CHANGE, ExtClock); break; 
  }

}

void ExtClock()
{
  Midi.ClockMessage();
  //Serial.println(0);
}

// This is the code inside the MIDI class

void ClockMessage()
{
  Port.write(MIDI_CLOCK);  //Port is the AltSoftserial Port
}

I have attached a Zip file containing the files the sketch uses + the libraries, sketch is divided in 9 tabs, so I cant directly upload a file, sorry for that :frowning:

a_MidiControlle_main.zip (77 KB)

Opening up an unknown zip file can be risky and I will not do it.

You have not shown complete code so it is hard to guess what is going on, especially since "code crashes" means many different things to different people and you have not explained what it means in this case.

I do not know AltSoftserial specifically, but it is often a bad idea to try to do serial input or serial output from within an ISR.

Have you tried commenting out the code in the ISR to see if that makes a difference? Have you tried an ISR that just increments a volatile counter each time it gets called to see if the ISR gets called and how many times it gets called?

It is guesswork to figure out the value of MIDI_CLOCK and the value of clock_mode.

vaj4088:
You have not shown complete code so it is hard to guess what is going on, especially since "code crashes" means many different things to different people and you have not explained what it means in this case.

This is the code that crashes (I think), Interrupt gets Enabled, but the ISR never run...

vaj4088:
I do not know AltSoftserial specifically, but it is often a bad idea to try to do serial input or serial output from within an ISR.

The software Serial function only writes a byte, is a sync message, so it must be done this way...

vaj4088:
Opening up an unknown zip file can be risky and I will not do it.

How else can I upload the code??

vaj4088:
Have you tried commenting out the code in the ISR to see if that makes a difference? Have you tried an ISR that just increments a volatile counter each time it gets called to see if the ISR gets called and how many times it gets called?

Yes, I have, indeed it is the ISR function that does not get attached, I tried putting a Serial.print inside the ISR, but that won't work.

vaj4088:
It is guesswork to figure out the value of MIDI_CLOCK and the value of clock_mode.

clock_mode is a boolean so it's either 0 or 1. 1 when enabling the interrupt 0 for disabling the interrupt.

INTERNAL_CLOCK is defined to be 0
EXTERNAL_CLOCK is defined to be 1

I can post my code but it would be several posts long, I think it might be a memory issue.

The pulse frequency the pin is recieving is about 48 Hz

Look at the bottom of the reply page, (not quick reply), click "Attachments and other options", click "Browse", find the file in your sketchbook folder, highlight it and click "OPEN".
HowToPost

attachInterrupt( digitalPinToInterrupt(3), CHANGE, ExtClock);

This is incorrect syntax. Use

attachInterrupt( digitalPinToInterrupt(3), ExtClock, CHANGE);

https://www.arduino.cc/en/Reference/AttachInterrupt

cattledog:

attachInterrupt( digitalPinToInterrupt(3), CHANGE, ExtClock);

This is incorrect syntax. Use

attachInterrupt( digitalPinToInterrupt(3), ExtClock, CHANGE);

attachInterrupt() - Arduino Reference

Duhhh such a dumb mistake! , thanks! ::slight_smile: :stuck_out_tongue_closed_eyes:

If you didn't get these compiler warnings you should turn up your warning level in Preferences:

BareMinimum.ino:3:60: warning: invalid conversion from 'int' to 'void (*)()' [-fpermissive]
 attachInterrupt( digitalPinToInterrupt(3), CHANGE, ExtClock);
                                                            ^
In file included from BareMinimum.ino.cpp:1:0: Arduino.h:150:6: note: initializing argument 2 of 'void attachInterrupt(uint8_t, void (*)(), int)'
 void attachInterrupt(uint8_t, void (*)(void), int mode);
      ^

BareMinimum.ino:3:60: warning: invalid conversion from 'void (*)()' to 'int' [-fpermissive]
 attachInterrupt( digitalPinToInterrupt(3), CHANGE, ExtClock);
                                                            ^
In file included from BareMinimum.ino.cpp:1:0: Arduino.h:150:6: note: initializing argument 3 of 'void attachInterrupt(uint8_t, void (*)(), int)'
 void attachInterrupt(uint8_t, void (*)(void), int mode);
      ^

If you DID get these warnings and ignored them, shame on you. :frowning: