-3
#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 ???

  • 4
    Looks like you are very confused. `SetDataTime()` is not a loop, it is a function. And others are functions, so you do not have 4 loops, you have 4 functions. Functions are executed when you or somebody else call them. Arduino hidden code calls function `loop()` (and it is still just a function), that's why it is executed all the time. Others you have to call in your code. – Slava Jan 05 '22 at 06:54
  • 2
    Does your code [compile without warnings](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings)? *Examples of warnings: "return-statement with a value, in function returning 'void'" and "statement is a reference, not call, to function 'Mode1'".* Best to fix the issues your compiler found (i.e. the warnings) before asking someone else to tell you what your compiler already told you. – JaMiT Jan 05 '22 at 06:56
  • 2
    You never call `SetDataTime`? `Mode1;` is missing the brackets to make it into a function call: `Mode1();` – Alan Birtles Jan 05 '22 at 07:09

1 Answers1

4

You are not writing loops, you are writing functions.

The way an Arduino works is there are two "special" functions, one called setup() and one called loop(). When the board starts up the boot loader calls the setup function once and then calls the loop function repeatedly. You might think this is a bit **** but its quite similar to the way a PC program executes, main() is where you would normally call all your code from.

I think what you need to do is create a new function and move the code from loop() into that new function. Then in you loop() function call the functions that you want to, int the order you want them to be called. Remember that the loop function repeats until you turn the power off, or you code crashes.

Code Gorilla
  • 962
  • 9
  • 23