calling multiple functions

Hello

I am a noob at programming, but I have read the rules of the forum and I will do my best to help you help me :frowning:

This is my first own code. I am programming a vivarium temperature/humidity controller. The light for the vivarium is on a standalone dayligt/season simulator. So the leangth of the day is shanging thrue the year.

The project is about keeping different environmental situations for day/night. Day night is defined by a photo sensor. Basically when it is light, use day mode and when it is dark, use night mode.

I have tested the day/night definition by itself and the two functions day mode and night mode by themselves and It all works fine. The problem occures in compiling when I try to get them together in one sketch that calls the day/night functions. Compiling works fine until where the day mode functions starts. The error is error: a function-definition is not allowed here before ‘{’ token

I am sure I have made a very simple misstake, but I have spent a few hour trying to find out what and I cannot find it. It is obvious that I have one or more curly brackets placed wrong, but just cant figure out where. So I think I am ready for asking for help :).

I am not sure the program is even doing what I think it will, but for now I just need the compiling to work so that I can test that.

OK, this is the code:

 #include "Adafruit_AM2320.h"
Adafruit_AM2320 am2320 = Adafruit_AM2320();
#define temLowTriggerDay 24  //Setting the trigger value for the temperture at day
#define humLowTriggerDay 45 //Setting the trigger value for the humidity at day
#define temLowTriggerNight 22  //Setting the trigger value for the temperture at night
#define humLowTriggerNight 50 //Setting the trigger value for the humidity at night
const int relay1 =  10;      // the number of the relay 1 pin
const int relay2 =  11;      // the number of the relay 2 pin


int DA=A0;  // Pin for Analog Output - AO
int threshold = 200; //setting the trigger for day/night
int sensorvalue=0; //
int nattKorning();
int dagsKorning();

void setup() {
  // put your setup code here, to run once:

   pinMode(relay1, OUTPUT); // sets output for relay 1
  pinMode(relay2, OUTPUT); //sets output for relay 2
  Serial.begin(9600); //Starts the serial monitor for test scenario
  while (!Serial) 
  
  {
    delay(10); // hang out until serial port opens

    Serial.println("Välkommen till temperatur/dagsljus styrnings test");
  am2320.begin();
  
}
}
    

void loop() {
  // put your main code here, to run repeatedly:

  sensorvalue = analogRead(DA);  //Read the analog value
  Serial.print("Analog: ");  
  Serial.println(sensorvalue);  //Print the analog value

  Serial.print("Temp: "); Serial.println(am2320.readTemperature());
  Serial.print("Hum: "); Serial.println(am2320.readHumidity());


  if(sensorvalue >= threshold){  //Compare analog value with threshold 
   nattKorning();//call function night mode
  }  
  else{
  dagsKorning(); //call function day mode
  }
  
  void nattKorning(){//Function for night mode

  if(am2320.readHumidity() < humLowTriggerNight) //if the humidity lower than the trigger value
      digitalWrite(relay1, HIGH); //start humidification
    else
      digitalWrite(relay1, LOW); //stop humidification

      if(am2320.readTemperature() < temLowTriggerNight)
      digitalWrite(relay2, HIGH);//start heating
    else
      digitalWrite(relay2, LOW);//stop heating
      }
      
      
      

void dagsKorning(){ //function for day mode......This is the place I get an error in compiling.

  if(am2320.readHumidity() < humLowTriggerDay) //if the humidity lower than the trigger value
      digitalWrite(relay1, HIGH); //start humidification
    else
      digitalWrite(relay1, LOW);//stop humification

      if(am2320.readTemperature() < temLowTriggerDay)
      digitalWrite(relay2, HIGH);//start heating
    else
      digitalWrite(relay2, LOW); //stop heating
      }
      

      delay 3000; //take a pause to avoid flickering relays


  

}

You have the function definitions dagsKorning() and nattKorning() within the body of the loop() function definition. That is not allowed. Easiest solution is to move the

      delay 3000; //take a pause to avoid flickering relays


  

}

at the end of your code to just above the nattKorning() function definition so that it terminates the loop() function definition.

You have defined both 'nattKorning()' and 'dagsKorning()' inside of function 'loop()'. C/C++ does not allow defining one function inside of another one.

stowite:
You have the function definitions dagsKorning() and nattKorning() within the body of the loop() function definition. That is not allowed. Easiest solution is to move the

      delay 3000; //take a pause to avoid flickering relays

}




at the end of your code to just above the nattKorning() function definition so that it terminates the loop() function definition.

Thanks a lot. I didnt realice that it was inside the loop function.

I did the shange suggested. But now the compiling ends in an error right in the beggining: ambiguating new declaration of ‘int nattKorning()’

How come? I havent changed any deffinition:

#include "Adafruit_AM2320.h"
Adafruit_AM2320 am2320 = Adafruit_AM2320();
#define temLowTriggerDay 24  //Setting the trigger value for the temperture at day
#define humLowTriggerDay 45 //Setting the trigger value for the humidity at day
#define temLowTriggerNight 22  //Setting the trigger value for the temperture at night
#define humLowTriggerNight 50 //Setting the trigger value for the humidity at night
const int relay1 =  10;      // the number of the relay 1 pin
const int relay2 =  11;      // the number of the relay 2 pin


int DA=A0;  // Pin for Analog Output - AO
int threshold = 200; //setting the trigger for day/night
int sensorvalue=0; //
int nattKorning();//here is the error now: ambiguating new declaration of 'int nattKorning()'
int dagsKorning();

void setup() {
  // put your setup code here, to run once:

   pinMode(relay1, OUTPUT); // sets output for relay 1
  pinMode(relay2, OUTPUT); //sets output for relay 2
  Serial.begin(9600); //Starts the serial monitor for test scenario
  while (!Serial) 
  
  {
    delay(10); // hang out until serial port opens

    Serial.println("Välkommen till temperatur/dagsljus styrnings test");
  am2320.begin();
  
}
}
    

void loop() {
  // put your main code here, to run repeatedly:

  sensorvalue = analogRead(DA);  //Read the analog value
  Serial.print("Analog: ");  
  Serial.println(sensorvalue);  //Print the analog value

  Serial.print("Temp: "); Serial.println(am2320.readTemperature());
  Serial.print("Hum: "); Serial.println(am2320.readHumidity());


  if(sensorvalue >= threshold){  //Compare analog value with threshold 
   nattKorning();//call function night mode
  }  
  else{
  dagsKorning(); //call function day mode
  }
  
  delay 3000; //take a pause to avoid flickering relays
  }
  
  void nattKorning(){//Function for night mode

  if(am2320.readHumidity() < humLowTriggerNight) //if the humidity lower than the trigger value
      digitalWrite(relay1, HIGH); //start humidification
    else
      digitalWrite(relay1, LOW); //stop humidification

      if(am2320.readTemperature() < temLowTriggerNight)
      digitalWrite(relay2, HIGH);//start heating
    else
      digitalWrite(relay2, LOW);//stop heating
      }
      
      
      

void dagsKorning(){ //function for day mode

  if(am2320.readHumidity() < humLowTriggerDay) //if the humidity lower than the trigger value
      digitalWrite(relay1, HIGH); //start humidification
    else
      digitalWrite(relay1, LOW);//stop humification

      if(am2320.readTemperature() < temLowTriggerDay)
      digitalWrite(relay2, HIGH);//start heating
    else
      digitalWrite(relay2, LOW); //stop heating
      }

You cannot name a variable and a function with the same name. Remove these 2 lines

int nattKorning();//here is the error now: ambiguating new declaration of 'int nattKorning()'
int dagsKorning();

Thanks a lot for your support. That one was something I picked up online (shouldnt do that).

This code is still confusing me. After creating all the separate codes and it worked fine now I get a lot of issues that I have never had before. It is obvious that I need to spend a lot more time on the basics. But for now I really need this code to work. All the hardware is allready built and the animals will arrive in a week.

Soo. Next issue is in the delay. “espected ;before numeric constant”. I dont understand. Never used ; within the delay command and adding it does not solve it.

 #include "Adafruit_AM2320.h"
Adafruit_AM2320 am2320 = Adafruit_AM2320();
#define temLowTriggerDay 24  //Setting the trigger value for the temperture at day
#define humLowTriggerDay 45 //Setting the trigger value for the humidity at day
#define temLowTriggerNight 22  //Setting the trigger value for the temperture at night
#define humLowTriggerNight 50 //Setting the trigger value for the humidity at night
const int relay1 =  10;      // the number of the relay 1 pin
const int relay2 =  11;      // the number of the relay 2 pin


int DA=A0;  // Pin for Analog Output - AO
int threshold = 200; //setting the trigger for day/night
int sensorvalue=0; //


void setup() {
  // put your setup code here, to run once:

   pinMode(relay1, OUTPUT); // sets output for relay 1
  pinMode(relay2, OUTPUT); //sets output for relay 2
  Serial.begin(9600); //Starts the serial monitor for test scenario
  while (!Serial) 
  
  {
    delay(10); // hang out until serial port opens

    Serial.println("Välkommen till temperatur/dagsljus styrnings test");
  am2320.begin();
  
}
}
    

void loop() {
  // put your main code here, to run repeatedly:

  sensorvalue = analogRead(DA);  //Read the analog value
  Serial.print("Analog: ");  
  Serial.println(sensorvalue);  //Print the analog value

  Serial.print("Temp: "); Serial.println(am2320.readTemperature());
  Serial.print("Hum: "); Serial.println(am2320.readHumidity());


  if(sensorvalue >= threshold){  //Compare analog value with threshold 
   nattKorning();//call function night mode
  }  
  else{
  dagsKorning(); //call function day mode
  }
  
  delay 3000; //take a pause to avoid flickering relays. Error "expected ; before numeric constant ??"
  }
  
  void nattKorning(){//Function for night mode

  if(am2320.readHumidity() < humLowTriggerNight) //if the humidity lower than the trigger value
      digitalWrite(relay1, HIGH); //start humidification
    else
      digitalWrite(relay1, LOW); //stop humidification

      if(am2320.readTemperature() < temLowTriggerNight)
      digitalWrite(relay2, HIGH);//start heating
    else
      digitalWrite(relay2, LOW);//stop heating
      }
      
      
      

void dagsKorning(){ //function for day mode

  if(am2320.readHumidity() < humLowTriggerDay) //if the humidity lower than the trigger value
      digitalWrite(relay1, HIGH); //start humidification
    else
      digitalWrite(relay1, LOW);//stop humification

      if(am2320.readTemperature() < temLowTriggerDay)
      digitalWrite(relay2, HIGH);//start heating
    else
      digitalWrite(relay2, LOW); //stop heating
      }

Sorry for spamming you with such easy questions. I found it myself. Embarrasing :(. It is getting late here, so I am thinking slow.

Thank you all for the support. Now it works. Will test if it does what I want tomorow.

BR Magnus

It would be a big help if you would post the entire error message instead of paraphrasing.

delay 3000;

delay() is a function. A function requires parameters to be in parenthesis.

delay {3000);

I didnt realice that it was inside the loop function.

It would have been fairly obvious if you had Auto formatted the code in the IDE.

tropfrog: Thanks a lot. I didnt realice that it was inside the loop function.

Your first hint should have been the error messages:

/Users/john/Documents/Arduino/sketch_nov29a/sketch_nov29a.ino: In function 'void loop()':
sketch_nov29a:58:3: error: a function-definition is not allowed here before '{' token
   {
   ^
sketch_nov29a:75:3: error: a function-definition is not allowed here before '{' token
   {
   ^

groundFungus: It would be a big help if you would post the entire error message instead of paraphrasing.

delay 3000;

delay() is a function. A function requires parameters to be in parenthesis.

delay {3000);

yea, that was the one I should never even asked here. But I did find it myself direct after posting. Thanks anyway.

UKHeliBob: It would have been fairly obvious if you had Auto formatted the code in the IDE.

Thanks, I didnt even knew about auto formatting yet. I will remember that for next time.

johnwasser: Your first hint should have been the error messages:

/Users/john/Documents/Arduino/sketch_nov29a/sketch_nov29a.ino: In function 'void loop()':
sketch_nov29a:58:3: error: a function-definition is not allowed here before '{' token
   {
   ^
sketch_nov29a:75:3: error: a function-definition is not allowed here before '{' token
   {
   ^

Despite I am not native English speeker normally I understand most things comunicated to me after some thinking. But in solving error codes in compiling its another issue. It does not say why the function is not allowed and therefore dont give a hint about how to solve it. Just like the last error says ; missing when it is really () that is missing. Still new to programming, I guess it will be more obvious after some training/gaining experience.

This forum is amazing, so friendly and helpful peaople. Thanks a lot!

I could not help myself and stayed up late last night. The code didnt work as expected. After some hours of troubleshooting, now everything works as planned.

Thanks again

BR Magnus