1307 with timeAlarm

This issue has come up in my project guidance thread but I thought I’d get it a thread of its own. I’ve read everything I can find but nothing seems to help me with this.

I want to use the time from the rtc to trigger repeating functions. basically 3 times a day, I want a servo to turn and then turn back after a second or two.

I was able to do this with an if statement but that screwed up other functions that I need to have working so I’m trying another way.

I’ll post my best attempt and you folks can tell me what I’m doing wrong.

#include <DS1307RTC.h>
#include <TimeAlarms.h>
#include <Servo.h>
#include <Wire.h>
#include <Time.h>

Servo feeder;

void setup() {
  Wire.begin();
  Serial.begin(9600);
  setSyncProvider(RTC.get);
  feeder.attach(10);
  feeder.write(0); 
  Alarm.alarmRepeat(0,55,0, feederOpen);     //12:55:00
  Alarm.alarmRepeat(0,55,2, feederClose);     //12:55:02
}

void loop() {
  
}

void feederOpen() {
    feeder.write(160);
}

void feederClose() {
  feeder.write(0);
}

This is just the alarm part. I’m trying to get this functioning before I put the other half of my code, that has been working reliably for quite a while, into it.

Thanks

At the very least you need Alarm.delay() in the loop() function but using TimeAlarms seems like an overkill for what you are trying to do. With an RTC in place it is easy to write code that checks the time and acts on it at certain times.

What problems did you have with using if to test the time ?

I’ll post my code below. I have that Servo that’s set to activate at certain times and three others set to be activated by rfid. When I put an if statement in the loop with the rfid stuff, all of the servos controlled by the rfid reading would just stop working after 20-40 seconds. All of these functions (servo by time, and 3 servos by rfid) need to work independent of each other.

I’m still quite new so my code might make you a bit ill

/* MOSI: Pin 11 / ICSP-4
 * MISO: Pin 12 / ICSP-1
 * SCK : Pin 13 / ICSP-3
 * SS : Pin 4,8,7 (Configurable)
 * RST : Pin 9 (Configurable)
 */
                               
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
                               
#define SS1_PIN 4
#define SS2_PIN 7
#define SS3_PIN 8
#define RST_PIN 9

Servo feeder;
Servo cover1;
Servo cover2;
Servo cover3;

MFRC522 readerOne(SS1_PIN, RST_PIN);
MFRC522 readerTwo(SS2_PIN, RST_PIN);
MFRC522 readerThree(SS3_PIN, RST_PIN);

long coverDuration = 2000;
long feedDuration = 1000;
long closeTime1 = 0;
long closeTime2 = 0;
long closeTime3 = 0;
long feedTime = 0;

void setup() {
  SPI.begin();
  readerOne.PCD_Init();
  readerTwo.PCD_Init();
  readerThree.PCD_Init();
  feeder.attach(10);
  feeder.write(0);
  cover1.attach(3);
  cover1.write(0);
  cover2.attach(5);
  cover2.write(0);
  cover3.attach(6);
  cover3.write(5);
}
       
  int myCat = 0x03;  
  int readTagOne;
  int readTagTwo;
  int readTagThree; 
  
void loop() { 
  unsigned long currentMillis = millis();
  tmElements_t tm;    
  
  if (RTC.read(tm)) {
  if (tm.Hour == 19){
    if (tm.Minute == 13 || tm.Minute == 14 || tm.Minute == 15 ){
      if (tm.Second == 0){
        feeder.write(160);
        feedTime = currentMillis;
      }
    }
  }
}
  if (currentMillis - feedTime > feedDuration) {  
    feeder.write(0);
  }
  
  if (readerOne.PICC_IsNewCardPresent()){
     readerOne.PICC_ReadCardSerial();
     readTagOne = (readerOne.uid.uidByte[0]);  
        if(readTagOne == myCat){
            cover1.write(160);
            closeTime1 = currentMillis;            
        }  
  }   

  if(currentMillis - closeTime1 > coverDuration) {
    cover1.write(0);
  }  
      
  if (readerTwo.PICC_IsNewCardPresent()){
     readerTwo.PICC_ReadCardSerial();  
       readTagTwo = (readerTwo.uid.uidByte[0]);
        if(readTagTwo == myCat){
          cover2.write(140);
          closeTime2 = currentMillis;
        }
  } 
 
   if(currentMillis - closeTime2 > coverDuration) {
     cover2.write(0);
   } 
  
      
   if (readerThree.PICC_IsNewCardPresent()){
     readerThree.PICC_ReadCardSerial(); 
       readTagThree = (readerThree.uid.uidByte[0]); 
        if(readTagThree == myCat){
          cover3.write(160);
          closeTime3 = currentMillis;
        }
   }
   
   if(currentMillis - closeTime3 > coverDuration) {
     cover3.write(0);
   }   
}

I know that first if might be unnecessary. I had it working in a less weird way but i changed it to this in an attempt to see if that might be the issue. It wasn’t.