Why this program takes me again to the initial function?

Well, i’m trying to prgram a pet feeder with a servomotor, rtc DS3231, two buttons, lcd 16x2 with I2c, and i’m using differents functions to use the feeder, but when i get to the last function it takes me again to the selection.

#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>

RTC_DS3231 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo servo;

const int BotonA = 6;
const int BotonB = 7;
const int Led = 53;
const int Echo = 3;
const int Trigger = 2;
const int LDR = 0;

int Boot = 0;
int welcome = 0;
int lecA = 0;
int lecB = 0;

//Horario1

int h1_s1 = 7; int m1_s1 = 0;
int h2_s1 = 13; int m2_s1 = 0;
int h3_s1 = 1; int m3_s1 = 0;

void setup() {
  
  servo.attach(8);
  pinMode(Echo, INPUT);
  pinMode(Trigger, OUTPUT);
  pinMode(Led, OUTPUT);
  pinMode(BotonA, INPUT);
  pinMode(BotonB, INPUT);

  lcd.init();
  lcd.backlight();
  lcd.setCursor(6, 0);
  lcd.print("Pets Care v1.0, Starting...");
  digitalWrite(Led, HIGH);
  Serial.begin(9600);

  if (! rtc.begin()) {
   lcd.print("No RTC Match");
   while(1); 
  }

  //rtc.adjust(DateTime(__DATE__, __TIME__));

}

void loop() {
  
  while (Boot < 20) {
    lcd.scrollDisplayLeft();
    delay(500);
    Boot++;
  }
  while (welcome < 1) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Welcome");
    delay(4000);
    welcome++;
  }
  modesel();
}

void modesel() {
  int mode = 0;
  while (mode < 1) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Mode Selection");
    lcd.setCursor(0, 1);
    lcd.print("a.Manual b.Phone");
    delay(4000);
    lecA = digitalRead(BotonA);
    lecB = digitalRead(BotonB);

    if (lecA >= 1) {
      mode++;
      manual();
    }
    if (lecB >=1) {
      mode++;
      phone();
    }
  }
}

void manual() {
  int sch = 0;
  int sure = 0;
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Manual Mode");
  delay(2000);
  
  while (sure < 1) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Continue?");
    lcd.setCursor(0, 1);
    lcd.print("A. Yes B. No");
    delay(2000);
    lecA = digitalRead(BotonA);
    lecB = digitalRead(BotonB);
    Serial.println(digitalRead(BotonB));
    Serial.println(digitalRead(BotonA));
    
    if (lecA >= 1) {
      sure++;
      scheduleselection();
    }
     if (lecB >= 1) {
      sure++;
      modesel();
     }
  }
}


void scheduleselection() {
  
    int sch = 0;
  
  while (sch < 1) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Schedule Selection");
    lcd.setCursor(0, 1);
    lcd.print("A. First B. Second");
    delay(2000);
    lecA = digitalRead(BotonA);
    lecB = digitalRead(BotonB);
    
    if (lecA >= 1) {
      sch++;
      schedule1();
    }
    if (lecB >= 1) {
      sch++;
      schedule2();    
    }
  }
}

void phone() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Phone Mode");
  delay(2000);
}

void schedule1() {
  
  int sch1 = 0;
  
  while (sch1 < 1) { 
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Schedule 1 selected");
    delay(2000);
    sch1++;
  }
  
  DateTime datetime = rtc.now();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Schedule 1");
  lcd.setCursor(0, 1);
  lcd.print("Time: ");
  lcd.print(datetime.hour());
  lcd.print(":"); 
  lcd.print(datetime.minute());
  lcd.print(":");
  lcd.print(datetime.second());
  delay(500);
  
  int hora =  datetime.hour();
  int minuto = datetime.minute();
  
  if (hora == h1_s1 && minuto == m1_s1) {feed();}
  if (hora == h2_s1 && minuto == m2_s1) {feed();}
  if (hora == h3_s1 && minuto == m3_s1) {feed();}
}
void schedule2() {
}

void feed() {
  int feeed = 0;
  int pos = 0;
  while (feeed < 4) {
    servo.write(pos);
    delay(1000);
    feeed++;
    pos + 180;
  }
}

as you can see i have differents functions in my program, the problem is that when i get to the function schedule 1 it takes me again to modesel() function.

thanks for the help and sorry for my english.

All you functions call the next function at some point (conditionally), by the time it gets to the end of schedule1() and the function end, you return to the previous functions 1 by 1, until you return to loop() which then calls modesel() again on the next pass. This looks like an incorrect implementation of a state machine.

Deva_Rishi:
All you functions call the next function at some point (conditionally), by the time it gets to the end of schedule1()
and the function end, you return to the previous functions 1 by 1, until you return to loop() which then calls modesel() again on the next pass. This looks like an incorrect implementation of a state machine.

So the schedule1() function it’s ending, to fix the problem can I put the schedule 1() content in a While? Like:

void schedule1() {
  
  int sch1 = 0;
  Int schstay = 0;

while (schstay < 1) {
  
  while (sch1 < 1) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Schedule 1 selected");
    delay(2000);
    sch1++;
  }
  
  DateTime datetime = rtc.now();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Schedule 1");
  lcd.setCursor(0, 1);
  lcd.print("Time: ");
  lcd.print(datetime.hour());
  lcd.print(":");
  lcd.print(datetime.minute());
  lcd.print(":");
  lcd.print(datetime.second());
  delay(500);
  
  int hora =  datetime.hour();
  int minuto = datetime.minute();
  
  if (hora == h1_s1 && minuto == m1_s1) {feed(); schstay++;}
  if (hora == h2_s1 && minuto == m2_s1) {feed(); schstay++;}
  if (hora == h3_s1 && minuto == m3_s1) {feed(); schstay++;}
}
}

You could do, but like that it is 'never ending' What you should do is set it up in such a way that the 'state' of the program takes care of calling the proper function 'over and over' So to exit you schedule1() you can press a button and it will return you to your menu.

1 Like