Calling Functions

Can anyone help with my code.I am only a beginner. So my problem is that my custom functions are not working as they should be.

My first function will be called as soon as the arduino is powered. I called the function in Loop() section. Then the second function will be called as soon as the first function is finished.But code is always staying at first function and the second function will not be called.

Appreciate your help guys.Thanks!!!

#include<Wire.h>//This library is used for I2C communication
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

const byte ROWS = 4; 
const byte COLS = 3; 
byte value = 0;
byte valuee =0;

char hexaKeys[ROWS][COLS] = {
 {'1', '2', '3'},
 {'4', '5', '6'},
 {'7', '8', '9'},
 {'*', '0', '#'}
};

byte rowPins[ROWS] = {9, 8, 7, 6}; 
byte colPins[COLS] = {5, 4, 3}; 

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup() {
 lcd.begin(20,4); 
 lcd.backlight();

 Wire.begin();
 
}

void loop() {
maxtemp();
}

void maxtemp(){
 lcd.setCursor(0,0);
 lcd.print("Enter Max Temp:");

 char key = customKeypad.getKey();
  if (key != NO_KEY)
    {
     if ( (key >= '0') && (key<= '9') )
       {
        value = value *10;
        value = value + key -'0';
        lcd.setCursor(0,1);
        lcd.print(value);
       }
     if ( key == '#' )
      {
       String strVal(value);
       int intVal = strVal.toInt();
       lcd.setCursor(4,1);
       lcd.print(intVal);
       delay(1000);
       lcd.clear();
       value = 0;
       Minn();
      } 
    }
    
}

void Minn(){
 lcd.setCursor(0,0);
 lcd.print("Enter Min Temp:");

 char keyy = customKeypad.getKey();
  if (keyy != NO_KEY)
    {
     if ( (keyy >= '0') && (keyy <= '9') )
       {
        valuee = valuee *10;
        valuee = valuee + keyy -'0';
        lcd.setCursor(0,1);
        lcd.print(valuee);
       }
     if ( keyy == '#' )
      {
       String strVall(valuee);
       int intVall = strVall.toInt();
       lcd.setCursor(12,1);
       lcd.print(intVall);
       delay(1000);
       lcd.clear();
       valuee = 0;
       maxtemp(); 
      }
    }
}

There are a couple of conditions to be satisfied in order for Minn() to be called. Are they being met?

yes it was being met.

"But code is always staying at first function and the second function will not be called."

That is because you never call the next function. Look at your loop function. Only the maxtemp() is called, then loop starts over. If you want the second function to be called you must code it that way.

Paul

You are calling the Minn() function approximately one second after pressing the # key in the maxtemp() function. If you do not press any keys during that one second interval, the code will return from the Minn() function without doing anything, then exit the maxtemp() function, and return to loop(), at which point you start over calling maxtemp() again.

Also, what exactly is this suppose to accomplish? value is a byte, which is an 8-bit integer, you don't need to convert it to a String then back to an int to go from 8-bit to 16-bit integer.

      String strVal(value);
      int intVal = strVal.toInt();
      lcd.setCursor(4, 1);
      lcd.print(intVal);

So it should look like this? Also i want to know if is possible to that the loop will rotate only at those functions? (i.e 1st function -> 2nd function-> 1st function -> ........)

My goal is to input 2 variable with a 2 digit integer that is why i created 2 functions.Any other options to do this? Thanks

void loop(){
maxtemp();
Minn();

}

Can anyone please help i badly need this .Thanks

Maybe this'll help. Maybe not.

Since you're doing one thing at startup and then a second thing and then, I'm guessing, another thing - implement a state machine* using *switch/case * statements. Call the first state getMax. Call the second state getMin. Call the third state doStuff. Set up an *enum * to hold these states. This will allow you to refer to the states by name.

In setup() initialize the state variable to getMax. This state will have all the code to input the maximum value. Once you have the max value safely tucked away change the state variable to getMin(). Do as you did for max value. When min value is ready, change state to doStuff.

  • Search these terms on this site and across the web.

Thank you bro. i’ll try this code and update you if i got it all corrected