Arduino -Need help with the C error: a function-definition is not allowed here before ' {'

Please help, i build this code but it keeps showing me this error, i already check the braces and all of them matches, i don’t know what’s wrong, please someone help me, here’s the code:

void setup() {
  for (int i=2;i<=11;i++) {
    pinMode(i,OUTPUT);
  }
  pinMode(12,INPUT);
  pinMode(13,INPUT);
  pinMode(A0,INPUT);
  pinMode(A1,INPUT);
  pinMode(A2,INPUT);

  int boton=0;

  //Funcion que prende y apaga un led y pone un delay
  void flash(int led, int tiempo){
    digitalWrite(led,1);
    delay(tiempo);
    digitalWrite(led,0);
    delay(tiempo);
  }

  //Funcion que prende y apaga 5 leds con un delay entre medias
  void flash5(int led1,int led2,int led3,int led4,int led5, int tiempo) {
    digitalWrite(led1,1);
    digitalWrite(led2,1);
    digitalWrite(led3,1);
    digitalWrite(led4,1);
    digitalWrite(led5,1);
    delay(tiempo);
    digitalWrite(led1,0);
    digitalWrite(led2,0);
    digitalWrite(led3,0);
    digitalWrite(led4,0);
    digitalWrite(led5,0);
    delay(tiempo);
  }

  //Funcion que prende 3 leds
  void flash1(int led1,int led2,int led3){
    digitalWrite(led1,1);
    digitalWrite(led2,1);
    digitalWrite(led3,1);
  }
  //Funcion que prende 4 leds
  void flash2(int led1,int led2,int led3,int led4){
    digitalWrite(led1,1);
    digitalWrite(led2,1);
    digitalWrite(led3,1);
    digitalWrite(led4,1);
  }
  //Funcion que apaga 3 leds
  void flash3(int led1,int led2,int led3,int tiempo){
    digitalWrite(led1,0);
    digitalWrite(led2,0);
    digitalWrite(led3,0);
    delay(tiempo);
  }
  //Funcion que apaga 4 leds
  void flash4(int led1,int led2,int led3,int led4,int tiempo){
    digitalWrite(led1,0);
    digitalWrite(led2,0);
    digitalWrite(led3,0);
    digitalWrite(led4,);
    delay(tiempo);
  }

  //Funcion de la animacion 1
  void animacion1() {
    flash5(2,4,6,8,10,30);
    flash5(2,4,6,8,10,30);
    flash5(3,5,7,9,11,30);
    flash5(3,5,7,9,11,30);
  }

  //Funcion de la animacion 2
  void animacion2() {
    flash1(3,6,9);
    flash4(2,5,8,11,30);
    flash2(2,5,8,11);
    flash3(4,7,10,30);
    flash1(4,7,10);
    flash3(3,6,9,30);
  }

  //Funcion de la animacion 3
  void animacion3() {
    flash1(3,5,7);
    delay(30);
    flash3(3,5,7,30);
    flash1(6,8,10);
    delay(30);
    flash3(6,8,10,30);
    flash2(9,11,2,4);
    delay(30);
    flash4(9,11,2,4,30);
  }

  //Funcion de la animacion de los leds aleatorios
  void aleatorio() {
    int aleatorio=random(2,11);
    if(aleatorio==2) {
      flash(2,30);
      flash(2,30);
      }
      else if(aleatorio==3) {
      flash(3,30);
      flash(3,30);
      }
      else if(aleatorio==4) {
      flash(4,30);
      flash(4,30);
      }
      else if(aleatorio==5) {
      flash(5,30);
      flash(5,30);
      }
      else if(aleatorio==6) {
      flash(6,30);
      flash(6,30);
      }
      else if(aleatorio==7) {
      flash(7,30);
      flash(7,30);
      }
      else if(aleatorio==8) {
      flash(8,30);
      flash(8,30);
      }
      else if(aleatorio==9) {
      flash(9,30);
      flash(9,30);
      }
      else if(aleatorio==10) {
      flash(10,30);
      flash(10,30);
      }
      else if(aleatorio==11) {
      flash(11,30);
      flash(11,30);
    }
  }

  //Funcion para parar las animaciones
  void parar() {
    for(int i=2;i<=11;i++) {
      digitalWrite(i,0);
    }
  }
}

void loop() {
  if (digitalRead(12)==1) {
    boton=1;
    delay(300);
  }
  else if (digitalRead(13)==1) {
    boton=2;
    delay(300);
  }
  else if (digitalRead(A0)==1) {
    boton=3;
    delay(300);
  }
  else if (digitalRead(A1)==1) {
    boton=4;
    delay(300);
  }
  else if (digitalRead(A2)==1) {
    boton=5;
    delay(300);
  }

  switch(boton) {
    case 1: animacion1();
    break;
    case 2: animacion2();
    break;
    case 3: animacion3();
    break;
    case 4: aleatorio();
    break;
    case 5: parar();
    break;
  }
}

the error is at the topic of my post, and this code is for turn on some leds connected to my arduino, and it also has 5 buttons connected and every one makes the leds turn on in a different way

“C” language, i made it on the arduino program, the error is in the line #143, but it also happens in every function

Working with an Arduino sometimes can be a bit tricky - parts of the C you are working with are hidden.

Background: With C, functions can be ‘declared’, which lets other files and functions know about the function or ‘defined’, which provides the actual code for the function. If a function has not yet been ‘declared’, then the ‘definition’ is also a ‘declaration’. C is rather particular about where ‘definitions’ and ‘declarations’ can occur.

Example:

  • A function declaration just describes the inputs and outputs:
return_type my_function_name( parameters );

or

int add_three(int a, int b, int c);
  • A function definition provides the code for the function:
return_type my_function_name( parameters ) {
  // Your code goes here
  return return_value;
};

or

int add_three(int a, int b, int c) {
  int result = a + b + c;

  return result;
};

Problem: The compiler is providing an error on line 143 because you are trying to ‘declare’ and ‘define’ a new function, parar() inside of setup(). It is very non-standard to define functions inside of another function, like you are doing with flash(), flash5(), …, aleatorio(), and parar(). This is called ‘lexical scoping’ and it is not supported in the C standard.

Solution: I would pull your functions outside of the function setup(). Remember, you need to make sure any function you call is ‘declared’ (or ‘declared’ + ‘defined’) (i.e. earlier in the file) before you use it, so these functions would need to come before loop().

Extra Information: Arduino is actually calling your functions setup() and loop() in a main() function (see below). These functions have separate scopes; the variable names inside of the code for these functions and all of the data is private to the running of these functions.
You can write data out of a function one of 3 ways: 1) using ‘return’, 2) updating a global variable, or 3) using pointers. I’m not sure if built in functions like pinMode() or digitalWrite() do 2) or 3).

Arduino Main Function:

    int main(void) {
    init();
    initVariant();

    setup();

    for (;;) {
        loop();
        if (serialEventRun) serialEventRun();
    }   
    return 0;
}

This tells me that the variable boton on line 11 will only be in scope for the function setup(). The variable is not in the scope for the function loop(). You do not need line 11, because you don’t use boton in setup(), but you will need to define the variable boton in the function loop().

TLDR: You can’t define functions inside of functions in the C standard. Pull your new functions, such as flash() and parar(), outside of setup() but before loop(). You’re also going to have scope issues with boton on line 11 and inside of loop.

I know that was a lot of information - please let me know if I can be clearer!

1 Like