Calling function with Alarm.alarmRepeat

I am trying to call sunrise using the Alarm.alarmRepeat. When i try i get an error

C:\Users\Tara Stanley\Documents\Arduino\AquariumControllerTest\AquariumControllerTest.ino: In function 'void loop()':

C:\Users\Tara Stanley\Documents\Arduino\AquariumControllerTest\AquariumControllerTest.ino:32:52: warning: invalid conversion from 'void ()(long unsigned int)' to 'OnTick_t {aka void ()()}' [-fpermissive]

alarms[0] = Alarm.alarmRepeat(8, 0, 35, sunrise);

^

In file included from C:\Users\Tara Stanley\Documents\Arduino\AquariumControllerTest\AquariumControllerTest.ino:1:0:

C:\Program Files (x86)\Arduino\libraries\TimeAlarms/TimeAlarms.h:110:13: note: initializing argument 4 of 'AlarmID_t TimeAlarmsClass::alarmRepeat(int, int, int, OnTick_t)'

AlarmID_t alarmRepeat(const int H, const int M, const int S, OnTick_t onTickHandler) {

^

The code will work like this but will continues to loop. If i comment out sunrise(currentMillis); in void loop and uncomment alarms[0] i get the above error.

#include <TimeAlarms.h>
#include <Time.h>
#include <Wire.h>
#include <ds3231.h>

#define UP 0                            // define directions for LED fade
#define DOWN 1                          // define directions for LED fade

const byte led = 11;
const int minPWM = 0;                   // constants for min and max PWM
const int maxPWM = 255;                 // constants for min and max PWM
int fadeValue = 0;                      // Global Fade Value, but be bigger than byte and signed, for rollover
int fadeInterval = 10;                  // How fast to increment?
byte fadeDirection = UP;                // State Variable for Fade Direction
byte fadeIncrement = 1;                 // How smooth to fade?
unsigned long previousFadeMillis = 0;   // millis() timing Variable, just for fading

AlarmID_t alarms[4];

void setup() {
  analogWrite(led, fadeValue);
  Serial.begin(9600);
  setTime(8, 0, 30, 7, 12, 16);
}

void loop() {
  unsigned long currentMillis = millis();
  //sunrise(currentMillis);

  showTime();
  Alarm.delay(1000);
  alarms[0] = Alarm.alarmRepeat(8, 0, 35, sunrise);
  //alarms[1] = Alarm.alarmRepeat(8,0,40, turnOffLed);

  //alarms[2] = Alarm.alarmRepeat(8,0,45, turnOnLed);
  //alarms[3] = Alarm.alarmRepeat(8,0,50, turnOffLed);
}

/*void turnOnLed() {
  digitalWrite(led, HIGH);
  }

  void turnOffLed() {
  digitalWrite(led, LOW);
  }
*/
void showTime() {
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println();
}

void printDigits(int digits) {
  Serial.print(":");
  if (digits < 10);
  Serial.print('0');
  Serial.print(digits);
}

void sunrise(unsigned long thisMillis) {
  // is it time to update yet?
  // if not, nothing happens
  if (thisMillis - previousFadeMillis >= fadeInterval) {
    // yup, it's time!
    if (fadeDirection == UP) {
      fadeValue = fadeValue + fadeIncrement;
      if (fadeValue >= maxPWM) {
        // At max, limit and change direction
        fadeValue = maxPWM;
        fadeDirection = DOWN;
      }
    } else {
      //if we aren't going up, we're going down
      fadeValue = fadeValue - fadeIncrement;
      if (fadeValue <= minPWM) {
        // At min, limit and change direction
        fadeValue = led;
        fadeDirection = UP;
      }
    }
    // Only need to update when it changes
    analogWrite(led, fadeValue);

    // reset millis for the next iteration (fade timer only)
    previousFadeMillis = thisMillis;
  }
}
void sunrise(unsigned long thisMillis)

Your sunrise function is not correct for an alarm handler. The function needs to be of the form void sunrise() and can not take input parameters.

My recommendation is to write a stand alone fade routine that you know works when called. Add a boolean variable to your program. You could use

boolean startMorningFade = false;

set it = true in sunrise.

void sunrise() {
startMorningFade = true;
}

Then in loop you will have a statement

if(startMorningFade == true){ 
//do the fade routine and set the flag false when it is complete
startMorningFade = false;
}

I would also put the alarm declaration in setup and not loop

alarms[0] = Alarm.alarmRepeat(8, 0, 35, sunrise);

I received my ds3231 and i have been trying to set the TimeAlarms to use the time provided by the rtc. I have tried several things all don't seem to work.

With this code i do not get an error it just does not turn on the led on pin 11

#include <Sodaq_DS3231.h>
#include <TimeAlarms.h>
#include <TimeLib.h>
#include <Time.h>
#include <Wire.h>


const byte led = 11;
boolean startMorningFade = false;
AlarmID_t alarms[4];

//////////             Fade Test           /////////////
int duty = 0;
int steps = 64;
int sunrisespeed = 5000;
int i;
int j;
int pulsepin = 11;

int lookup[64] = {1, 2, 4, 6, 9, 12, 16, 20, 25, 30, 36,
                  42, 49, 56, 64, 72, 81, 90, 100, 110, 121, 132,
                  144, 156, 169, 182, 196, 210, 225, 240, 256, 272, 289,
                  306, 324, 342, 361, 380, 400, 420, 441, 462, 484, 506,
                  529, 552, 576, 600, 625, 650, 676, 702, 729, 756, 784,
                  812, 841, 870, 900, 930, 961, 992, 992, 992
                 };




//////////              FOR CLOCK             ////////
uint32_t old_ts;
unsigned long previousMillis = 0;
const long interval = 1000;
char weekDay[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

void setup() {
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();

  pinMode(pulsepin, OUTPUT);

  DateTime now = rtc.now(); //get the current date-time
  uint32_t ts = now.getEpoch();
  if (old_ts == 0 || old_ts != ts) {
    old_ts = ts;
    setTime(ts);
    alarms[0] = Alarm.alarmRepeat(16, 38, 45, ledOn);
  }
}

  void loop() {
    unsigned long currentMillis = millis();

    if (currentMillis - previousMillis >= interval) {
      // save the last time you displayed the time
      previousMillis = currentMillis;
      clockDisplay();
    }
  }

  void ledOn() {
    digitalWrite(pulsepin, HIGH);
  }

  void clockDisplay() {
    DateTime now = rtc.now(); //get the current date-time
    uint32_t ts = now.getEpoch();

    if (old_ts == 0 || old_ts != ts) {
      old_ts = ts;
      Serial.print(now.year(), DEC);
      Serial.print('/');
      Serial.print(now.month(), DEC);
      Serial.print('/');
      Serial.print(now.date(), DEC);
      Serial.print(' ');
      Serial.print(now.hour(), DEC);
      Serial.print(':');
      Serial.print(now.minute(), DEC);
      Serial.print(':');
      Serial.print(now.second(), DEC);
      Serial.print(' ');
      //Serial.print(weekDay[now.dayOfWeek()]);
      Serial.println();
      Serial.print("Seconds since Unix Epoch: ");
      Serial.print(ts, DEC);
      Serial.println();
    }
  }

The more i dig and try to solve this the more confused i become.

Take a look at the reference material for the TimeAlarms library. TimeAlarms Library, Run Functions At Specific Times

Somewhere in the sketch need to use a call to Alarm.delay() to make the alarms function. Replace any standard delays with Alarm.delay, and if there are no delays in the sketch just add Alarm.delay(1) at the start of loop().

Also, you shouldn't call the same pin two names

const byte led = 11;
int pulsepin = 11;
pinMode(pulsepin, OUTPUT);

Thank you, that solved that issue.

I'm now having a problem trying check time from the rtc to see if say 9:00 has passed and if so do function aka-turn on lights.

I know the code is messy but i'm a noob and just trying to work my way through it and learn as much as possible.

Here's the code, the problem im having is with the if(statement) in void startSunrise.

#include <Sodaq_DS3231.h>
#include <TimeAlarms.h>
#include <TimeLib.h>
#include <Time.h>
#include <Wire.h>

//--------------------LED'S-------------------------------

boolean startMorningFade = false;
boolean startEveningFade = false;
int led = 11;
int brightness = 0;
int prevBrightness;
int fadeCurrentMillis = 0;
int fadePreviousMillis = 0;
int fadeInterval = 7059;
int x = 0;

//-------------------CLOCK---------------------------------

unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
const long interval = 1000;
uint32_t old_ts;
char weekDay[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

//----------------------------------------------------------

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();
  pinMode(led, OUTPUT);
}

void loop() {
  currentMillis = millis();
  fadeCurrentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;                      // Save the last time time and temp was displayed
    //clockDisplay();                                      //Print date and time to serial(9600)
    //roomTemp();                                          //Print temp from rtc to serial(9600)
  }


  if (startMorningFade == true) {                        //Sunrise led over 30 minutes
    if (x <= 255) {
      if (fadeCurrentMillis - fadePreviousMillis >= fadeInterval) {
        // save the last time sunrise(); was called
        fadePreviousMillis = fadeCurrentMillis;
        sunrise();
        //Serial.println(x);
        x++;
      }
      if (x >= 255) {
        startMorningFade = false;
      }
    }
  }
}

//------------------------------------------------------------------------

void startSunrise() {
  DateTime now = rtc.now(); //get the current date-time
  uint32_t ts = now.getEpoch();
  if (now.hour() && now.minute() >= hour(9) && minute(0)) {
    Serial.println("It Works");
    startMorningFade = true;
  }
}

//------------------------------------------------------------------------

void sunrise() {              //Fade in led value counts up from 0 to 255
  if (x <= 255) {
    brightness++;
    analogWrite(led, brightness);
  }
}

//------------------------------------------------------------------------

void clockDisplay() {
  DateTime now = rtc.now(); //get the current date-time
  uint32_t ts = now.getEpoch();

  if (old_ts == 0 || old_ts != ts) {
    old_ts = ts;
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.date(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.print(' ');
    //Serial.print(weekday[now.dayOfWeek()]);
    Serial.println();
    Serial.print("Seconds since Unix Epoch: ");
    Serial.print(ts, DEC);
    Serial.println();
  }
}
  if (now.hour() && now.minute() >= hour(9) && minute(0)) {

If now.hour() what? If minute(0) what?

Where is the hour() function defined? Where is the minute() function defined?

PaulS:

  if (now.hour() && now.minute() >= hour(9) && minute(0)) {

If now.hour() what? If minute(0) what?

Where is the hour() function defined? Where is the minute() function defined?

Thanks, this kicked a brain cell....

#include <Sodaq_DS3231.h>
#include <TimeAlarms.h>
#include <TimeLib.h>
#include <Time.h>
#include <Wire.h>

byte startHour = 9;
byte startMinute = 1;


//--------------------LED'S-------------------------------

boolean startMorningFade = false;
boolean startEveningFade = false;
int led = 11;
int brightness = 0;
int prevBrightness;
int fadeCurrentMillis = 0;
int fadePreviousMillis = 0;
int fadeInterval = 7059;
int x = 0;

//-------------------CLOCK---------------------------------

unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
const long interval = 1000;
uint32_t old_ts;
char weekDay[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

//----------------------------------------------------------

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();
  pinMode(led, OUTPUT);
}

void loop() {
  currentMillis = millis();
  fadeCurrentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;                      // Save the last time time and temp was displayed
    //clockDisplay();                                      //Print date and time to serial(9600)
    //roomTemp();                                          //Print temp from rtc to serial(9600)
  }


  if (startMorningFade == true) {                        //Sunrise led over 30 minutes
    if (x <= 255) {
      if (fadeCurrentMillis - fadePreviousMillis >= fadeInterval) {
        // save the last time sunrise(); was called
        fadePreviousMillis = fadeCurrentMillis;
        sunrise();
        //Serial.println(x);
        x++;
      }
      if (x >= 255) {
        startMorningFade = false;
      }
    }
  }
  startSunrise();
}

//------------------------------------------------------------------------

void startSunrise() {
  DateTime now = rtc.now(); //get the current date-time

  if (now.hour() >= startHour && now.minute() >= startMinute) {
    Serial.println("It Works");
    startMorningFade = true;
  }
}

//------------------------------------------------------------------------

void sunrise() {              //Fade in led value counts up from 0 to 255
  if (x <= 255) {
    brightness++;
    analogWrite(led, brightness);
  }
}

//------------------------------------------------------------------------

void clockDisplay() {
  DateTime now = rtc.now(); //get the current date-time
  uint32_t ts = now.getEpoch();

  if (old_ts == 0 || old_ts != ts) {
    old_ts = ts;
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.date(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.print(' ');
    //Serial.print(weekday[now.dayOfWeek()]);
    Serial.println();
    Serial.print("Seconds since Unix Epoch: ");
    Serial.print(ts, DEC);
    Serial.println();
  }
}

fixed by

byte startHour = 9;
byte startMinute = 1;


//////////////and/////////////

void startSunrise() {
  DateTime now = rtc.now(); //get the current date-time

  if (now.hour() >= startHour && now.minute() >= startMinute) {
    Serial.println("It Works");
    startMorningFade = true;
  }
}