Hi forum users I was wondering can i create another loop outside of void loop(void) and call that loop based on a if statement about time in the main void loop?
I have created this simple sketch, it compiles but doesn't seem to be calling the loop, is this even possible?
I'm using a a DS1307 RTC to keep time, on the sketch below I would like to run loop1 between the hrs 15 and 20 and if the time falls out of this region to run loop 2 until it is in between the hours.
Any help will be greatly appreciated and here is the basic code i've been working on:
#include <OneWire.h>
#include <RTClib.h>
#include <Wire.h>
#include <Time.h>
RTC_DS1307 RTC;
void setup(void)
{
// Start serial
Serial.begin(9600);
// Instantiate the RTC
Wire.begin();
RTC.begin();
// Check if the RTC is running.
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running");
}
// Get time from RTC
DateTime current = RTC.now();
DateTime compiled = DateTime(__DATE__, __TIME__);
if (current.unixtime() < compiled.unixtime()) {
Serial.println("RTC is older than compile time! Updating");
RTC.adjust(DateTime(__DATE__, __TIME__));
}
Serial.println("Setup Complete.");
}
void loop(void)
{
// Get the current time
DateTime now = RTC.now();
setTime(now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
Serial.print("Current time: ");
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();
delay(2000);
if (now.hour() == 15 && 20) {
void loop1();
}else{
void loop2();
}
}
void loop1()
{
Serial.println("running loop 1");
delay(2000);
}
void loop2()
{
Serial.println("running loop 2");
delay(2000);
}
if (now.hour() == 15 && now.hour() ==20) This will only be true if the hour is 15 and the hour is 20 which will be never. Do you mean if (now.hour() >= 15 && now.hour() <= 20) which will be true when the hour is between 15 and 20
void loop1();Instead of calling the loop1() function I believe that this will be seen by the compiler as a function prototype and to all intents and purposes will be ignored. Fix the limits of the if test and remove the void from this line.
A small but important point. I am sorry to have to ask this, but I take it you realise that your loop1() and loop2() functions are not loops at all but functions that will run and exit immediately. True, they will be called repeatedly by the main loop() function but they themselves will not loop. It is normal practice to give functions meaningful names indicating what they do.