Minutes_alarm does not increment (SOLVED)

I need to have three alarms one minute apart.
The sketch is as follows:

#include "arduino_secrets.h"
/*********************  non funziona
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/b8b6492c-e136-4705-96e2-a7402e3e023b 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  bool alarm_state;
  bool led;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"
#include "RTCZero.h"

int hours_alarm = 15;
int minutes_alarm = 40;
int seconds_alarm = 0;
int contatore;
int LED = 2;

RTCZero alarm;

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);

  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();

  alarm.begin();
  led = false;
  contatore = 0;
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);
}

void loop() {
  ArduinoCloud.update();
  Serial.print(alarm.getHours());
  Serial.print(":");
  Serial.print(alarm.getMinutes());
  Serial.print(":");
  Serial.println(alarm.getSeconds());
}

void onHoursAlarmChange() {
  alarm.setAlarmTime(hours_alarm, minutes_alarm, seconds_alarm);
}

/*
void onMinutesAlarmChange() {
  alarm.setAlarmTime(hours_alarm, minutes_alarm, seconds_alarm);
}

void onSecondsAlarmChange() {
  alarm.setAlarmTime(hours_alarm, minutes_alarm, seconds_alarm);
}
*/

void onAlarmStateChange() {
  if (alarm_state == true) {
    alarm.setAlarmTime(hours_alarm, minutes_alarm, seconds_alarm);
    alarm.enableAlarm(alarm.MATCH_HHMMSS);
    alarm.attachInterrupt(alarmMatch);
  }
  else {
    //alarm.disableAlarm();
    led = false;
    digitalWrite(LED_BUILTIN, LOW);
  }
}

void alarmMatch(){
  led = true; /7  accende il led
  digitalWrite(LED_BUILTIN, HIGH);
  Serial.print(contatore);
  Serial.println(" : ");
  contatore = ++contatore;
  Serial.println(contatore);
  
  if(contatore == 1){
    Serial.println(contatore);
    //Serial.print(alarm.getHours());
    digitalWrite(LED, HIGH);
    hours_alarm = 15;
    minutes_alarm = ++minutes_alarm;
    seconds_alarm = 0;
  }
  else if(contatore == 2){
   Serial.println(contatore);
    digitalWrite(LED, LOW);
    hours_alarm = 15;
    minutes_alarm = ++minutes_alarm;
    seconds_alarm = 0;
  }
  else if(contatore == 3){
    Serial.println(contatore);
    digitalWrite(LED, HIGH);
  }
}

In the monitor, I do not see the counter incrementing.
It also executes the first of the ifs then by not incrementing the counter it does not execute the subsequent ifs.
Should I enter the new values in the function 'onMinutesAlarmChange()' ?
Where am I going wrong?

EzioGi

        minutes_alarm = ++minutes_alarm;

This line of code will result in an unspecified outcome

Try

        minutes_alarm++;

instead

I made the change you recommended but it doesn't work.
The variable 'counter' does not increment, which means that the 'alarmMatch()' function is not executed.

EzioGi

I inserted the function at the end of the 'if' and now it works.
After I had changed the alarm minutes I also had to call the function " alarm.setAlarmTime(hours_alarm, minutes_alarm, seconds_alarm);".

EzioGi

1 Like

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