-1

I'm trying to get warning with buzzer while I get same values on 3 seconds. But first warning doesn't wait 3 seconds. Second warning is correct. Code is attached. BlinkLED1() method is same BlinkLED2. Probably, I'm missing crowning touch. Can you help me? Thanks in advance.

void initialsettings(double roll,double pitch){
  if(level==0){
    if(roll>=34 && timerstatus==false){

        Timer1.initialize(3000000);
        timerstatus=true;
        Timer1.attachInterrupt(blinkLED);
    } else{
         Timer1.stop();
         timerstatus=false;
         level=0;
    }

   }else if(level==1){
      if(roll<=-34 && timerstatus==false){

        Timer1.initialize(3000000);
        timerstatus=true;
         Timer1.attachInterrupt(blinkLED2);
      } else{
           Timer1.stop();
         timerstatus=false;

      }

  }else if(level==2){


  }
}

void blinkLED(void)
{
    noInterrupts();
    Timer1.stop();

    //Timer1.restart();
    digitalWrite(buzzerpin,HIGH);
    delay(7000);
    digitalWrite(buzzerpin,LOW);
    timerstatus=false;
    interrupts();
    if(level==0){
      level=1;
      Serial.println("level 1");
    }
  Serial.println("Sag ");
    //Serial.print("\n");

}

1 Answer 1

0
    Timer1.attachInterrupt(blinkLED);

...

void blinkLED(void)
{
    noInterrupts();
    Timer1.stop();

    //Timer1.restart();
    digitalWrite(buzzerpin,HIGH);
    delay(7000);
    digitalWrite(buzzerpin,LOW);
    timerstatus=false;
    interrupts();
    if(level==0){
      level=1;
      Serial.println("level 1");
    }
  Serial.println("Sag ");

  • Don't do serial prints inside an interrupt service routine (ISR).
  • Don't use delay() inside an ISR.
  • Don't turn interrupts on or off inside an ISR.
  • Variables shared between an ISR and the main code should be volatile (I can't see from your snippet whether they are or not).

Reference: Interrupts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.