This code returns correct minute and hour on serial output:
#include <Wire.h>
#include <RTClibExtended.h>
RTC_DS3231 rtc;
void setup () {
Serial.begin(9600);
// while the serial stream is not open, do nothing:
while (!Serial) ;
Wire.begin();
rtc.begin();
}
void loop () {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.println();
delay(1000);
}
This code returns 42 for minute and 6 for hour forever:
// http://forum.arduino.cc/index.php?topic=499210.msg3407044#msg3407044
// http://forum.arduino.cc/index.php?topic=396450.0
// HC12 Communication between Arduinos
// v2: modify to include RTC and transmit time
// corresponds with Arduino_serial_Arduino_RX_HC12_burst_test_hardware_v4
/*
This program serves to send two integers (or byte) from one (later to become two different)
HC12 transmitter.
The data are sent with start- and endmarkers
// TRANSMITTER PART
*/
#include <Wire.h>
#include <RTClibExtended.h>
#include <LowPower.h>
#define DS3231_I2C_ADDRESS 0x68
RTC_DS3231 RTC; //we are using the DS3231 RTC
int analogValue3, val3, seconds, minutes;
int HC12power = 8;
const int interval1 = 100; // HC12 power-off delay time in milliseconds
const int interval3 = 1000; // HC12 fixed retransmission timings
unsigned long previousMillis1 = 0; // timer1
unsigned long previousMillis3 = 0; // timer3
void setup()
{
Serial.begin(9600); // Open serial port to computer
pinMode(HC12power, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
Wire.begin();
RTC.begin();
}
void loop() {
// read analog pin 3
analogValue3 = analogRead(3);
// remap values from the analogValue5 variable to 0 / 255
val3 = map(analogValue3, 0, 1023, 0, 255);
DateTime now = RTC.now;
if (millis() - previousMillis3 >= interval3) {
previousMillis3 = millis();
if (digitalRead(HC12power) == LOW) {
digitalWrite(HC12power, HIGH);
digitalWrite(LED_BUILTIN, HIGH);
previousMillis1 = millis();
delay(50);
}
Serial.print('<');
Serial.print(val3);
Serial.print(',');
Serial.print(now.minute(), DEC);
Serial.print(',');
Serial.print(now.hour(), DEC);
Serial.print(",B>");
Serial.println();
/*
HC12.print('<');
HC12.print(val5);
HC12.print(',');
HC12.print(val5);
HC12.print(",B>");
*/
}
//start timer to switch off HC12
if (millis() - previousMillis1 >= interval1) {
digitalWrite(HC12power, LOW);
digitalWrite(LED_BUILTIN, LOW);
}
}
What is wrong with this last piece of code?