attachInterrupt() cause to hard fault

Hi everyone,

I need to declare 3 interrupts in my firmware
1 - on pin P1S1
2 - on pin P1S2
3 - on pin TX (#64)

The first two interrupt are working well, but when I tried to add the 3rd interrupt, it caused to a hard fault.

I am pretty sure that it’s not related to syntax because I used interrupts before and it worked well.

maybe it’s related to this pice information in the particle reference:

1

Fan.h:

#include "Particle.h"

#define FAN_PIN        RX
#define FAN_TACHO      TX
#define INIT_FAN_VALUE 255  //off

class Fan
{
private:
    uint8_t fanSpeed;

    void ISRfunc();

public:
    Fan();
    void setup();
    void run();
    void run(uint8_t);

    void setFanSpeed(uint8_t);
};

Fan.cpp:

#include "Fan.h"

Fan::Fan()
{
    this->setFanSpeed(INIT_FAN_VALUE);
}
void Fan::setup()
{
    pinMode(FAN_PIN, OUTPUT);
    this->run();

    pinMode(FAN_TACHO, INPUT);
    attachInterrupt(FAN_TACHO, &Fan::ISRfunc, this, CHANGE);
}
void Fan::run()
{
    analogWrite(FAN_PIN, this->fanSpeed);
}
void Fan::run(uint8_t fanSpeed)
{
    if (fanSpeed > 255 || fanSpeed < 0)
    {
        return;
    }

    analogWrite(FAN_PIN, fanSpeed);
}

void Fan::setFanSpeed(uint8_t fanSpeed)
{
    if (fanSpeed <= 255 || fanSpeed >= 0)
    {
        this->fanSpeed = fanSpeed;
    }
}

void Fan::ISRfunc()
{
    Particle.publish("test", "*#*#*#*");
    delay(200);
}

I hope that someone can help me with this issue soon.
Thanks in advance,
Omri

@Omri, you shouldn’t be doing a Particle.publish() within an ISR. Nor should you have a delay() as millis() don’t run in an ISR and it could block other interrupts for the duration. Rember that ISR’s need to be lite and fast. For your ISR, you should set a flag or increment a counter that you test and handle in loop().

2 Likes

@peekay123, Thank you very much!!!

I did delete the Particle.publish and it’s work now.

1 Like