!!----Simple interrupt Please Help----!!

Hello everybody,

I've got a simple question:

I've attached a beeper on Pin 3 and a Switch (internal Pullup activated on Arduino -> Switch pushed -> GND) on Pin 8, when the Button is pushed the Beeper should be Muted for 30 seconds and the rest of my project should continue working. So a delay is not possible but an interrupt, but how can i make the interrupt work?

I am sorry, if my question is written in really bad english.

Can someone help me please?

Greets

:slight_smile:

#include "TimerOne.h"


const int ADC_Xin = A0; //Vertikal
int ADCValue_Xin = 0;   

const int ADC_Yin = A1; //Horizontal
int ADCValue_Yin = 0;  

const int Horizontal_min = 400;
const int Horizontal_max = 600;

const int Vertikal_min = 400;
const int Vertikal_max = 600;

const int OUT_LEDHorizontal = 1; //Horizontal LED
const int OUT_LEDVertikal = 2; //Vertikal LED
const int OUT_Buzzer = 3; //Buzzer
//const int OUT_LEDStatus = 13; //Buzzer

const int IN_LEDrot = 9; //Buzzer
const int IN_LEDgelb = 10; //Buzzer

const int IN_Keyswitch = 8; //Keyswitch

unsigned long timer;

boolean Muting = false;

int j=0;


//void callback()
//{
//     digitalWrite(OUT_Buzzer, LOW );
//}


// the setup function runs once when you press reset or power the board
void setup() {

   pinMode(OUT_LEDVertikal, OUTPUT); // Vertical OUT LED Grün
   pinMode(OUT_LEDHorizontal, OUTPUT); // Horizontal OUT LED rot
   pinMode(OUT_Buzzer, OUTPUT); // Buzzer

   pinMode(IN_LEDrot, INPUT); // rote Warn LED
   pinMode(IN_LEDgelb, INPUT); // gelbe Warn LED
   pinMode(IN_Keyswitch, INPUT_PULLUP);//Keyswitch

//   Timer1.initialize(500000);
//   Timer1.setPeriod(500000); 
//   Timer1.attachInterrupt(callback);
//   Timer1.disablePwm(OUT_Buzzer); 
   


   
//////////////////////////// Initialisiere Ein - und Ausgänge:

////INPUTS:

ADCValue_Xin = analogRead(ADC_Xin);


ADCValue_Yin = analogRead(ADC_Yin);
}

// the loop function runs over and over again forever
void loop() 
{
      
if (digitalRead(IN_Keyswitch) == LOW) 
 {
   for(j=1, 
   digitalWrite(OUT_Buzzer, LOW)
 }
 



/////////////////////////////////////////////////////Akustische Ausgabe LED ROT UND GELB

 if (digitalRead(IN_LEDrot) == HIGH) {
    digitalWrite(OUT_Buzzer, HIGH);
      delay(5);   
      digitalWrite(OUT_Buzzer, LOW);
      delay(5);   
  }

  if (digitalRead(IN_LEDrot) == LOW)
  {
    digitalWrite(OUT_Buzzer, LOW);
  }


   if (digitalRead(IN_LEDgelb) == HIGH) 
   {
 //Serial.println(">>> ALARM gelbe LED ");
    digitalWrite(OUT_Buzzer, HIGH);
      delay(10);   
      digitalWrite(OUT_Buzzer, LOW);
      delay(10);   
  }

  if (digitalRead(IN_LEDgelb) == LOW)
  {
    digitalWrite(OUT_Buzzer, LOW);
  }


  //////////////////////////////////////////////////////////////////////////////////////

ADCValue_Xin = analogRead(ADC_Xin);

///////////////////////////////////////Kippverhalten

  if (analogRead(ADC_Xin)> Vertikal_max)
  {
      digitalWrite(OUT_LEDVertikal, HIGH);
      digitalWrite(OUT_Buzzer, HIGH);
    }

     if (analogRead(ADC_Xin)< Vertikal_min)
  {
      digitalWrite(OUT_LEDVertikal, HIGH);
      digitalWrite(OUT_Buzzer, HIGH);
    }

     if (analogRead(ADC_Xin)> Vertikal_min &  analogRead(ADC_Xin) < Vertikal_max)
  {
      digitalWrite(OUT_LEDVertikal, LOW);
      digitalWrite(OUT_Buzzer, LOW);
    }

  //////////////////////////////////////////////////////////////////////////////////////
 


///////////////////////////////////////Kippverhalten

  if (analogRead(ADC_Yin)> Horizontal_max)
  {
      digitalWrite(OUT_LEDHorizontal , HIGH);
      digitalWrite(OUT_Buzzer, HIGH);
    }

     if (analogRead(ADC_Yin)< Horizontal_min)
  {
      digitalWrite(OUT_LEDHorizontal, HIGH);
      digitalWrite(OUT_Buzzer, HIGH);
    }

    if(analogRead(ADC_Yin)> Horizontal_min &  analogRead(ADC_Yin) < Horizontal_max)
  {
      digitalWrite(OUT_LEDHorizontal, LOW);
      digitalWrite(OUT_Buzzer, LOW);
    }

  //////////////////////////////////////////////////////////////////////////////////////

}

You have posted code without using code tags. This creates certain problems and obstacles for other forum members. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the [code] and [/code] metatags.

When you are finished that, please read these two posts:

How to use this forum - please read.
and
Read this before posting a programming question ...

You should probably embrace the thinking shown here in BlinkWithOutDelay.

Thanks to the two of you,

I will look up BlinkWithoutdelay again.

Sorry for the troubles with my code.

Greets

It looks to me as though you have completely misunderstood the use of interrupts.

Surely what you need to do is to save the start time from millis() when the button is pressed, put the buzzer in the required state then, each time through loop() check whether 30 seconds has elapsed since the start time by subtracting it from the current value of millis(). If not then go round loop() again else put the buzzer in the required state.

I note that despite not wanting to use delay() there are several instances of it in the program.

Can you please give me an example? :o

Log_IT:
Can you please give me an example? :o

Best example is blinkwithoutdelay. It keeps looking to see if the time's up... if not, do nothing, if yes, do something.

This video explains it nicely, I think.