-2

I, being new to Arduino started off by making a simple lcd watch which also shows the temperature.

Everything was working fine untill this error came up "a function-definition is not allowed here before '{' token "

And I have done everything to resolve the problem. I wrote the function from scratch. I read somewhere that a function definition can't come within a function, so I made it a Global function(though, personally I somehow don't feel that it's true). But even then the error managed to survive.

Being completely frustrated I don't know what to do. Can anyone please help me out ?

here's my code :-

#include < LiquidCrystal.h >

  const int rs = 12,
    en = 11,
    d4 = 5,
    d5 = 4,
    d6 = 3,
    d7 = 2,
    lm = A0;
int input;
float temp;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void tem() //Calculates Temperature
{
  int i = analogRead(lm);
  float temp = 500.0 * i / 1024;

  lcd.print("Temp : ");
  lcd.print(temp);
  lcd.print(" ^C");

}

void Clock(int h = 0, int m = 0, int s = 0, char ch = 'A') {
  int H = h;

  char ampm = ch;

  void mornight(char ampm) //changes am to am to pm and vice versa
  { //THE RED HIGHLIGHTED ERROR LINE
    String AMPM;
    if (ampm == 'A')
      AMPM = "PM";
    else
      AMPM = "AM";

  }
  while (H != 12) //HOUR'S LOOP
  {
    for (int M = m; M <= 59; M++) //MINUTE'S LOOP
    {
      for (int S = s; S <= 59; S++) //SECOND'S LOOP
      {
        delay(1000);
        lcd.clear();

        if (H < 10) {
          lcd.print('0');
          lcd.print(H);
          lcd.print(':');
        } else {
          lcd.print(H);
          lcd.print(":");
        }
        if (M < 10) {
          lcd.print('0');
          lcd.print(M);
          lcd.print(':');
        } else {
          lcd.print(M);
          lcd.print(":");
        }
        if (S < 10) {
          lcd.print('0');
          lcd.print(S);
          lcd.print(AMPM);
        } else {
          lcd.print(S);
          lcd.print(AMPM);
        }
        lcd.setCursor(0, 1);
        tem(); // shows temperature

      }
      s = 0;
    }
    m = 0;
    if (H == 23) {
      H = 1;
      mornight(ampm);
    } else
      H++;
  }
}

void setup() {
  lcd.begin(16, 2);
  lcd.setCursor(0, 1);
  Clock(10, 1, 20);

}
void loop() {

}

2 Answers 2

1

Your line:

void mornight(char ampm)

Is trying to define a function called mornight() inside of Clock().

Functions are "global" if they are defined outside of any other function. I can't see what you tried in order to make this function global, but besides object-oriented programming, plain functions are always global.

A "non-global" function would mean defining a class and declaring the function inside the class definition; but even then you have to define the function (that is, the actual instructions in the function) outside of any other class or function definition.

0

Move function void mornight(char ampm) outside of function void Clock(int h = 0, int m = 0, int s = 0, char ch = 'A') {

as the error message says, function can't be defined inside functions

Not the answer you're looking for? Browse other questions tagged or ask your own question.