Diferencia entre revisiones de «now()»

De ArduWiki
Saltar a: navegación, buscar
(Ejemplo 1)
(Parámetros)
 
(No se muestran 13 ediciones intermedias del mismo usuario)
Línea 1: Línea 1:
 
== Descripción ==
 
== Descripción ==
 +
Lee la fecha y hora actual desde le reloj.
  
 
== Sintaxis ==
 
== Sintaxis ==
 
<pre>
 
<pre>
DateTime variable = rtc.now();
+
//#include <Wire.h>  //Solo si usas reloj físico
 +
#include <RTClib.h>
 +
RTC_Millis rtc;      //Reloj por software
 +
//RTC_DS1307 rtc;    //Reloj con DS1307
 +
//RTC_DS3231 rtc;    //Reloj con DS3231
 +
//RTC_PCF8523 rtc;  //Reloj con PCF8523
 +
 
 +
DateTime hoy = rtc.now();
 
</pre>
 
</pre>
  
 
== Parámetros ==
 
== Parámetros ==
;variable:Nombre de la variable de tipo [[DateTime]]
+
;hoy:Nombre de la variable de tipo [[DateTime]]
  
 
== Retorno ==
 
== Retorno ==
Fecha y hora actual en formato [[DateTime]]
+
Fecha y hora actual en formato UnixTimeStamp del tipo [[DateTime]]
 
 
 
 
== Metodos ==
 
{| class="wikitable"
 
|-
 
! Metodo !! Descripcion
 
|-
 
| .year() || año
 
|-
 
| .month() || mes (1~12)
 
|-
 
| .day() || dia (1~31)
 
|-
 
| .hour() || hora (0~23)
 
|-
 
| .minute() || minuto (0~59)
 
|-
 
| .second() || segundo (0~59)
 
|-
 
| .dayOfWeek() || dia de la semana (1~7) 1=domingo
 
|-
 
| .unixtime() || segundo desde 1-ENE-1970
 
|-
 
| .begin() || consulta si reloj esa iniciado
 
|-
 
| .adjunt() || reajusta reloj
 
|-
 
| .isrunning() || consulta estado reloj
 
|}
 
  
 
== Comentarios ==
 
== Comentarios ==
Línea 45: Línea 24:
  
 
== Advertencia ==
 
== Advertencia ==
Ninguna
+
Cuidado porque existe la función '''now()''' en ambas librerias [[Time|TimeLib.h]] y [[RTC|RTClib.h]].
  
 
== Ejemplo 1 ==
 
== Ejemplo 1 ==
 
<syntaxhighlight lang="c++">
 
<syntaxhighlight lang="c++">
#include <Wire.h>
+
//#include <Wire.h>
 
#include <RTClib.h>
 
#include <RTClib.h>
  
Línea 86: Línea 65:
  
 
== Vea también ==
 
== Vea también ==
* [[Fecha y hora]]
+
<categorytree mode=all>Libreria RTC</categorytree>
* [[time_t]] - TimeLib.h
 
* [[tmElements_t]] - TimeLib.h
 
* [[DateTime]] - RTClib.h
 
* [[TimeSpan()]] - RTClib.h
 
  
 
== Referencias ==
 
== Referencias ==
 
* [https://github.com/adafruit/RTClib RTClib] - Adafruit
 
* [https://github.com/adafruit/RTClib RTClib] - Adafruit
  
[[Category:Variables]]
+
[[Category:Libreria RTC]]

Revisión actual del 13:00 23 ene 2020

Descripción

Lee la fecha y hora actual desde le reloj.

Sintaxis

//#include <Wire.h>  //Solo si usas reloj físico
#include <RTClib.h>
RTC_Millis rtc;      //Reloj por software
//RTC_DS1307 rtc;    //Reloj con DS1307
//RTC_DS3231 rtc;    //Reloj con DS3231
//RTC_PCF8523 rtc;   //Reloj con PCF8523

DateTime hoy = rtc.now();

Parámetros

hoy
Nombre de la variable de tipo DateTime

Retorno

Fecha y hora actual en formato UnixTimeStamp del tipo DateTime

Comentarios

Nada

Advertencia

Cuidado porque existe la función now() en ambas librerias TimeLib.h y RTClib.h.

Ejemplo 1

//#include <Wire.h>
#include <RTClib.h>

RTC_Millis rtc;     //Reloj por software
//RTC_DS1307 rtc;   //Reloj con DS1307
//RTC_DS3231 rtc;   //Reloj con DS3231

DateTime hoy,anterior = 0;

void setup () {
   Serial.begin(57600);
   //Para setear RTC con fecha y hora cuando se compila
   //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
   //Para setear RTC a 21-MAR-2019 8:00:00
   rtc.adjust(DateTime(2019, 3, 21, 8, 0, 0));
}

void loop(){
   hoy = rtc.now();
   if (hoy.second() != anterior.second()){
      anterior = hoy;
      Serial.print(hoy.year());
      Serial.print('-');
      Serial.print(hoy.month());
      Serial.print('-');
      Serial.print(hoy.day());
      Serial.print(' ');
      Serial.print(hoy.hour());
      Serial.print(':');
      Serial.print(hoy.minute());
      Serial.print(':');
      Serial.println(hoy.second());
   }
}

Vea también


Referencias