daylight saving time... there is a libaray which can handle that automatically.
Could you post a link please, cattledog. Would be interested to see that.
daylight saving time... there is a libaray which can handle that automatically.
Could you post a link please, cattledog. Would be interested to see that.
cattledog:
You can add this at the top of loop.
No matter where I added that, the sketch wouldn't compile. I added it immediately below void loop(), immediately above void loop() and replaced setSyncProvider(RTC.get); along with the few lines associated with setSync.
The reason I'd like to change the time in the serial monitor is, I want to add stirrers and dosers. They need to happen several times a day at certain times and be coordinated. It would be nice to change time at the SM for a minute before things are supposed to happen rather than loading sketches back and forth. I want to watch things happen on the SM before firing up stirring motors and pumps. BTW, I'm only concerned with hours and minutes.
I'd swear that last year on this very same sketch I changed time in the SM at will. I copied and pasted this sketch and tweaked things at the margins but tried to avoided messing around with the meat of the sketch. I must have bumped into something I shouldn't have.
Do you want me to strip the sketch down as best I can and post it?
the sketch wouldn't compile.
Please post the sketch and the error messages.
Do you want me to strip the sketch down as best I can and post it?
Give me a look at the actual sketch which would not compile first.
daylight saving time... there is a library which can handle that automatically.
Could you post a link please, cattledog. Would be interested to see that.
Timezone.h Available through the library manager.
OMG, this is an X/Y problem !
The reason I'd like to change the time in the serial monitor is, I want to add stirrers and dosers. They need to happen several times a day at certain times and be coordinated. It would be nice to change time at the SM for a minute before things are supposed to happen rather than loading sketches back and forth. I want to watch things happen on the SM before firing up stirring motors and pumps. BTW, I'm only concerned with hours and minutes.
if you look at the wall clock you say it is 3:14 and then say, I need to run this for 7 minutes.
you do not change the time on the wall clock and use the wall clock as a timer.
there are lots and lots of ways to make timers in software.
if you say, this needs to go on a 3:17, then you just make an alarm for 3:17.
or 3:16 so you have that minute before it goes off/on
The Arduino has a pretty accurate internal clock, especially in the short term of a few hours.
you can have an event, like a button press
then 21 minutes and 17 seconds later turn on something.
you can have a temperature hit 165.000 deg and then start a 5 minute timer...
please take a minute and write up what your project needs for times. it will be far easier to create count up timers, and count down timers and duration timers, and alarm timers in the regular software than to re-set the system time.
IMHO, the only reason for a RTC is for keeping track of time over longer periods or when power loss is a concern
No matter where I added that, the sketch wouldn't compile.
Add these two variables right after the if Serial.available() line
if (Serial.available() >= 12) {
//add these two variables
time_t t;
tmElements_t tm;
dave-in-nj, I need accuracy over the long haul. It's my understanding that arduino's clock can be off a couple minutes a day. I plan to add alarms for stirrers and pumps that will operate several times a day. I want to be able to manipulate time on the Serial Monitor so I can easily see if things are working before I get out the soldering iron.
cattledog, here is the sketch that compiles but won't let the SM set time. I replaced this part of the sketch
//use the RTC to set the system time
setSyncProvider(RTC.get); // the function to get the time from the RTC
if(timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
with both parts that you posted and it did compile but I still could not set time at the SM. The sketch is back to it's original form.
//This sketch made possible with generous help from TomGeorge and cattledog!
#include <Time.h>
#include <DS3232RTC.h>
#include <Wire.h>
long secondsInMs(int s){
return s*1000L; //values will be expressed in seconds
}
int purpLED = 9;
int purpLEDmax;
int purpPOT = A0;
int purpPOTval;
int purpFAN = 12;
int nemLED = 3;
int nemLEDmax;
int nemPOT = A1;
int nemPOTval;
int nemFAN = 4;
int leftrockLED = 6;
int leftrockLEDmax;
int leftrockPOT = A3;
int leftrockPOTval;
int leftrockFAN = 8;
int rightrockLED = 5;
int rightrockLEDmax;
int rightrockPOT = A2;
int rightrockPOTval;
int rightrockFAN = 7;
void setup() {
Serial.begin(9600);
pinMode(nemPOT, INPUT);
pinMode(nemLED, OUTPUT);
pinMode(nemFAN, OUTPUT);
pinMode(rightrockPOT, INPUT);
pinMode(rightrockLED, OUTPUT);
pinMode(rightrockFAN, OUTPUT);
pinMode(leftrockPOT, INPUT);
pinMode(leftrockLED, OUTPUT);
pinMode(leftrockFAN, OUTPUT);
pinMode(purpPOT, INPUT);
pinMode(purpLED, OUTPUT);
pinMode(purpFAN, OUTPUT);
//use the RTC to set the system time
setSyncProvider(RTC.get); // the function to get the time from the RTC
if(timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
// create the alarms
Alarm.alarmRepeat(10, 33, 0, purpLEDonAlarm); // every morning
Alarm.alarmRepeat(10, 33, 30, nemLEDonAlarm); // every morning
Alarm.alarmRepeat(10, 34, 0, rightrockLEDonAlarm); // every morning
Alarm.alarmRepeat(10, 34, 30, leftrockLEDonAlarm); // every morning
Alarm.alarmRepeat(21, 56, 0, LEDoffAlarm); // every evening
}
void loop()
{
nemPOTval = analogRead(nemPOT); //read pot, save value 0 to 1023 range
nemLEDmax = map(nemPOTval, 0, 1023, 0, 255); //convert value to 0 to 255 range
rightrockPOTval = analogRead(rightrockPOT);
rightrockLEDmax = map(rightrockPOTval, 0, 1023, 0, 255);
leftrockPOTval = analogRead(leftrockPOT);
leftrockLEDmax = map(leftrockPOTval, 0, 1023, 0, 255);
purpPOTval = analogRead(purpPOT);
purpLEDmax = map(purpPOTval, 0, 1023, 0, 255);
digitalClockDisplay();
Alarm.delay(1000); // wait one second between clock display
}
// functions to be called when an alarm triggers:
void purpLEDonAlarm()
{
Serial.println("Alarm: - fade up purpLED ");
for (int fadeValue = 0 ; fadeValue <= purpLEDmax; fadeValue += 1) // fade up to value set in purpLEDmax
{
analogWrite(purpLED, fadeValue);
Serial.print("\tpurpLEDmax =");
Serial.print("\tfadeup ");
Serial.print(purpLEDmax);
Serial.print("\tfadeValue =");
Serial.println(fadeValue);
delay(600);
if(fadeValue>=20)
{
digitalWrite(purpFAN, HIGH);
Serial.println("purpFAN on");
} } }
// functions to be called when an alarm triggers:
void nemLEDonAlarm()
{
Serial.println("Alarm: - fade up nemLED ");
for (int fadeValue = 0 ; fadeValue <= nemLEDmax; fadeValue += 1) // fade up to value set in nemLEDmax
{
analogWrite(nemLED, fadeValue);
Serial.print("\tfadeup ");
Serial.print("\tnemLEDmax =");
Serial.print(nemLEDmax);
Serial.print("\tfadeValue =");
Serial.println(fadeValue);
delay(600);
if(fadeValue>=20)
{
digitalWrite(nemFAN, HIGH);
Serial.println("nemFAN on");
}}}
// functions to be called when an alarm triggers:
void rightrockLEDonAlarm()
{
Serial.println("Alarm: - fade up rightrockLED ");
for (int fadeValue = 0 ; fadeValue <= rightrockLEDmax; fadeValue += 1) // fade up to value set in rightrockLEDmax
{
analogWrite(rightrockLED, fadeValue);
Serial.print("\tfadeup ");
Serial.print("\trightrockLEDmax =");
Serial.print(rightrockLEDmax);
Serial.print("\tfadeValue =");
Serial.println(fadeValue);
delay(600);
if(fadeValue>=20)
{
digitalWrite(rightrockFAN, HIGH);
Serial.println("rightrockFAN on");
}}}
// functions to be called when an alarm triggers:
void leftrockLEDonAlarm()
{
Serial.println("Alarm: - fade up leftrockLED ");
for (int fadeValue = 0 ; fadeValue <= leftrockLEDmax; fadeValue += 1) // fade up to value set in leftrockLEDmax
{
analogWrite(leftrockLED, fadeValue);
Serial.print("\tfadeup ");
Serial.print("\tleftrockLEDmax =");
Serial.print(leftrockLEDmax);
Serial.print("\tfadeValue =");
Serial.println(fadeValue);
delay(600);
if(fadeValue>=20)
{
digitalWrite(leftrockFAN, HIGH);
Serial.println("leftrockFAN on");
}}}
// functions to be called when an alarm triggers:
void LEDoffAlarm()
{
Serial.println("Alarm: - fade down LEDs ");
for (int fadeValue = nemLEDmax ; fadeValue >= 0; fadeValue -= 1) // fade down from value set in nemLEDmax
{
analogWrite(nemLED, fadeValue);
Serial.print("\tfadedown");
Serial.print("\tnemLEDmax =");
Serial.print(nemLEDmax);
Serial.print("\tfadeValue =");
Serial.println(fadeValue);
delay(600);
if(fadeValue<20)
{
digitalWrite(nemFAN, LOW);
Serial.println("nemFAN off");
}}
for (int fadeValue = rightrockLEDmax ; fadeValue >= 0; fadeValue -= 1) // fade down from value set in rightrockLEDmax
{
analogWrite(rightrockLED, fadeValue);
Serial.print("\tfadedown");
Serial.print("\trightrockLEDmax =");
Serial.print(rightrockLEDmax);
Serial.print("\tfadeValue =");
Serial.println(fadeValue);
delay(600);
if(fadeValue<20)
{
digitalWrite(rightrockFAN, LOW);
Serial.println("rightrockFAN off");
}}
for (int fadeValue = leftrockLEDmax ; fadeValue >= 0; fadeValue -= 1) // fade down from value set in leftrockLEDmax
{
analogWrite(leftrockLED, fadeValue);
Serial.print("\tfadedown");
Serial.print("\tleftrockLEDmax =");
Serial.print(leftrockLEDmax);
Serial.print("\tfadeValue =");
Serial.println(fadeValue);
delay(600);
if(fadeValue<20)
{
digitalWrite(leftrockFAN, LOW);
Serial.println("leftrockFAN off");
}}
for (int fadeValue = purpLEDmax ; fadeValue >= 0; fadeValue -= 1) // fade down from value set in purpLEDmax
{
analogWrite(purpLED, fadeValue);
Serial.print("\tfadedown");
Serial.print("\tpurpLEDmax =");
Serial.print(purpLEDmax);
Serial.print("\tfadeValue =");
Serial.println(fadeValue);
delay(600);
if(fadeValue<20)
{
digitalWrite(purpFAN, LOW);
Serial.println("purpFAN off");
}}
}
void digitalClockDisplay()
{
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
void printDigits(int digits)
{
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}
cattledog:
Timezone.h Available through the library manager.
Thanks, will have a play with that.
I have put the serial adjustment in a function adjustRTC(). The first line in loop calls that function. You can comment that line in or out depending if you are engaged in debugging, but it really does not matter if you always leave it active.
#include <Time.h>
//The time library may have been renamed and
//you may have to add #include <TimeLib.h> as well
#include <TimeAlarms.h>
#include <DS3232RTC.h>
#include <Wire.h>
long secondsInMs(int s) {
return s * 1000L; //values will be expressed in seconds
}
int purpLED = 9;
int purpLEDmax;
int purpPOT = A0;
int purpPOTval;
int purpFAN = 12;
int nemLED = 3;
int nemLEDmax;
int nemPOT = A1;
int nemPOTval;
int nemFAN = 4;
int leftrockLED = 6;
int leftrockLEDmax;
int leftrockPOT = A3;
int leftrockPOTval;
int leftrockFAN = 8;
int rightrockLED = 5;
int rightrockLEDmax;
int rightrockPOT = A2;
int rightrockPOTval;
int rightrockFAN = 7;
void setup() {
Serial.begin(9600);
pinMode(nemPOT, INPUT);
pinMode(nemLED, OUTPUT);
pinMode(nemFAN, OUTPUT);
pinMode(rightrockPOT, INPUT);
pinMode(rightrockLED, OUTPUT);
pinMode(rightrockFAN, OUTPUT);
pinMode(leftrockPOT, INPUT);
pinMode(leftrockLED, OUTPUT);
pinMode(leftrockFAN, OUTPUT);
pinMode(purpPOT, INPUT);
pinMode(purpLED, OUTPUT);
pinMode(purpFAN, OUTPUT);
//use the RTC to set the system time
setSyncProvider(RTC.get); // the function to get the time from the RTC
if (timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
// create the alarms
Alarm.alarmRepeat(10, 33, 0, purpLEDonAlarm); // every morning
Alarm.alarmRepeat(10, 33, 30, nemLEDonAlarm); // every morning
Alarm.alarmRepeat(10, 34, 0, rightrockLEDonAlarm); // every morning
Alarm.alarmRepeat(10, 34, 30, leftrockLEDonAlarm); // every morning
Alarm.alarmRepeat(21, 56, 0, LEDoffAlarm); // every evening
}
void loop()
{
adjustRTC(); //remove comments if adjustment required for debugging
nemPOTval = analogRead(nemPOT); //read pot, save value 0 to 1023 range
nemLEDmax = map(nemPOTval, 0, 1023, 0, 255); //convert value to 0 to 255 range
rightrockPOTval = analogRead(rightrockPOT);
rightrockLEDmax = map(rightrockPOTval, 0, 1023, 0, 255);
leftrockPOTval = analogRead(leftrockPOT);
leftrockLEDmax = map(leftrockPOTval, 0, 1023, 0, 255);
purpPOTval = analogRead(purpPOT);
purpLEDmax = map(purpPOTval, 0, 1023, 0, 255);
digitalClockDisplay();
Alarm.delay(1000); // wait one second between clock display
}
// functions to be called when an alarm triggers:
void purpLEDonAlarm()
{
Serial.println("Alarm: - fade up purpLED ");
for (int fadeValue = 0 ; fadeValue <= purpLEDmax; fadeValue += 1) // fade up to value set in purpLEDmax
{
analogWrite(purpLED, fadeValue);
Serial.print("\tpurpLEDmax =");
Serial.print("\tfadeup ");
Serial.print(purpLEDmax);
Serial.print("\tfadeValue =");
Serial.println(fadeValue);
delay(600);
if (fadeValue >= 20)
{
digitalWrite(purpFAN, HIGH);
Serial.println("purpFAN on");
}
}
}
// functions to be called when an alarm triggers:
void nemLEDonAlarm()
{
Serial.println("Alarm: - fade up nemLED ");
for (int fadeValue = 0 ; fadeValue <= nemLEDmax; fadeValue += 1) // fade up to value set in nemLEDmax
{
analogWrite(nemLED, fadeValue);
Serial.print("\tfadeup ");
Serial.print("\tnemLEDmax =");
Serial.print(nemLEDmax);
Serial.print("\tfadeValue =");
Serial.println(fadeValue);
delay(600);
if (fadeValue >= 20)
{
digitalWrite(nemFAN, HIGH);
Serial.println("nemFAN on");
}
}
}
// functions to be called when an alarm triggers:
void rightrockLEDonAlarm()
{
Serial.println("Alarm: - fade up rightrockLED ");
for (int fadeValue = 0 ; fadeValue <= rightrockLEDmax; fadeValue += 1) // fade up to value set in rightrockLEDmax
{
analogWrite(rightrockLED, fadeValue);
Serial.print("\tfadeup ");
Serial.print("\trightrockLEDmax =");
Serial.print(rightrockLEDmax);
Serial.print("\tfadeValue =");
Serial.println(fadeValue);
delay(600);
if (fadeValue >= 20)
{
digitalWrite(rightrockFAN, HIGH);
Serial.println("rightrockFAN on");
}
}
}
// functions to be called when an alarm triggers:
void leftrockLEDonAlarm()
{
Serial.println("Alarm: - fade up leftrockLED ");
for (int fadeValue = 0 ; fadeValue <= leftrockLEDmax; fadeValue += 1) // fade up to value set in leftrockLEDmax
{
analogWrite(leftrockLED, fadeValue);
Serial.print("\tfadeup ");
Serial.print("\tleftrockLEDmax =");
Serial.print(leftrockLEDmax);
Serial.print("\tfadeValue =");
Serial.println(fadeValue);
delay(600);
if (fadeValue >= 20)
{
digitalWrite(leftrockFAN, HIGH);
Serial.println("leftrockFAN on");
}
}
}
// functions to be called when an alarm triggers:
void LEDoffAlarm()
{
Serial.println("Alarm: - fade down LEDs ");
for (int fadeValue = nemLEDmax ; fadeValue >= 0; fadeValue -= 1) // fade down from value set in nemLEDmax
{
analogWrite(nemLED, fadeValue);
Serial.print("\tfadedown");
Serial.print("\tnemLEDmax =");
Serial.print(nemLEDmax);
Serial.print("\tfadeValue =");
Serial.println(fadeValue);
delay(600);
if (fadeValue < 20)
{
digitalWrite(nemFAN, LOW);
Serial.println("nemFAN off");
}
}
for (int fadeValue = rightrockLEDmax ; fadeValue >= 0; fadeValue -= 1) // fade down from value set in rightrockLEDmax
{
analogWrite(rightrockLED, fadeValue);
Serial.print("\tfadedown");
Serial.print("\trightrockLEDmax =");
Serial.print(rightrockLEDmax);
Serial.print("\tfadeValue =");
Serial.println(fadeValue);
delay(600);
if (fadeValue < 20)
{
digitalWrite(rightrockFAN, LOW);
Serial.println("rightrockFAN off");
}
}
for (int fadeValue = leftrockLEDmax ; fadeValue >= 0; fadeValue -= 1) // fade down from value set in leftrockLEDmax
{
analogWrite(leftrockLED, fadeValue);
Serial.print("\tfadedown");
Serial.print("\tleftrockLEDmax =");
Serial.print(leftrockLEDmax);
Serial.print("\tfadeValue =");
Serial.println(fadeValue);
delay(600);
if (fadeValue < 20)
{
digitalWrite(leftrockFAN, LOW);
Serial.println("leftrockFAN off");
}
}
for (int fadeValue = purpLEDmax ; fadeValue >= 0; fadeValue -= 1) // fade down from value set in purpLEDmax
{
analogWrite(purpLED, fadeValue);
Serial.print("\tfadedown");
Serial.print("\tpurpLEDmax =");
Serial.print(purpLEDmax);
Serial.print("\tfadeValue =");
Serial.println(fadeValue);
delay(600);
if (fadeValue < 20)
{
digitalWrite(purpFAN, LOW);
Serial.println("purpFAN off");
}
}
}
void digitalClockDisplay()
{
// digital clock display of the time
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 adjustRTC()
{
// check for input to set the RTC, minimum length is 12, i.e. yy,m,d,h,m,s
if (Serial.available() >= 12)
{
time_t t;
tmElements_t tm;
// note that the tmElements_t Year member is an offset from 1970,
// but the RTC wants the last two digits of the calendar year.
// use the convenience macros from the Time Library to do the conversions.
int y = Serial.parseInt();
if (y >= 100 && y < 1000)
//Serial << F("Error: Year must be two digits or four digits!") << endl;
Serial.println("Error: Year must be two digits or four digits!");
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); // use the time_t value to ensure correct weekday is set
setTime(t);
Serial.println("RTC time adjusted.");
// dump any extraneous input
while (Serial.available() > 0) Serial.read();
}
}
}
Thank you cattledog, a million times THANK YOU CATTLEDOG!!!
It works like a champ! I appreciate the heck out of that.
saltyjoe:
dave-in-nj, I need accuracy over the long haul. It's my understanding that arduino's clock can be off a couple minutes a day. I plan to add alarms for stirrers and pumps that will operate several times a day. I want to be able to manipulate time on the Serial Monitor so I can easily see if things are working before I get out the soldering iron.
Maybe I was reading your post wrong. I thought you wanted to change the time of the RTC frequently and not read the time of the RTC and then correct the time on the Arduino.
The RTC is often set as a master time keeper and you can re-set the Arduino time so that it is closer to the exact time.
The beauty of the RTC is that it is much more accurate than the Arduino that uses a resonator.
The Arduino's that use a crystal are much better, seconds a day, but still not as accurate as an RTC over the long run.
For Data Logging, I use the RTC and correct my NANO time hourly. Soil moisture drifts slow enough that checking it each hour is easy, but over a season, it is nice to know that you're only seconds off and not hours off.