Function is not workging if no SerialMonitor is inside Function

Hello Ladies and Gents

I am new here and this is may first question. I got a strange problem during programming an alarm Clock
with my RTC DS3231. I created a simple Function with gives an alarm at a specific time with a specific durance.
The strange thing is that it is working only if I show a message"serial monitor" inside the function.
If I comment the message the Function is not working anymore.
Has anyone an Idea why this is happening ?

Hardware:

  • orig Arduino Uno
  • RTC 3231 shield
    Lib: RTCLib
    IDE version 1.8.13

Sketch:

#include “RTClib.h”
RTC_DS3231 rtc;

bool fullalarm_01 = false;
bool fullalarm_02 = false;

unsigned long sekundenAlarmON_01 = 0;
unsigned long sekundenAlarmOFF_01 = 0;
unsigned long sekundenNow_01 = 0;

void setup () {

pinMode(13, OUTPUT);
Serial.begin(9600);

#ifndef ESP8266
while (!Serial); // wait for serial port to connect. Needed for native USB
#endif

if (! rtc.begin()) {
Serial.println(“Couldn’t find RTC”);
Serial.flush();
abort();
}

rtc.adjust(DateTime(2021, 3, 14, 14, 29, 58));
}

void loop ()
{
DateTime now = rtc.now();
Serial.print(now.hour(), DEC);
Serial.print(’:’);
Serial.print(now.minute(), DEC);
Serial.print(’:’);
Serial.println(now.second(), DEC);

if(FullAlarmFunktion_01(14, 30, 0, 10) == 1)
{
digitalWrite(13, HIGH);
Serial.print(" ALARM ON in loop");
Serial.println("");
}
else
{
digitalWrite(13, LOW);
}

delay(1000);
}

// function begin

bool FullAlarmFunktion_01(byte a_01, byte b_01, byte c_01, byte durance_01)
{
DateTime now = rtc.now();

sekundenNow_01 = ((now.hour()3600)+(now.minute()60)+now.second());
sekundenAlarmON_01 = ((a_01
3600)+(b_01
60)+c_01);
sekundenAlarmOFF_01 = sekundenAlarmON_01 + durance_01;

if(sekundenNow_01 >= sekundenAlarmON_01 && sekundenNow_01 < sekundenAlarmOFF_01)
{
//Serial.println("Alarm ON in Function "); THIS FUNKTION IS ONLY WORKINH WHEN THIS LINE IS UNCOMMENT !!
fullalarm_01 = true;
}
if(sekundenNow_01 >= sekundenAlarmOFF_01)
{
Serial.println(“Alarm OFF 1”);
fullalarm_01 = false;
}
return fullalarm_01;
}

thank you for your help
Sebastian

Just some low hanging fruit... you should use predefined values 'true' and 'false' with bool types, not integer 0 and 1.

What happens if you change delay(1000) to delay(500)?

Also, please describe, "not working" in more detail. What happens or does not happen that is different?

I use bool type in my sketch, where do you see int ??

It is really strange: All inside the if statements is only working when The Line with the serial Monitor is inside the if.

  • if(sekundenNow_01 >= sekundenAlarmON_01 && sekundenNow_01 < sekundenAlarmOFF_01)*

  • { *

  • Serial.println("Alarm ON in Function "); With this line all is working fine (i.e. LED 13 HIGH)*

  • fullalarm_01 = true;*

  • digitalWrite(13, HIGH); *

  • }*

* if(sekundenNow_01 >= sekundenAlarmON_01 && sekundenNow_01 < sekundenAlarmOFF_01)
_
{ _
_
// Serial.println("Alarm ON in Function "); Now nothing is working, also No LED HIGH*_

  • fullalarm_01 = true;*

  • digitalWrite(13, HIGH); *

  • }*
    This is really cannot understand !

Welcome to the forum

Please follow the advice on posting code given in Read this before posting a programming question in order to make your sketch easy to follow, download and test

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

If the code exceeds the 9000 character inline limit then attach it to a post

As to the boolean, which is almost certainly not causing the problem, the FullAlarmFunktion_01() returns a boolean

bool fullalarm_01 = false;
  return fullalarm_01;

but is compared to an integer

  if (FullAlarmFunktion_01(14, 30, 0, 10) == 1)

I think this is causing an integer overflow on the right side of the assignment:

  if (FullAlarmFunktion_01(14, 30, 0, 10))

bool FullAlarmFunktion_01(byte a_01, byte b_01, byte c_01, byte durance_01) {
sekundenAlarmON_01 = ((a_01 * 3600) + (b_01 * 60) + c_01);

a_01 is an ‘unsigned char’ which has a lower rank than ‘int’ so it gets promoted to ‘int’. The calculation “14 * 3600” results in 50400 which won’t fit in an ‘int’. This causes an overflow to a negative value. Eventually the negative ‘int’ gets sign-extended to fit in the ‘unsigned long’ and thus you have a value close to 4 billion seconds. I don’t know why adding a Serial.print() causes it to work.

One fix would be to change “3600” to “3600ul”. That will promote ‘14’ to ‘14ul’ and the result will not overflow.

Thank you for your answer. I am sorry, obviously I cannot explain my Problem in a clear way.
UKHeliBob: Youre right I was using integer in this case but this is not the Problem. Also witg false/true it is not working. Also with delay(500); same.

To say clear. All the Scatch is working as I want if there is this line: Serial.println(“Alarm ON” ); inside the IF statement in the function.
so the programm is going into the IF and the Pin13 will be HIGH in this example.
Buit if there is this line // Serial.println(“Alarm ON” ); not inside the programm will not enter the IF statement.
I really cannot understand why just a “Serial.print” leads to such a bug ?

Example A → working, LED 13 stay dark

if(sekundenNow >= sekundenAlarmON && sekundenNow < sekundenAlarmOFF)
     {     
                      fullalarm = true;
                      digitalWrite(13, HIGH);
     }

Example B → Workin working LED IS HIGH

     if(sekundenNow >= sekundenAlarmON && sekundenNow < sekundenAlarmOFF)
     {     
                      Serial.println("Alarm ON");  
                      fullalarm = true;
                      digitalWrite(13, HIGH);
     }

alarm.ino (1.86 KB)

Thank You Johnwasser

I tried now also with “unsigned Long”
Same problem, my function only activates my PIN13 only when there is also this stupid Seral.print

Eigene_Wecker_Funktionen_03.ino (1.93 KB)

Please don’t attach such a small amount of code - post it, in code tags.

thank you all :slight_smile:
It was really the UL behind the 3600, with this it is also working without the Serial.Print
But why it is working when Serial.Print is a miracle

Thank you All
Sebastian

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