Put int variables into a time_t type

Hi! I need help! I've got a problem: I need to compare hours and minutes setted by a user whit ordinary hours and minutes, for an alarm. I've tried with many libraries and methods like <Time.h> and <TimeLib.h> but I don''t understand many things. For example, a "time_t" type, need all fields defined? And, is possible take ints from a form for convert them in "time_t" and compare it whit the current time of an RTC?

Pratically, I need to convert variables "ore" and "minuti" (both ints) into a certain time type (respectively, "hours" and "minutes" of the type) to compare it with the incoming time. This is the part of code that take inputs from two components of an app called Blink which allows an "interface" between Android and Arduino:

BLYNK_WRITE(V0){
  int i;
  for(i=1; i<=24; i++){
    if(param.asInt() == i){
      if(param.asInt() < 10){
      Serial.println("Ore: 0");
      Serial.println(i-1);
      ore = i-1;
      Serial.println(ore);
      }else{
        Serial.println("Ore: ");
        Serial.println(i-1);
        ore = i-1;
        Serial.println(ore);
      }
    }
  }
}



BLYNK_WRITE(V1){
  int i;
  for(i=1; i<=60; i++){
    if(param.asInt() == i){
      if(param.asInt() < 10){
      Serial.print("Minuti: 0");
      Serial.println(i-1);
      minuti = i-1;
      }else{
        Serial.println("Minuti: ");
        Serial.println(i-1);
        minuti = i-1;
      }
    }
  }
}

Into the app, I take hours and minutes that I need to compare to the real time for ring an alarm (sorry for my bad english...)

If you have some experience, I wrote a library that can accommodate what you are trying to do, and it may be easier for you to use.

Take a look at my Git Repo DailyTimer library, there is an example that you can try.

Anyone who wants to make a contribution is welcome; I've been using it for a while and it seems pretty stable.

example sketch:

#include "DailyTimer.h"
#include <Time.h>

DailyTimer timer1( 14, 37, 14, 39, EVERY_DAY, FIXED, customSeedGenerator);   // optional callback function for random number generation, see below example
DailyTimer timer2( 14, 38, 20, 30, SATURDAY, RANDOM_START);                   // default is FIXED, this will randomize the start time only
DailyTimer timer3( 14, 45,  1, 30, WEEKENDS);                                 // creates with a fixed start time and end time
bool timer1_LastState = false;

void setup() 
{
  setTime(1461422271);                        // set your sync provider!!
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  timer1.setDaysActive(WEEKENDS);             // Set the timer to be active on weekends
  //timer1.setDaysActive(B10101010);          // or define a custom day mask... SMTWTFS0
  //timer1.setRandomDays(4);                  // or four random days per week
  //timer2.setRandomOffset(30, RANDOM_START); // Change to random start time, +/- 30 minutes... max 59 mins
  Serial.println(timer1.getDays(), BIN);      // getDays() returns active days as a byte in the format above
  Serial.print(F("Today is "));
  Serial.println(dayStr(weekday()));
}

uint32_t lastUpdateTime = 0;

void loop() 
{
  bool timerState = timer1.isActive();
  if(timerState != timer1_LastState)
  {
    if(timerState)
    {
      digitalWrite(13, HIGH);
      Serial.println(F("Timer 1 is ON"));
    }
    else
    {
      digitalWrite(13, LOW);
      Serial.println(F("Timer 1 is OFF"));
    }
    timer1_LastState = timerState;
  }
  if(timer2.startTrigger()) 
  {
    Serial.println(F("Timer 2 FIRED!"));
  }
  if(millis() - lastUpdateTime > 1000UL)
  {
    char timeBuffer[32] = "";
    sprintf(timeBuffer, "%02d/%02d/%d %02d:%02d:%02d" , month(), day(), year(), hour(), minute(), second());
    Serial.println(timeBuffer);
    lastUpdateTime += 1000UL;
  }
  if(timer3.startTrigger())
  {
    Serial.println(F("Timer 3 FIRED!"));
  }
}

uint32_t customSeedGenerator()
{
  uint32_t seed = analogRead(A5);
  delay(100);
  seed << 8;
  return  seed |= analogRead(A5);
}

BulldogLowell:
If you have some experience, I wrote a library that can accommodate what you are trying to do, and it may be easier for you to use.

Take a look at my Git Repo DailyTimer library, there is an example that you can try.

Anyone who wants to make a contribution is welcome; I've been using it for a while and it seems pretty stable.

example sketch:

#include "DailyTimer.h"

#include <Time.h>

DailyTimer timer1( 14, 37, 14, 39, EVERY_DAY, FIXED, customSeedGenerator);  // optional callback function for random number generation, see below example
DailyTimer timer2( 14, 38, 20, 30, SATURDAY, RANDOM_START);                  // default is FIXED, this will randomize the start time only
DailyTimer timer3( 14, 45,  1, 30, WEEKENDS);                                // creates with a fixed start time and end time
bool timer1_LastState = false;

void setup()
{
  setTime(1461422271);                        // set your sync provider!!
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  timer1.setDaysActive(WEEKENDS);            // Set the timer to be active on weekends
  //timer1.setDaysActive(B10101010);          // or define a custom day mask... SMTWTFS0
  //timer1.setRandomDays(4);                  // or four random days per week
  //timer2.setRandomOffset(30, RANDOM_START); // Change to random start time, +/- 30 minutes... max 59 mins
  Serial.println(timer1.getDays(), BIN);      // getDays() returns active days as a byte in the format above
  Serial.print(F("Today is "));
  Serial.println(dayStr(weekday()));
}

uint32_t lastUpdateTime = 0;

void loop()
{
  bool timerState = timer1.isActive();
  if(timerState != timer1_LastState)
  {
    if(timerState)
    {
      digitalWrite(13, HIGH);
      Serial.println(F("Timer 1 is ON"));
    }
    else
    {
      digitalWrite(13, LOW);
      Serial.println(F("Timer 1 is OFF"));
    }
    timer1_LastState = timerState;
  }
  if(timer2.startTrigger())
  {
    Serial.println(F("Timer 2 FIRED!"));
  }
  if(millis() - lastUpdateTime > 1000UL)
  {
    char timeBuffer[32] = "";
    sprintf(timeBuffer, "%02d/%02d/%d %02d:%02d:%02d" , month(), day(), year(), hour(), minute(), second());
    Serial.println(timeBuffer);
    lastUpdateTime += 1000UL;
  }
  if(timer3.startTrigger())
  {
    Serial.println(F("Timer 3 FIRED!"));
  }
}

uint32_t customSeedGenerator()
{
  uint32_t seed = analogRead(A5);
  delay(100);
  seed << 8;
  return  seed |= analogRead(A5);
}

Wow! Awesome! I'll try it and let you know!

Delta_G:
Use the Time library. Make a tmElements and use makeTime to put that into a time_t.

Could you, please, let me see? I'm in big troubles! I've tried to make it like this:

if(ore == NULL){
  t.tm_year = year();
  t.tm_mon = month();
  t.tm_mday = day();
  t.tm_hour = ore;  
  t.tm_min = minuti;   
  t.tm_sec = second();   
  t.tm_isdst = 0;

  sveglia = mktime(&t);
  }

Where "ore" and "minuti" are ints taken by Blink application

if ore is an int why are you comparing it to a NULL pointer?
And surely you mean !=

KeithRB:
if ore is an int why are you comparing it to a NULL pointer?
And surely you mean !=

Because in globals I set them NULL and so, if they were NULL because the Blink app doesn't put nothing into that ints, I'll make a control (if != NULL) which doesn't assign nothing.

Maybe, all the code, help to understand what I mean:

/*-------------------LIBRERIE----------------------------------*/
#define SENSIBILITA 200
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <WiFi.h>
#include <BlynkSimpleWifi.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <Time.h>
#include <DailyTimer.h>
#include <WidgetRTC.h>
#include <TimeAlarms.h>
#include "pitches.h"

/*--------------------GLOBALI---------------------------------*/
char auth[] = "********";

char ssid[] = "*******";
char pass[] = "*******";          // Set to "" for open networks
IPAddress ip (45,55,195,102);

int SHIR = A5;

//SimpleTimer timer;

WidgetRTC rtc;
BLYNK_ATTACH_WIDGET(rtc, V4);

int ore = NULL;
int minuti = NULL;

bool hosuonato = false;
int dist;

BLYNK_WRITE(V0){
  int i;
  for(i=1; i<=24; i++){
    if(param.asInt() == i){
      if(param.asInt() < 10){
      Serial.println("Ore: 0");
      Serial.println(i-1);
      ore = i-1;
      Serial.println(ore);
      }else{
        Serial.println("Ore: ");
        Serial.println(i-1);
        ore = i-1;
        Serial.println(ore);
      }
    }
  }
}



BLYNK_WRITE(V1){
  int i;
  for(i=1; i<=60; i++){
    if(param.asInt() == i){
      if(param.asInt() < 10){
      Serial.print("Minuti: 0");
      Serial.println(i-1);
      minuti = i-1;
      }else{
        Serial.println("Minuti: ");
        Serial.println(i-1);
        minuti = i-1;
      }
    }
  }
}

int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};

int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4
};

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

void clockDisplay()
{
  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details
  Serial.print("Current 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();
}

void suona()
{
  delay(500);
        for (int thisNote = 0; thisNote < 8; thisNote++) {
        int noteDuration = 1000 / noteDurations[thisNote];
        tone(8, melody[thisNote], noteDuration);
        int pauseBetweenNotes = noteDuration * 1.30;
        delay(pauseBetweenNotes);
        noTone(8);
      }
}

void sveglia()
{
  int dist = analogRead(SHIR);
  if(hosuonato == false){
    Serial.println(dist);
    suona();
    if(dist > SENSIBILITA){
      Serial.println(dist);
      hosuonato = true;
    }
  }
}
/*------------------SETUP-------------------------------------*/
void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass, ip, 8442);
  pinMode( SHIR,INPUT );

  while (Blynk.connect() == false) {
    // Wait until connected
  }
  rtc.begin();
  //Alarm.alarmRepeat(ore,minuti,0, suona);
}
  
/*---------------------LOOP--------------------------------------*/
void loop()
{ 
  time_t sveglia;
  struct tm t;
  
  if(ore == NULL){
  t.tm_year = year();
  t.tm_mon = month();
  t.tm_mday = day();
  t.tm_hour = ore;  
  t.tm_min = minuti;   
  t.tm_sec = second();   
  t.tm_isdst = 0;

  sveglia = mktime(&t);
  }
  
  Blynk.run();
  /*clockDisplay();
  Alarm.delay(1000);*/
  sveglia();
  
  //timer.run();
}

That isn't your real Wifi login, is it? :slight_smile:

aarg:
That isn't your real Wifi login, is it? :slight_smile:

Aaaah... Damn, I forgot. But I stay in south Italy! If someone come here and want to use it, is welcome! :smiley: