Arduino Interrupt and I2C Communication

Hello guys,
I am new with Arduino and I need some help with this error.
I have a magnetic switch interrupt that is counting the rotations of an object. I need to send the RPMs value via I2C to the master Arduino but the loop function freezes even without the I2C code.

Heres the relevant code:

int firstMagneticSensor = 2;               // choose the input pin
int secondMagneticSensor = 3;               // choose the input pin
volatile int firstMagneticSensorValue = 0;
volatile int rpm = 0;
volatile unsigned long firstMagneticSensorTime = 0;
const long debouncingInterval = 1000;  

void setup() {
    pinMode(firstMagneticSensor, INPUT);     // declare pushbutton as input
    pinMode(secondMagneticSensor, INPUT);     // declare pushbutton as input
    Serial.begin(115200);  // ONLY DEBUG
    attachInterrupt(digitalPinToInterrupt(firstMagneticSensor), firstMagneticSensorActive, RISING);
    attachInterrupt(digitalPinToInterrupt(secondMagneticSensor), secondMagneticSensorActive, RISING);

}

void loop(){  
     String rotation_str = String(rotationDirection, DEC);
     String rpm_str = String(rpm, DEC);
     String to_send = String("02;" + ";" + rpm_str);
     char buffer[8];
     to_send.toCharArray(buffer, 32); 
     Serial.print("Sent: ");        
     Serial.println(buffer);
}

void proccess_first_sensor() {
    unsigned long currentMillis = millis();
    // TODO Change to Hardware
    if (currentMillis - firstMagneticSensorTime >= debouncingInterval) {
        //Serial.println("Entrou 1");
        /* Count RPM */
        rpm = 60/((currentMillis - firstMagneticSensorTime)/1000);
       
        firstMagneticSensorTime = currentMillis;       
        
    }
}

void firstMagneticSensorActive() {
    firstMagneticSensorValue = digitalRead(firstMagneticSensor);
    if(firstMagneticSensorValue == 1) {
        proccess_first_sensor();
    }
}

The loop function prints everything ok until the first interrupt, then it freezes and don't print anymore.

What am I doing wrong?
Thanks,
Artur

Well, your first problem is that you never define the function named secondMagneticSensorActive. Next, it's unusual for the interrupt function you do have to call another function. Interrupt service routines (ISR's) should be as short as possible and execute as quickly as possible. Also, variables inside an ISR are usually defined with the volatile data specifier. Nick Gammon has a good discussion of interrupts here. I think your first order of business is to get your code to compile and use some Serial.print() calls to see what's happening. (Don't use those in your ISR, however.)

Thanks for your answer.
I do have secondMagneticSensorActive method and the code compiles. As I said I've only pasted the relevant code.

The second function code does nothing at this time:

void secondMagneticSensorActive() {
    secondMagneticSensorValue = digitalRead(secondMagneticSensor);
    if(secondMagneticSensorValue == 1) {
    unsigned long currentMillis = millis();
    if (currentMillis - secondMagneticSensorTime >= debouncingInterval) {
        secondMagneticSensorTime = currentMillis;
    }
    }
}

I'am using the prints in loop method, but as I said it crashes after the first interrupt.

If you are triggering the ISRs only on the RISING edge, why do feel it necessary to check that the sensor really is HIGH?

There is no reason to piss away resources using the String class to convert ints to strings.

itoa() or sprintf() can do that, using far less SRAM and flash memory.

     char buffer[8];
     to_send.toCharArray(buffer, 32);

Why are you telling the String class that it is OK to write 32 bytes into an array that can only hold 8?

If you actually write more than 8 characters (including the terminating NULL) into the array, you just f**ked up, and ANY expectations about how the Arduino will react after that are unfounded.

Thanks for your input PaulS :wink:
That should be the problem. I will make the changes you suggest and post the result.