A classic "function-definition" problem that is stumping me for days

Hello, I'm looking for some guidance. I'm making a simple random number generator that outputs to a MAX7219 8x8 matrix LED display. My first test is simply to have it output a number between 1 and 12 every time I press the reset button.

I keep running into this error, despite me checking things over a bunch.

Arduino: 1.8.2 (Windows 10), Board: "Arduino/Genuino Uno"

D:\Documents\Arduino\firstrandomtomatrixtest\firstrandomtomatrixtest.ino: In function 'void setup()':

firstrandomtomatrixtest:38: error: a function-definition is not allowed here before '{' token

   void loop () {

                ^

firstrandomtomatrixtest:106: error: expected '}' at end of input

 }

 ^

exit status 1
a function-definition is not allowed here before '{' token

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

My code is here:

//first test of this random number generator to MAX7219 matrix display

#include "LedControlMS.h"

/*
  pin 12 is connected to the DataIn
  pin 11 is connected to the CLK
  pin 10 is connected to LOAD
*/



#define NBR_MTX 2
LedControl lc = LedControl(12, 11, 10, 4);
long randNumber;

void setup() {
  /*
    The MAX72XX is in power-saving mode on startup,
    we have to do a wakeup call
  */
  Serial.begin (9600);
  Serial.println("Setup");
  //digitCounter=0;
  for (int i = 0; i < NBR_MTX; i++) {
    lc.shutdown(i, false);
    /* Set the brightness to a medium values */
    lc.setIntensity(i, 1);
    /* and clear the display */
    lc.clearDisplay(i);
    randomSeed(analogRead(0));
  }


  //delay(100);


  void loop () {

    randNumber = random(12);
    delay(100);

    if (randNumber == 0) {
      lc.writeString(0, "1");
      delay(5000);
    }


    if (randNumber == 1) {
      lc.writeString(0, "2");
      delay(5000);
    }

    if (randNumber == 2) {
      lc.writeString(0, "3");
      delay(5000);
    }

    if (randNumber == 4) {
      lc.writeString(0, "5");
      delay(5000);
    }

    if (randNumber == 5) {
      lc.writeString(0, "6");
      delay(5000);
    }

    if (randNumber == 6) {
      lc.writeString(0, "7");
      delay(5000);
    }

    if (randNumber == 7) {
      lc.writeString(0, "8");
      delay(5000);
    }

    if (randNumber == 8) {
      lc.writeString(0, "9");
      delay(5000);
    }

    if (randNumber == 9) {
      lc.writeString(0, "10");
      delay(1000);
      lc.writeString(1, "10");
      delay(5000);
    }

    if (randNumber == 10) {
      lc.writeString(0, "11");
      delay(1000);
      lc.writeString(1, "11");
      delay(5000);
    }

    if (randNumber == 11) {
      lc.writeString(0, "12");
      delay(1000);
      lc.writeString(1, "12");
      delay(5000);
    }
  }
}

Any help would be deeply appreciated!

For each { you need a }

.

Looks to me that the } you need to close setup() is down in loop(). You have an extra in loop() and missing one at the end of setup().

Thank you! It appears like I've been staring at the code for too long to realize that. It's working. :slight_smile:

When you are trying to troubleshoot a sketch do a Tools > Auto Format (as you seem to have done) and then check that all the indentation is as expected. You will notice that:

  void loop () {

was indented, which it should not be. That is a very useful clue to where the problem is. The Arduino IDE also provides a few other tools that can be used for troubleshooting this type of problem. When the cursor is on a bracket the matching bracket will be highlighted. In Arduino IDE 1.8.2 if the cursor is at the closing bracket and the associated opening state is off the screen it will make a tooltip pop up on the toolbar after a second that shows the statement. If you do File > Preferences > Enable Code Folding (check) > OK you can use the little -/+ buttons on the left margin to fold brackets.