2 switches 3 cases alarm functions

Hi!

I have 2 buttons and 4 leds. When I press button A it changes in sequence 3 cases (turns on and off each LED for each case respectively). Each case does a function. The first one does AlarmAction with alarm function and it is working. The second function (case) does a calculation what day (even-odd) we have and if it is odd will do the AlarmAction but it is not working. The third function does nothing. The second button whenever it is pressed (second led)does the AlarmAction (second led) and it is working.

#include <math.h>
#include <Time.h>  
#include <TimeAlarms.h>
#include <TimeLib.h>
#include <DS1302RTC.h>

#define A 33//orange
#define B 35//blue
#define C 37//grey
#define D 39//green
#define E 41//yellow
#define F 43//red
#define G 45//brown

#define ON LOW
#define OFF HIGH

const int inbutton1 = 2;
const int inbutton2 = 3;
int button2state;
int lastbutton1state = HIGH;
int lastbutton2state = HIGH;
int actual;
 int val;
  int val2;// variable for reading the pin status
                      // variable for reading the delayed status
    int button1state;                // variable to hold the button state
    int Mode = 0;
 
unsigned long startTime;
unsigned long elapsedTime;

const int ledPin1 = 7;
const int ledPin2 = 8;
const int ledPin3 = 9;
const int ledPin4 = 10; 
 
 // variables will change:  
  // variable for reading the pushbutton status  

   
unsigned long previousMillis = 0;
const long interval = 1000;  
unsigned long noOfSeconds;
int noOfWholeDays;
int todayIsDay;
bool todayIsOdd;

 DS1302RTC RTC(27, 29, 31);//WHITE - BROWN - ORANGE - BLACK GND - RED POSITIVE

//boolean debug = 1; // 1 = Print Serial Enabled / 0 = disabled
 
  
//===============================================================
// SETUP
//===============================================================
void setup () {
 digitalWrite(A, HIGH);
 digitalWrite(B, HIGH);
 digitalWrite(C, HIGH);
 digitalWrite(D, HIGH);
 digitalWrite(E, HIGH);
 digitalWrite(F, HIGH);
 digitalWrite(G, HIGH);
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);
   button1state = digitalRead(inbutton1);
 button2state = digitalRead(inbutton2);
    
   pinMode(ledPin1, OUTPUT); 
    pinMode(ledPin2, OUTPUT); 
     pinMode(ledPin3, OUTPUT); 
      pinMode(ledPin4, OUTPUT);   
        
  pinMode(inbutton1, INPUT);
  pinMode(inbutton2, INPUT);
  
 //if (debug)
 {
 Serial.begin(9600);
 Serial.print("Initial Mode: ");
 Serial.print("Setup Complete\n");
 Serial.print("RTC activated");
 if (RTC.haltRTC())
    Serial.print("Clock stopped!");
  else
    Serial.print("Clock working");
     Serial.print("RTC Sync");
  setSyncProvider(RTC.get);          // the function to get the time from the RTC
  if(timeStatus() == timeSet)
    Serial.print(" Ok!");
  else
    Serial.print(" FAIL!");
     //setTime(21,37,0,9,20,18); // set time to Saturday 8:29:00am Jan 1 2011
 }
}
 
//===============================================================
// Main Loop
//===============================================================
void loop() {
  button2state = digitalRead(inbutton2);
   if (button2state == HIGH) {  
      AlarmAction ();  
    } else {  
     // turn LED off:  
     digitalWrite(ledPin1, LOW); 
  } 
 
   val = digitalRead(inbutton1);      // read input value and store it in val
      delay(10);                         // 10 milliseconds is a good amount of time
      val2 = digitalRead(inbutton1);     // read the input again to check for bounces
      if (val == val2) {                 // make sure we got 2 consistant readings!
        if (val != button1state) {          // the button state has changed!
          if (val == LOW) {                // check if the button is pressed
            if (Mode == 0) {          
              Mode = 1;               
            } else {
                if (Mode == 1) {        
                Mode = 2;           
            } else {
                if (Mode == 2) {      
                Mode = 3;           
            } else {
                if (Mode == 3) { 
                Mode = 0;          
                  } 
                  }
                  }
                  }
                  }
                  }
                  }
unsigned long currentMillis = millis();
 { if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    digitalClockDisplay();
    // if the LED is off turn it on and vice-versa:
     
 }
 }
 button1state = digitalRead(inbutton1);
if (button1state == HIGH && button1state != lastbutton1state)
   {
     if(actual == 3) actual = 0;
     else actual++;
   }
 lastbutton1state = button1state;

 switch(actual)
 {
   case 0:
    
 showState();
   break;

   case 1:
   
 WhatDay ();
   break;

   case 2:
   break;
   }
    button1state = val;                 // save the new state in our variable
     

      // Now do whatever the lightMode indicates
      if (Mode == 0) { // all-off
        digitalWrite(ledPin2, HIGH);
        digitalWrite(ledPin3, LOW);
        digitalWrite(ledPin4, LOW);
      }

      if (Mode == 1) { 
        digitalWrite(ledPin2, LOW);
        digitalWrite(ledPin3, HIGH);
        digitalWrite(ledPin4, LOW);
      }

      if (Mode == 2) { 
        digitalWrite(ledPin2, LOW);
        digitalWrite(ledPin3, LOW);
        digitalWrite(ledPin4, HIGH);
      }
      }
//===============================================================
// Subroutine
//===============================================================
void showState() {
 //if (debug)
 {
 Serial.println("Mode:EVERYDAY");
  Alarm.delay(50); // wait one second between clock display
 Alarm.alarmOnce(21,40,0, AlarmAction);  //  every day 
 }
}
void WhatDay () {
  Serial.println("Mode:ODD-EVEN");
  noOfSeconds = RTC.get();
  noOfWholeDays = noOfSeconds / 86400;
  todayIsDay = noOfWholeDays + 1;
  if (todayIsDay % 2 == 0)
  {
    todayIsOdd = false;
  }
  else
  {
    todayIsOdd = true;
  }
 if (todayIsOdd)
  {
    Serial.print("Today ODD day");
    Alarm.alarmOnce(21,50,0,AlarmAction);
  }
  else
  {
    Serial.print("Today EVEN day");
  }
  delay (25);
 }
 void AlarmAction () {
  // functions to be called when an alarm triggers:
  Serial.print("POTISMA");
  digitalWrite(ledPin1, HIGH); 
  /*one(); 
  digitalWrite(52, LOW); //RELAY 1 ON
  delay(1000L*60L*1L);               //wait 4 min 
  two();
  digitalWrite(52, HIGH);  //RELAY 1 OFF
  digitalWrite(50, LOW); //RELAY 2 ON
  delay(1000L*60L*1L);               //wait  4 min
  three();
  digitalWrite(50, HIGH);  //RELAY 2 OFF
  digitalWrite(48, LOW); //RELAY 3 ON
  delay(1000L*60L*1L);               //wait 5 min
  four();
  digitalWrite(48, HIGH);  //RELAY 3 OFF
  digitalWrite(46, LOW); //RELAY 4 ON
  delay(1000L*60L*1L);               //wait 5 min
  five();
  digitalWrite(46, HIGH);  //RELAY 4 OFF
  digitalWrite(44, LOW); //RELAY 5 ON
  delay(1000L*60L*1L);               //wait 5 min
  six();
  digitalWrite(44, HIGH);  //RELAY 5 OFF
  digitalWrite(42, LOW); //RELAY 6 ON
  delay(1000L*60L*1L);               //wait 5 min*/
  
  delay(5000);               //wait 5 min
  digitalWrite(ledPin1, LOW);
}
void digitalClockDisplay()
{
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print("-");
  Serial.print(day());
  Serial.print("/");
  Serial.print(month());
  Serial.print("/");
  Serial.print(year()); 
  Serial.println(); 
  delay(10);
}
void printDigits(int digits)
{
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

What I want to do at the first case is the same thing the same time everyday. In second case I want to do it every second day the same time. In third case do nothing.
The problem is at the second function. The alarm function does not work.
Thanks in advance!

In setup() why are you calling digitalWrite() before setting the pins as output?

Reading unindented code on a smartphone is too painful, sorry

(noticed though that you could do mode = (mode+1)%3; to iterate between 0,1 and 2 instead of all your if)

The modes are working good. The problem I am facing is the alarm function at mode odd-even!

Is any another option except alarm function to make a function be repeated at specific time everyday?

Read the clock every loop turn and take action when it’s time to do so?

Thank you for your replies. Can you be more specific?

nikakis30:
Thank you for your replies. Can you be more specific?

which DS1302RTC library are you using ? that one ?

if so, look at their DS1302_Serial example in the loop they read the time every second (and if you remove the delay, then you read it as fast as your processor can loop)

So you can see they have the current hour in tm.Hour, the current minute in tm.Minute and the current second in tm.Second.

if you get this moment of the day in second tm.Second + 60 *  tm.Minute + 3600 * tm.Hour then you can compare that to specific moment (also transformed into number of seconds since midnight) and decide what to do then

The example you refer to is the alarm function. OK I am using it but it's working only in the loop(every day function case1). The case 2 has the problem.

can you please rephrase your issue? reading the time in the loop - if you don't want to go to low power and sleep - is not a problem

Thank you for your interest. In case 0 I have the function showcase which have an alarm that's working. Case 1 has the function 1 whatday which has a non operative. alarm.

Case 1 only calls WhatDay(); - there is no alarm... I don’t get what you are saying... sorry

Here is Function WhatDay

void WhatDay () {
  Serial.println("Mode:ODD-EVEN");
  noOfSeconds = RTC.get();
  noOfWholeDays = noOfSeconds / 86400;
  todayIsDay = noOfWholeDays + 1;
  if (todayIsDay % 2 == 0)
  {
    todayIsOdd = false;
  }
  else
  {
    todayIsOdd = true;
  }
 if (todayIsOdd)
  {
    Serial.print("Today ODD day");
    Alarm.alarmOnce(21,50,0,AlarmAction);
  }
  else
  {
    Serial.print("Today EVEN day");
  }
  delay (25);
 }

The Alarm.alarmOnce(21,50,0,AlarmAction);,does not work.