I want to be able to set the time using the serial monitor, but I can't seem to get it working with my code.
The code im using is "Setserial" by "JChristensen"
This is my current code
#include <Wire.h>
#include <DS3232RTC.h>
#include <dht.h>
#include <Stepper.h>
#include <TimeLib.h>
#include <Streaming.h>
#define DHT11_PIN 7
int val;
const int relais_warmte = 6;
const int stappen_per_draai = 285; // 1000 stappen = 3,5 toeren -> 1 toer = 285 stappen
const unsigned long interval = 3600000; // dit is 1 uur
unsigned long previousMillis = 0;
bool raam_schakelaar = false; //Als het raam dicht is dan is deze HIGH en als het raam open is dan is deze LOW
byte stappen_teller = 0; // de teller waarmee ik kan zien of het dak dicht is of niet
float VerwarmingLaagsteTemp; //globale variabele voor de verwarming
float VerwarmingHoogsteTemp;
float RaamLaagsteTemp;
float RaamHoogsteTemp;
Stepper myStepper(stappen_per_draai, 8, 9, 10, 11);
dht DHT;
void setup() {
Serial.begin(9600);
myStepper.setSpeed(30);
pinMode(relais_warmte, OUTPUT);
pinMode (raam_schakelaar, INPUT);
setSyncProvider(RTC.get);
Serial << F("RTC Sync");
if (timeStatus() != timeSet) Serial << F(" FAIL!");
Serial << endl;
if(raam_schakelaar, LOW) myStepper.step(-stappen_per_draai); // Het raam is open en moet dicht
}
void loop() {
if(Serial.available()>= 12) tijd_instellen();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) { // hierin wordt de RTC per uur gemeten
previousMillis = currentMillis;
val = (hour()); // Hiermee wordt het uur gelezen, en val krijgt die waarde
}
if ( val >= 5 && val <= 19){ //als het dag is dan ..
VerwarmingLaagsteTemp = 18.7;
VerwarmingHoogsteTemp = 19.3;
RaamLaagsteTemp = 25.7;
RaamHoogsteTemp = 26.3;
}
else{
VerwarmingLaagsteTemp = 12.7;
VerwarmingHoogsteTemp = 13.3;
RaamLaagsteTemp =18.7;
RaamHoogsteTemp = 19.3;
}
temperatuur_actie();
}
void temperatuur_actie(){ // functie waarin de actie gebeurd
int chk = DHT.read11(DHT11_PIN); //Er wordt gelezen vanuit de sensor welke temperatuur het is
Serial.print("Temperatuur = "); // Temperatuur = x
Serial.println(DHT.temperature);
delay(1000); //Wacht 1 second
if (chk < VerwarmingLaagsteTemp) digitalWrite(relais_warmte, HIGH);
if (chk > VerwarmingHoogsteTemp) digitalWrite(relais_warmte, LOW);
if (chk > RaamHoogsteTemp)if(stappen_teller = 0)
{
myStepper.step(stappen_per_draai);
stappen_teller = 1;
}
if (chk < RaamLaagsteTemp) if(stappen_teller = 1){ // als het raam open in dan pas mag het dicht gaan
myStepper.step(-stappen_per_draai);
stappen_teller = 0;
}
}
void tijd_instellen(){
static time_t tLast; //Dit stukje code komt van het internet, hiermee wordt de tijd bepaald in de seriele monitor
time_t t;
tmElements_t tm;
int y = Serial.parseInt();
if (y >= 100 && y < 1000)
Serial << F("Error: Year must be two digits or four digits!") << endl;
else {
if (y >= 1000)
tm.Year = CalendarYrToTm(y);
else // (y < 100)
tm.Year = y2kYearToTm(y);
tm.Month = Serial.parseInt();
tm.Day = Serial.parseInt();
tm.Hour = Serial.parseInt();
tm.Minute = Serial.parseInt();
tm.Second = Serial.parseInt();
t = makeTime(tm);
RTC.set(t);
setTime(t);
Serial << F("RTC set to: ");
printDateTime(t);
Serial << endl;
while (Serial.available() > 0) Serial.read();
}
t = now();
if (t != tLast) {
tLast = t;
printDateTime(t);
if (second(t) == 0) {
float c = RTC.temperature() / 4.;
float f = c * 9. / 5. + 32.;
Serial << F(" ") << c << F(" C ") << f << F(" F");
}
Serial << endl;
}
}
void printDateTime(time_t t){ // dit hoort nog bij de klok
printDate(t);
Serial << ' ';
printTime(t);
}
void printTime(time_t t)
{
printI00(hour(t), ':');
printI00(minute(t), ':');
printI00(second(t), ' ');
}
// print date to Serial
void printDate(time_t t)
{
printI00(day(t), 0);
Serial << monthShortStr(month(t)) << _DEC(year(t));
}
void printI00(int val, char delim)
{
if (val < 10) Serial << '0';
Serial << _DEC(val);
if (delim > 0) Serial << delim;
return; //tot hier
}
I can not confirm you issue. When I run your program and enter the year,month,day,hour,minute,second from the serial monitor I see your RTC set to: message and the time is updated correctly in the rtc.
cattledog:
I can not confirm you issue. When I run your program and enter the year,month,day,hour,minute,second from the serial monitor I see your RTC set to: message and the time is updated correctly in the rtc.
Hi,
I can say it does work (sort of), but after 1 read it resets the time to 31 DECEMBER1999
I don't know why? How do I fix this?
Many thanks for all the replies I got so far I really appreciate it!
I set the time to 22 OCT 2022 but after 1 time the program has looped it sets the time to 31 DECEMBER 1999
You shouldn't use external links like that. There is a possability that these links are malware-infected.
You can attach code that exceeds 9000 byte as an *.ino or a zip-file you can attach pictures to the postings if you click on preview you get an addtional option to manage attachment.
As long as you just write such short messages the forum will be unable to help you because the non-existant glas-pheres of the users (including th evirtual ones on the screen) do not show were your error is.
You still have to still have to provide all the details what you did do
And this is only the code for the RTC (by JChristensen). Im trying to just set the RTC using the serial monitor, but this code resets the input after 1 loop and I don't know why. I don't fully understand his code but I haven't found a code thath has (kinda) worked so far yet.
but this code resets the input after 1 loop and I don't know why.
Neither do I. When I run the code you posted, it does not reset.
I do not see how the code can reset if there is no serial input.
Indeed, the last line of tijd_instellen() throws away any additional characters in the serial buffer and clears it with
while (Serial.available() > 0) Serial.read();
Are you using the serial monitor of the ide?
You previously said that when you run the library example to set the time from serial without any additional code, it works correctly. Indeed, I have worked with Jack Christensen's code extensively, and it is rock solid.
When I test your code, I am not physically connected to a temperature sensor or stepper, so you might try to remove those pieces of code or disconnect the outputs to see if you can avoid the reset.
cattledog:
Neither do I. When I run the code you posted, it does not reset.
I do not see how the code can reset if there is no serial input.
Indeed, the last line of tijd_instellen() throws away any additional characters in the serial buffer and clears it with
while (Serial.available() > 0) Serial.read();
Are you using the serial monitor of the ide?
You previously said that when you run the library example to set the time from serial without any additional code, it works correctly. Indeed, I have worked with Jack Christensen's code extensively, and it is rock solid.
When I test your code, I am not physically connected to a temperature sensor or stepper, so you might try to remove those pieces of code or disconnect the outputs to see if you can avoid the reset.
I found what was wrong, I tried the standalone code. And it works fine now (It didn't previously idk why).
Im finding out what went wrong putting JChristensen's code into mine.
Thanks for the replies