Arduino UNO doesn´t accept second interruption

Hey!

Currently we´re programming an arduino in school, our project is a simple alarm clock.

The Probleim is, the Arduino does not accept the second interrupt (funktion name ,,clock1") and I do not really know why.

There is no error message, it just happens nothing if I press the Key on Ping 3.

Can someone help me?

(CODE)

#include <LiquidCrystal.h>

const int rs = 11, en = 12, d4 = 8, d5 = 9, d6 = 4, d7 = 5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int TasterLinks = 6;
int TasterMitte = 7;
int TasterMitteRechts = 2;
int TasterRechts = 3;

long StundeZeit = 0;
long MinuteZeit = 0;
long StundeAlarm = 0;
long MinuteAlarm = 0;

bool alarmSet = false;
bool uhrSet = false;

void setup()
{
 pinMode(9, OUTPUT);
 pinMode(TasterLinks, INPUT_PULLUP);
 pinMode(TasterMitte, INPUT_PULLUP);
 pinMode(TasterMitteRechts, INPUT_PULLUP);
 pinMode(TasterRechts, INPUT_PULLUP);
 pinMode(13, OUTPUT); 
 
 lcd.begin(8,2);
 Serial.begin(9600);

 attachInterrupt(digitalPinToInterrupt(TasterMitteRechts),alarm ,CHANGE);
  //Beim wechseln des Signals wird void alarm() ausgeführt
  
 attachInterrupt(digitalPinToInterrupt(TasterRechts),clock1 ,RISING);
  //Beim wechseln des Signals wird void clock1() ausgeführt
}

void loop()
{
  
  		  lcd.setCursor(0,0);
  		  lcd.print("Weckzeit?");
  
       	  lcd.setCursor(0,1);
          lcd.print("0");
          lcd.setCursor(1,1);
          lcd.print("0");
          lcd.setCursor(2,1);
          lcd.print(":");    
          lcd.setCursor(3,1);
          lcd.print("0");
          lcd.setCursor(4,1);
          lcd.print("0");
  			//Untere Uhr (Weckzeit) wird auf 00:00 gestellt

          int i = 2;
          while (i > 1) {

            if (alarmSet == false) {
              
             //Setzt StundeAlarm 
       int TasterStatusLinks = digitalRead(TasterLinks);

       if (TasterStatusLinks == 0) {

         delay(200);
         if (StundeAlarm == 23) {
           StundeAlarm = 00;
         } 
         else StundeAlarm ++;

         if (StundeZeit < 10) {
           lcd.setCursor(1,1);
           lcd.print(StundeAlarm);
           lcd.setCursor(0,1);
           lcd.print("0");
         } else{

           lcd.setCursor(0,1);
           lcd.print(StundeAlarm);
         }
       }

       //Setzt MinuteAlarm 
       int TasterStatusMitte = digitalRead(TasterMitte);  
       if (TasterStatusMitte == 0) {

         delay(200);
         if (MinuteAlarm >= 59) {
           MinuteAlarm = 00;
         } 
         else MinuteAlarm ++;

         if (MinuteAlarm < 10) {
           lcd.setCursor(4,1);
           lcd.print(MinuteAlarm);
           lcd.setCursor(3,1);
           lcd.print("0");
         } else{

           lcd.setCursor(3,1);
           lcd.print(MinuteAlarm);
         }

        }
            
          long AlarmZeit = StundeAlarm + MinuteAlarm;
       }   
  
  		/*Setzt die Weckzeit
      		1. Taster von links: Stunde
            2. Taster von links: Minute
            3. Taster von links: Bestätigen & wechseln zum Urzeit
            einstellen.
      	*/ 
            
         }
}  
  

void alarm() { //Uhrfunktion
  
  	alarmSet = true;
  
	int TasterStatusRechts = digitalRead(TasterRechts);
  	if (uhrSet == false) {
  	
      lcd.clear();
      uhrSet = true;

      lcd.setCursor(0,0);
      lcd.print("Uhrzeit?       ");

      lcd.setCursor(0,1);
      lcd.print("0");
      lcd.setCursor(1,1);
      lcd.print("0");
      lcd.setCursor(2,1);
      lcd.print(":");    
      lcd.setCursor(3,1);
      lcd.print("0");
      lcd.setCursor(4,1);
      lcd.print("0");

        int i = 2;
        while (i > 1) {

        int TasterStatusLinks = digitalRead(TasterLinks);

         if (TasterStatusLinks == 0) {

           delay(2000);
           if (StundeZeit == 23) {
             StundeZeit = 00;
           } 
           else StundeZeit ++;

           if (StundeZeit < 10) {
             lcd.setCursor(1,1);
             lcd.print(StundeZeit);
             lcd.setCursor(0,1);
             lcd.print("0");
           } else{

             lcd.setCursor(0,1);
             lcd.print(StundeZeit);
           }
         }

         //Setzt MinuteAlarm 
         int TasterStatusMitte = digitalRead(TasterMitte);  
         if (TasterStatusMitte == 0) {

           delay(2000);
           if (MinuteZeit >= 59) {
             MinuteZeit = 00;
           } 
           else MinuteZeit ++;

           if (MinuteZeit < 10) {
             lcd.setCursor(4,1);
             lcd.print(MinuteZeit);
             lcd.setCursor(3,1);
             lcd.print("0");
           } else{

             lcd.setCursor(3,1);
             lcd.print(MinuteZeit);
           }

          }

            long UhrZeit = StundeZeit + MinuteZeit;
           
          /*Setzt die Weckzeit
              1. Taster von links: Stunde
              2. Taster von links: Minute
              3. Taster von links: Bestätigen & wechseln zum Urzeit
              einstellen.
          */
          
        }
    
  
    } 
      
}

void clock1() {
 
  Serial.print("test");
  
}

The Code is not perfect, I´m still beginner :confused:

Best regards
Nico

All of the code in your interrupt handlers is going to cause serious problems. All of it.

You will be much better served avoiding interrupts.

At a quick glance, you should not use print statements in alarm and clock1. And for buttons, there is no need to use interrupts; it's not that the world will come to an end if your code reacts a few milliseconds late :wink:

What should I use instead?

Just write all your code in loop(). Make use of functions that you call from loop().

Finite state machine.

I suspect you'll find some examples here.

void loop()
{
  readButtons();
  updateLCD();
  doAction();
}

void readButtons()
{
  ...
  ...
}

void updateLCD()
{
  ...
  ...
}

void doAction()
{
}

Note: updating LCD might be slow; only update if date to be displayed changes.

Hi Nico - if you could add comments in english it would help me , my german is non-existent.

Interrupt routines MUST execute in a very short time. both of yours have "blocking code" eg

    if (TasterStatusLinks == 0) {

           delay(2000);

That will prevent proper operation.

A better solution is to use the interrupt routine just to set a flag eg bool minuteAlarm
then test for that in your loop.

And use the millis() to see if the required time has elapsed. See the "blink without delay" example.

Worked! Thanks for you help :pray:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.