#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd (3, 4, 5, 6, 7, 8);
RTC_DS1307 RTC;
const int b1 = 9; //45 minutlik
const int b2 = 10; //40 minutlik
const int relay = 11; //zvanok
int hourupg;
int minupg;
int dayupg;
int monthupg;
int yearupg;
void setup () {
Serial.begin (9600);
Serial.println ("Starting");
Wire.begin ();
RTC.begin ();
pinMode(b1, INPUT);
pinMode(b2, INPUT);
if (!RTC.isrunning()) {
Serial.println ("Don't set");
RTC.adjust (DateTime(__DATE__, __TIME__));
}
else {
Serial.println ("Set");
}
}
void SetDataTime () {
DateTime now = RTC.now();
Serial.print ("Hour: ");
if (now.hour() <= 9) {
Serial.print ("0");
}
Serial.print (now.hour(), DEC);
hourupg = now.hour();
Serial.print (":");
if (now.minute() <= 9) {
Serial.print ("0");
}
Serial.print (now.minute(), DEC);
minupg = now.minute();
Serial.print (":");
Serial.println (now.second(), DEC);
Serial.print ("Date: ");
if (now.day() <= 9) {
Serial.print ("0");
}
Serial.print (now.day(), DEC);
dayupg = now.day();
Serial.print ("/");
if (now.month() <= 9) {
Serial.print ("0");
}
Serial.print (now.month(), DEC);
monthupg = now.month();
Serial.print ("/");
Serial.println (now.year(), DEC);
yearupg = now.year();
delay (1000);
}
void Mode1 () { //Mode1
if (hourupg == 8 && minupg == 0) {
digitalWrite (relay, HIGH);
delay (3000);
digitalWrite (relay, LOW);
delay (1000);
return 3;
digitalWrite (relay, LOW);
}
}
void Mode2 () { //Mode2
if (hourupg == 17 && minupg == 40) {
digitalWrite (relay, HIGH);
delay (3000);
digitalWrite (relay, LOW);
delay (1000);
return 3;
digitalWrite (relay, LOW);
}
}
void loop () {
if (digitalRead(b1) == HIGH) {
Serial.println ("Button1 pressed");
Mode1;
}
if (digitalRead(b2) == HIGH) {
Serial.println ("Button2 pressed");
Mode2;
}
}
//this code by Asilbek Ergashov
I used 4 loops in my code, and when I run them, only the functions in the void loop work and do not switch to other loops.
For example: I first used the SetDataTime loop and wrote the code that would output it to the monitor in series. But that didn’t work. void Mode1 and Mode2 are similar.
The main function of my code is to switch to Mode1 when button 1 is pressed and to Mode2 when button 2 is pressed.
If I use a void loop function, that function works.
What I do ??? How can I switch from one loop to another ???