Problem with subprogram

Hi my friend, I want to use subprogram in my project but I have a problem. I post two photos under of it:

Moderator edit: Post your CODE here, not photos

I have tried in two different ways, but I have two different error.

What is the corrent way to write it?

Looks like you're trying to post photos as code. You need to post a link to your pictures or add them as attachments. NOT code block.

Hi my friend, I want to use subprogram in my project but I have a problem. I post two photos under of it:

double tmax = 0;
double tmin = 0;

void setup(){
  Serial.begin(9600);
}

void loop(){

 tmax = set_temp(double temp_max); 
 tmin = set_temp(double temp_min);
 
}

//Imposto la soglia di temperatura massima
double set_temp(double temp_max, double temp_min){
   temp_max = 25.2;
   temp_min = 17.3;
   
   return temp_max;
   return temp_min;
}

And this is my error:

Or:

double tmax = 0;
double tmin = 0;
double temp_max;
double temp_min;

void setup(){
  Serial.begin(9600);
}

void loop(){

 tmax = set_temp(temp_max); 
 tmin = set_temp(temp_min);
 
}

//Imposto la soglia di temperatura massima
double set_temp(double temp_max, double temp_min){
   temp_max = 25.2;
   temp_min = 17.3;
   
   return temp_max;
   return temp_min;
}

and this is the error:

I tried to declare the subprogram as I do in C++, in this way:

double tmax = 0;
double tmin = 0;
double set_temp(double temp_max, double temp_min);

void setup(){
  Serial.begin(9600);
}

void loop(){
 double temp_max;
double temp_min;

 tmax = set_temp(temp_max); 
 tmin = set_temp(temp_min);
 
}

//Imposto la soglia di temperatura massima
double set_temp(double temp_max, double temp_min){
   temp_max = 25.2;
   temp_min = 17.3;
   
   return temp_max;
   return temp_min;
}

But I have the second error too.

What is the corrent way to write it?

Thank you.

The error message gives a HUGE clue.

double set_temp(double temp_max, double temp_min);

vs.

tmax = set_temp(temp_max);

Please, don't post screenshots.

You have a function defined as double set_temp(double temp_max, double temp_min) which is expecting TWO parameters to be passed to it.

Within your loop function you attempt to call it with just ONE parameter and even that one is not initialised.

Also, this:

 return temp_max;
   return temp_min;

You can't return two items from a single function like that.
The function returns on the first "return" it hits.

AWOL:
You can't return two items from a single function like that.

It's not a single function. It's a double :wink:

It's not a single function. It's a double

I have changed my code:

typedef struct Temp{
 double tmax;
 double tmin;
} t_Temp;

t_Temp set_temp(Temp);

void setup() {
 Serial.begin(9600);
}

void loop() {
  double tM;
  double tm;
  t_Temp result;
  
  tM = set_temp(result.tmax);
  tm = set_temp(result.tmin);

}

t_Temp set_temp(){
   Temp_ result;
   result.tmax = 25.2;
   result.tmin = 17.3;
   
   return result;
}

But now I have another error:

't_Temp does not name a type

Why?

I suspect we are being played with here.

The code you post has many issues. You're now creating a function that expects NO parameters yet you try to pass one to it in both calls.

You also declare it with a return type, yet it never returns anything, so a void would be just fine.

You have a mess like this t_Temp set_temp(Temp); (where Temp is a datatype)

YET the only error you tell us about is the one that is caused by a known issue with the compiler.

OK Game over, I'm not playing any more.

How can I correct this code?

Why don't you tell us what you want to do, not how you think it ought to be done?

typedef struct t_Temp{
  double tmax;
  double tmin;
} Temp;

Temp set_temp();

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  Temp result = set_temp();

  double tM = result.tmax;
  double tm = result.tmin;
}

Temp set_temp()
{
  Temp result;
  result.tmax = 25.2;
  result.tmin = 17.3;

  return result;
}

Ok now it work.
But I have another problem :-[

switch(posizione){
    case 0:
      if(flag == true){
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(menu_principale[0]);
        //flag = false;
        }
      lcd.setCursor(0,1);
      //Visualizzo i valori letti dal sensore DHT22
      lcd.print("T1: ");
      lcd.print(myDHT22.getTemperatureC(),1);
      lcd.print("C  ");
      lcd.print("H1: ");
      lcd.print(myDHT22.getHumidity(),1);
      lcd.print("%");
      lcd.setCursor(0,2);
      //Visualizzo i valori letti dal sensore DHT11
      lcd.print("T2: ");
      lcd.print(DHT.temperature,0);
      lcd.print("C  ");
      lcd.print("H1: ");
      lcd.print(DHT.humidity,0);
      lcd.print("%");
      lcd.setCursor(0,3);
      lcd.print("        Destra");
      lcd.write(byte(0));
      lcd.print("Menu'");
      delay(300);
      break;
      
    case 1:
      if(flag == true){
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(menu_principale[1]);       
            lcd.setCursor(1,1);
            lcd.print(submenut[0]);
            lcd.setCursor(1,2);
            lcd.print(submenut[1]);

            Envir result = setting();
          //Imposto le soglie di temperature
          tmax = result.tmax;
          tmin = result.tmin;
          
          //Controllo soglie temperatura
          if(tmin > tmax){
            tmin = tmax - 4;
            lcd.setCursor(0,1);
            lcd.print(tmin);
          } 
      }
     
      
      delay(200);
      break;

In the case 1, I've written

Envir result = setting();

Envir setting(){

  int i = 0;
  boolean message = false;
  int tpush_button;
  int pos = 4;
  Envir result;
  
  if(lcd_key == btnSELECT){
     i = 2;
     delay(100);
     }

  while(i == 2){
  
  if(lcd_key == btnDOWN){
    lcd.setCursor(0,2);
    lcd.print(" ");
    lcd.setCursor(0,1);
    lcd.write(byte(0));
    pos++;
    } else if(lcd_key == btnUP){
             lcd.setCursor(0,1);
             lcd.print(" ");
             lcd.setCursor(0,2);
             lcd.write(byte(0));
             pos--;
             }
             
  if(pos < 4)
    pos = 5;
  if(pos > 5)
    pos = 4;
  
  switch(pos){
    case 4:
      if(lcd_key == btnSELECT){
        message = true;
        tpush_button = read_LCD_buttons();
          if( message == true ){
            lcd.print(":");
            lcd.setCursor(7,1);
            lcd.print(result.tmax);
            lcd.print("C");
            message = false;
          }
          if(tpush_button == btnUP || tpush_button == btnDOWN || tpush_button == btnSELECT){
            if(tpush_button == btnUP){
              result.tmax = result.tmax + 0.1;
              lcd.setCursor(7,1);
              lcd.print(result.tmax);
              lcd.print("C");    
            }
            if(tpush_button == btnDOWN){
              result.tmax = result.tmax - 0.1;
              lcd.setCursor(7,1);
              lcd.print(result.tmax);    
              lcd.print("C");
            }
            if(tpush_button == btnSELECT){
              pos = 1;
              //message = true;
              delay(200);
            } 
         }  
       delay( 100 );
    }
  case 5:
    if(lcd_key == btnSELECT){
      tpush_button = read_LCD_buttons();
        if( message == true ){
          lcd.print(":");
          lcd.setCursor(7,2);
          lcd.print(result.tmin);
          lcd.print("C");
          message = false;
        }
        if(tpush_button == btnUP || tpush_button == btnDOWN || tpush_button == btnSELECT){
          if(tpush_button == btnUP){
            result.tmin = result.tmin + 0.1;
            lcd.setCursor(7,2);
            lcd.print(result.tmin);
            lcd.print("C");    
          }
          if(tpush_button == btnDOWN){
            result.tmin = result.tmin - 0.1;
            lcd.setCursor(7,2);
            lcd.print(result.tmin);    
            lcd.print("C");
          }
          if(tpush_button == btnSELECT){
            message = false;
            delay(200);
          } 
       }  
     i = 1;
     delay( 100 );
  } 
 } 
}
}
return result;
}

But the first "IF"

if(lcd_key == btnSELECT)

in this subroutine doesn't work.
For test I've written in this way:

  if(lcd_key == btnSELECT){
     i = 2;
     Serial.print(i);
     delay(100);
     }

But the program doesn't print "i", because it doesn't entry in that "IF". Why?

Try here

:roll_eyes:

I post all my code:

#include <dht.h>
#include <EEPROM.h>
#include <DHT22.h>
#include <LiquidCrystal.h>

//Definisco tutti i PIN
#define UMIDIFICATORE  A4
#define RISCALDATORE   A5

#define DHT22_PIN      13
#define DHT11_PIN      12
#define RETROILL       10

#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5

//Definisco l'istanza DHT22
DHT22 myDHT22(DHT22_PIN);

//Definisco l'istanza DHT11
dht DHT;

//Inizializzo il display Hitachi HDD7780 (16x2)
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);  

//Creo il menù e il submenù
char* menu_principale[4] = { 
  "Home","Temperatura","Umidita'ambiente","Configurazione" };
char* submenut[2] = { 
  "Tmax","Tmin" };
char* submenuh[2] = { 
  "Hmax","Hmin" };
  
//Definisco la struttura per temperatura e umidità
typedef struct t_Envir{
  double tmax;
  double tmin;
  int hmax;
  int hmin; 
} Envir;

Envir setting();

//Carattere speciale "ARROW"
byte arrow[8] = {0,4,6,31,6,4,0,0};

//Variabili
float temp = 0;
int posizione = 0;
double tmax = 0;
double tmin = 0;
int hmax = 0; 
int hmin = 0;   
//Variabili gestione pulsanti
int lcd_key     = 0;
int adc_key_in  = 0;
//Variabili che uso per lo standby della retroilluminazione del display
int time = 0;
int brightness = 255;

void setup(){

  Serial.begin(9600);
  //Inizializzazione lcd
  lcd.begin(20,4);
  //Inizializzazione PIN (INPUT - OUTPUT)
  pinMode(RISCALDATORE, OUTPUT);
  digitalWrite(RISCALDATORE, LOW);
  pinMode(UMIDIFICATORE, OUTPUT);
  digitalWrite(UMIDIFICATORE, LOW);
  pinMode(RETROILL, OUTPUT);
  analogWrite(RETROILL, brightness);
  //Pulisco e setto il display
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("LOADING...");
  delay(2000); 
  //Creo il carattere
  lcd.createChar(0, arrow);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Home");
}

void loop(){
  
  DHT22_ERROR_t errorCode;
  boolean flag = false;
  boolean fly = false;
  lcd_key = read_LCD_buttons();
  int m = 0;
 
  errorCode = myDHT22.readData();
  
  //Inizializzo il sensore DHT11
  int chk = DHT.read11(DHT11_PIN);

  //Gestisco il sensore di umidità e temperatura DHT11
  switch (chk)
  {
  case DHTLIB_OK:  
    //Serial.print("OK,\t"); 
    break;
  case DHTLIB_ERROR_CHECKSUM: 
    //Serial.print("Checksum error,\t"); 
    break;
  case DHTLIB_ERROR_TIMEOUT: 
    //Serial.print("Time out error,\t"); 
    break;
  default: 
    //Serial.print("Unknown error,\t"); 
    break;
  }
  
  int a;
  int b;
  
  //Gestisco il sensore di umidità e temperatura DHT22
  switch(errorCode)
  {
    case DHT_ERROR_NONE:
      //Serial.print("Got Data ");
      //Serial.print(myDHT22.getTemperatureC());
      //Serial.print("C ");
      //Serial.print(myDHT22.getHumidity());
      //Serial.println("%");
      // Alternately, with integer formatting which is clumsier but more compact to store and
  // can be compared reliably for equality:
  //  
      char buf[128];
      sprintf(buf, "Integer-only reading: Temperature %hi.%01hi C, Humidity %i.%01i %% RH",
                   myDHT22.getTemperatureCInt()/10, abs(myDHT22.getTemperatureCInt()%10),
                   myDHT22.getHumidityInt()/10, myDHT22.getHumidityInt()%10);
      //Serial.println(buf);
      break;
    case DHT_ERROR_CHECKSUM:
      //Serial.print("check sum error ");
      //Serial.print(myDHT22.getTemperatureC());
      //Serial.print("C ");
      //Serial.print(myDHT22.getHumidity());
      //Serial.println("%");
      break;
    case DHT_BUS_HUNG:
      //Serial.println("BUS Hung ");
      break;
    case DHT_ERROR_NOT_PRESENT:
      //Serial.println("Not Present ");
      break;
    case DHT_ERROR_ACK_TOO_LONG:
      //Serial.println("ACK time out ");
      break;
    case DHT_ERROR_SYNC_TIMEOUT:
      //Serial.println("Sync Timeout ");
      break;
    case DHT_ERROR_DATA_TIMEOUT:
      //Serial.println("Data Timeout ");
      break;
    case DHT_ERROR_TOOQUICK:
      //Serial.println("Polled to quick ");
      break;
  }
  
  //Standby
  time++;
  if(time == 300){
    while(brightness != 0){
      brightness = brightness - 1;
      analogWrite(RETROILL, brightness);
      delay(10);
      }
    }
    
  if(lcd_key != btnNONE && brightness == 0){ 
    brightness = 255;
    if(digitalRead(RETROILL) == 0){
      if(lcd_key == btnRIGHT){       
        analogWrite(RETROILL, brightness); 
        posizione--; 
      }
    else if(lcd_key != btnNONE){
           analogWrite(RETROILL, brightness);  
         }
    time = 0; 
    }
  }

  //Menù
  if(lcd_key == btnRIGHT){
    posizione++;
    flag = true;
  } else if(lcd_key == btnLEFT){
           posizione--;
           flag = true;
           }

    //Inizializzo le sogliee con i valori salvati nella EEPROM
    tmax = EEPROM_readDouble(0);
    tmin = EEPROM_readDouble(4);
    //Inizializzo le sogliee con i valori salvati nella EEPROM
    hmax = EEPROM_readInt(8);
    hmin = EEPROM_readInt(10);
    
    if ( posizione > 3 ) 
      posizione = 0;
    if ( posizione < 0 )
      posizione = 0; 
    
    switch(posizione){
    case 0:
      if(flag == true){
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(menu_principale[0]);
        //flag = false;
        }
      lcd.setCursor(0,1);
      //Visualizzo i valori letti dal sensore DHT22
      lcd.print("T1: ");
      lcd.print(myDHT22.getTemperatureC(),1);
      lcd.print("C  ");
      lcd.print("H1: ");
      lcd.print(myDHT22.getHumidity(),1);
      lcd.print("%");
      lcd.setCursor(0,2);
      //Visualizzo i valori letti dal sensore DHT11
      lcd.print("T2: ");
      lcd.print(DHT.temperature,0);
      lcd.print("C  ");
      lcd.print("H1: ");
      lcd.print(DHT.humidity,0);
      lcd.print("%");
      lcd.setCursor(0,3);
      lcd.print("        Destra");
      lcd.write(byte(0));
      lcd.print("Menu'");
      delay(300);
      break;
      
    case 1:
      if(flag == true){
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(menu_principale[1]);       
            lcd.setCursor(1,1);
            lcd.print(submenut[0]);
            lcd.setCursor(1,2);
            lcd.print(submenut[1]);
            Envir result = setting();
          //Imposto le soglie di temperature
          tmax = result.tmax;
          tmin = result.tmin;
          
          //Controllo soglie temperatura
          if(tmin > tmax){
            tmin = tmax - 4;
            lcd.setCursor(0,1);
            lcd.print(tmin);
          } 
      }
      
      //Salvo le soglie nella EEPROM
      EEPROM_writeDouble(0, tmax);
      EEPROM_writeDouble(4, tmin);
      
      time = 0;
      
      delay(200);
      break;
      
    case 2:
      if(flag == true){
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(menu_principale[2]);
        //Imposte le soglie di umidità
        hmax = set_hum_max();
        hmin = set_hum_min();
        //Serial.println(menu_principale[2]);
        // flag = false;
        //Controllo soglie umidità
        if(hmin > hmax){
          hmin = hmax - 10;
          lcd.setCursor(0,1);
          lcd.print(hmin);
        }
      }
      
      //Salvo le soglie nella EEPROM
      EEPROM_writeInt(8, hmax);
      EEPROM_writeInt(10, hmin);
      time = 0;
      delay(200);
      break;
    
    case 3:     
    
      if(flag == true){
        lcd.clear();
        //Visualizzo le tutte le soglie impostate
        lcd.setCursor(0,0);
        //lcd.print(menu_principale[3]);
        lcd.print("H:");
        lcd.print(hmax);
        lcd.print("-");
        lcd.print(hmin);
        lcd.print("% ");
        lcd.setCursor(0,1);
        lcd.print("T:");
        lcd.print(tmax);
        lcd.print("C ");
        lcd.print(tmin);
        lcd.print("C "); 
        //flag = false;
        }
      time = 0;
      delay(200);   
      break;
    }
}
//Imposto la soglia di temperatura massima
Envir setting(){

  int i = 0;
  boolean message = false;
  int tpush_button;
  int pos = 4;
  Envir result;
  
  if(lcd_key == btnSELECT){
     i = 2;
     delay(100);
     }

  while(i == 2){
  
  if(lcd_key == btnDOWN){
    lcd.setCursor(0,2);
    lcd.print(" ");
    lcd.setCursor(0,1);
    lcd.write(byte(0));
    pos++;
    } else if(lcd_key == btnUP){
             lcd.setCursor(0,1);
             lcd.print(" ");
             lcd.setCursor(0,2);
             lcd.write(byte(0));
             pos--;
             }
             
  if(pos < 4)
    pos = 5;
  if(pos > 5)
    pos = 4;
  
  switch(pos){
    case 4:
      if(lcd_key == btnSELECT){
        message = true;
        tpush_button = read_LCD_buttons();
          if( message == true ){
            lcd.print(":");
            lcd.setCursor(7,1);
            lcd.print(result.tmax);
            lcd.print("C");
            message = false;
          }
          if(tpush_button == btnUP || tpush_button == btnDOWN || tpush_button == btnSELECT){
            if(tpush_button == btnUP){
              result.tmax = result.tmax + 0.1;
              lcd.setCursor(7,1);
              lcd.print(result.tmax);
              lcd.print("C");    
            }
            if(tpush_button == btnDOWN){
              result.tmax = result.tmax - 0.1;
              lcd.setCursor(7,1);
              lcd.print(result.tmax);    
              lcd.print("C");
            }
            if(tpush_button == btnSELECT){
              pos = 1;
              //message = true;
              delay(200);
            } 
         }  
       delay( 100 );
    }
  case 5:
    if(lcd_key == btnSELECT){
      tpush_button = read_LCD_buttons();
        if( message == true ){
          lcd.print(":");
          lcd.setCursor(7,2);
          lcd.print(result.tmin);
          lcd.print("C");
          message = false;
        }
        if(tpush_button == btnUP || tpush_button == btnDOWN || tpush_button == btnSELECT){
          if(tpush_button == btnUP){
            result.tmin = result.tmin + 0.1;
            lcd.setCursor(7,2);
            lcd.print(result.tmin);
            lcd.print("C");    
          }
          if(tpush_button == btnDOWN){
            result.tmin = result.tmin - 0.1;
            lcd.setCursor(7,2);
            lcd.print(result.tmin);    
            lcd.print("C");
          }
          if(tpush_button == btnSELECT){
            message = false;
            delay(200);
          } 
       }  
     i = 1;
     delay( 100 );
  } 
 } 
}
}
return result;
}

//Imposto la soglia di umidità massima
int set_hum_max(){

  int j = 0;
  boolean message = true;
  int hpush_button;
  
  while(j == 0){
    hpush_button = read_LCD_buttons();

    if( message ){
      lcd.setCursor(0,1);
      lcd.print(submenuh[j]);
      lcd.print(":");
      lcd.setCursor(6,1);
      lcd.print(hmax);    
      lcd.print("%");
      message = false;
    }
    if(hpush_button == btnUP || hpush_button == btnDOWN || hpush_button == btnSELECT){
      if(hpush_button == btnUP){
        if(j == 0){
          hmax = hmax + 1;
          lcd.setCursor(6,1);
          lcd.print(hmax);    
          lcd.print("%");
        }
      }
      if(hpush_button == btnDOWN){
        if(j == 0){
          hmax = hmax - 1;
          lcd.setCursor(6,1);
          lcd.print(hmax);
          lcd.print("%");    
        }
      }
      if(hpush_button == btnSELECT){
        j = j + 1;
        message = true;
        delay(200);
      } 
    }
    delay( 100 );
  }  
  return hmax;
}

//Imposto la soglia di umidità minima
int set_hum_min(){

  int j = 1;
  boolean message = true;
  int hpush_button;
  
  while(j == 1){
    hpush_button = read_LCD_buttons();

    if( message ){
      lcd.setCursor(0,1);
      lcd.print(submenuh[j]);
      lcd.print(":");
      lcd.setCursor(6,1);
      lcd.print(hmin);
      lcd.print("%");
      message = false;
    }
    if(hpush_button == btnUP || hpush_button == btnDOWN || hpush_button == btnSELECT){
      if(hpush_button == btnUP){
        if(j == 1){
          hmin = hmin + 1;
          lcd.setCursor(6,1);
          lcd.print(hmin);
          lcd.print("%");    
        }
      }
      if(hpush_button == btnDOWN){
        if(j == 1){
          hmin = hmin - 1;
          lcd.setCursor(6,1);
          lcd.print(hmin);
          lcd.print("%");    
        }
      }
      if(hpush_button == btnSELECT){
        j = j + 1;
        message = true;
        delay(200);
      } 
    }
    delay( 100 );
  }  
  return hmin;
}

I repeat, first IF in the

Envir setting(){...}

doesn't work.

rossotony88:
I repeat, first IF in the doesn't work.

You have no setup() function
You have no loop() function
you haven't defined what Envir means.
you haven't defined what lcd_key is
you haven't defined what btnSELECT is

TOOOOO much code missing

I have splitted a program in two answers because it is a bit long. So, you can read all the program, the other middle is in the previous page :wink:

Do you have any advice?

rossotony88:
Do you have any advice?

Post all your code in one place.
If it is too big to post, attach it.