Interrupt stop working

I’m working on an ac dimmer circuit using Arduino and Hc - 05 bt module but the bulb stops glowing for a brief sec I’m assuming the interrupt stop working here for a brief sec rest is working fine I saw that when I disconnect the bt interrupt stop working more frequently. I have also seen that it happens when the Arduino RX led glows maybe it try to receive something while stopping the interrupt. I’m using the hardware serial.

volatile int detectado = 0;
volatile int valor=0;
volatile int last_CH1_state = 0;
int pinValue;
    
#include <BlynkSimpleSerialBLE.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "i8GfxSLhfmCF6QKjdqWtr5JvGh7ro0AU";


// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin V1
BLYNK_WRITE(V1)
{
   pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
valor = map(pinValue,90,255,7000,10);
  // process received value

}

void setup()
{
  
  
  PCICR |= (1 << PCIE0);    //enable PCMSK0 scan                                                 
  PCMSK0 |= (1 << PCINT0);  //Set pin D8 trigger an interrupt on state change. Input from optocoupler
  pinMode(3,OUTPUT);        //Define D3 as output for the DIAC pulse
  
 Serial.begin(38400);
Blynk.begin(auth, Serial);

  
}

void loop()
{
  Blynk.run();
if (detectado)
    {
      delayMicroseconds(valor); //This delay controls the power
      digitalWrite(3,HIGH);
      delayMicroseconds(50);
      digitalWrite(3,LOW);
      detectado=0;
    } 
 
}
//This is the interruption routine
//----------------------------------------------
ISR(PCINT0_vect){
  /////////////////////////////////////               //Input from optocoupler
  if(PINB & B00000001){                               //We make an AND with the pin state register, We verify if pin 8 is HIGH???
    if(last_CH1_state == 0){                          //If the last state was 0, then we have a state change...
      detectado=1;                                    //We haev detected a state change!
    }
  }
  else if(last_CH1_state == 1){                       //If pin 8 is LOW and the last state was HIGH then we have a state change      
    detectado=1;                                      //We haev detected a state change!
    last_CH1_state = 0;                               //Store the current state into the last state for the next loop
    }
}

@Sk_Sahabaj please edit your post using the pencil icon at the bottom and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

sorry for that Now I have corrected it.

You need to clean-up your void loop…

Pete.

I have clean the loop using timmer but still got the same issue.

And what does your new code look like?

Pete.


volatile int detectado = 0;
volatile int valor=0;
volatile int last_CH1_state = 0;
int pinValue;
   
   
#include <BlynkSimpleSerialBLE.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "i8GfxSLhfmCF6QKjdqWtr5JvGh7ro0AU";

BlynkTimer timer; // Announcing the timer

// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin V1
BLYNK_WRITE(V1)
{
pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
valor = map(pinValue,90,255,7000,10);
  // process received value

}

void setup()
{
  
  
  PCICR |= (1 << PCIE0);    //enable PCMSK0 scan                                                 
  PCMSK0 |= (1 << PCINT0);  //Set pin D8 trigger an interrupt on state change. Input from optocoupler
  pinMode(3,OUTPUT);        //Define D3 as output for the DIAC pulse
  
 Serial.begin(38400);
Blynk.begin(auth, Serial);

  timer.setInterval(50L, sensorDataSend); //timer will run every sec 

  
}

void loop()
{
  Blynk.run();
   timer.run();        // run timer every second

 
 
}
void sensorDataSend()
{
 if (detectado)
    {
      delayMicroseconds(valor); //This delay controls the power
      digitalWrite(3,HIGH);
     delayMicroseconds(100); //This delay controls the power
      digitalWrite(3,LOW);
      
      detectado=0;
    } 
 
 }
 
//This is the interruption routine
//----------------------------------------------
ISR(PCINT0_vect){
  /////////////////////////////////////               //Input from optocoupler
  if(PINB & B00000001){                               //We make an AND with the pin state register, We verify if pin 8 is HIGH???
    if(last_CH1_state == 0){                          //If the last state was 0, then we have a state change...
      detectado=1;                                    //We haev detected a state change!
    }
  }
  else if(last_CH1_state == 1){                       //If pin 8 is LOW and the last state was HIGH then we have a state change      
    detectado=1;                                      //We haev detected a state change!
    last_CH1_state = 0;                               //Store the current state into the last state for the next loop
    }
}

Did you read the bit in the “keep your void loop clean” documentabout delays?

You currently have a function that is being called every 50ms that contains a delay of between 10m and 7000ms.
How do you envisage that this will work?

Pete.

1 Like