For a clock program (display and hammer strike) I use a main loop (with also other stuff) with under mentioned relevant routines.
The bell strike routine consist of two nested parts: the number of pings will be related to the hour/halfhour; the inner loop will give a pulse of 0,5 sec, and a wait period of 1,5 sec.
//Update display every second only row2 with date and time
if (blnShowDateTime){
Get_RTCtime();
strLine2 = strDate + " " + strTime;
lcd.setCursor(0,1); lcd.print(strLine2);
blnShowDateTime = false;
}
//=========================================================================================
//Activates the strike hammer every 30 minutes
//if (blnSoundflag and (minute==0 or minute==30) and second==0){ //condition to strike the hammer
blnBellCycle = true; //Force a start of the bell
//condition to strike the hammer
if (blnBellCycle = true){
//Calculate the number of Pings
if (hour==0){hour=24;} //Midnight is presented as 0:00, but the bell has to ring 12 times
if (hour >=1 and hour<=12) {numberPings = hour ;} else {numberPings = hour -12;} //Fix number of beats for hours
if (minute ==30){numberPings=1;} //One beat for the half hours
numberPings =12; //Temporaly
//Serial.println (numberPings);
//Striking the hammer a number of pings
int i=0;
// while(i <= numberPings){
// blnBellStrike = true; //Enables counting cnt1 from Timer 1 (20 Hz)
// //Serial.println (blnBellStrike);
// switch (cnt1){
// case 1:
// Serial.println (String("Slag ") + i);
// digitalWrite(Bellpin, LOW); //Signal to hammer, takes 500 ms
// break;
// case 10:
// digitalWrite(Bellpin, HIGH); //Wait 1500 ms for the next strike
// break;
// case 40:
// blnBellStrike = false; //disable counting cnt1 from Timer 1 (20 Hz)
// cnt1 =0; //reset counter
// i++;
// } //End switch
//
// } //End while
blnBellCycle = false;
} //End if blnBellCycle
They make use of Timer0 (20 Hz) for the hammer strike and Timer1 (1 Hz) for updating display.
//===============================================================================================================
//Interrupt every 50 ms (20 Hz) with Timer0
ISR(TIMER0_COMPA_vect){
if (blnBellStrike){cnt1++;} //Increment 50 ms counter
} //End interrupt service request
//===============================================================================================================
//Interrupt every 1 s (1 Hz) with Timer1
ISR(TIMER1_COMPA_vect){
blnShowDateTime= true;
Serial.println ("Secondepuls komt binnen");
} //End interrupt service request
PROBLEM:
With the commented lines the time display is working fine.
If i use my whole program, ISR(TIMER1_COMPA_vect) works (the second pulse comes through), but the LCD is not updated (it stocks).
The question is WHY?
I did experiences with delays en other loop-functions but they keep the micro TOO busy, the program crashes.