Sign up ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

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");

}
share|improve this question

1 Answer 1

    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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.