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

I have been looking through the topics and have tried to fix my sketch, but I can't find what I am missing. Here is the sketch:

#include <Adafruit_GFX.h>          // Core graphics library
#include <Adafruit_ST7735.h>       // Hardware-specific library
#include <SdFat_Adafruit_Fork.h>   // SD card & FAT filesystem library
#include <Adafruit_SPIFlash.h>     // SPI / QSPI flash library
#include <Adafruit_ImageReader.h>  // Image-reading functions

#define USE_SD_CARD

#define SD_CS 4    // SD card chip select
#define TFT_CS 10  // TFT select pin
#define TFT_DC 8   // TFT data/command pin
#define TFT_RST 9  // Or set to -1 and connect TFT RST to Arduino reset pin

#if defined(USE_SD_CARD)
SdFat SD;                         // SD card filesystem
Adafruit_ImageReader reader(SD);  // Image-reader object, pass in SD filesys
#else
// SPI or QSPI flash filesystem (i.e. CIRCUITPY drive)
#if defined(__SAMD51__) || defined(NRF52840_XXAA)
Adafruit_FlashTransport_QSPI flashTransport(PIN_QSPI_SCK, PIN_QSPI_CS,
                                            PIN_QSPI_IO0, PIN_QSPI_IO1, PIN_QSPI_IO2, PIN_QSPI_IO3);
#else
#if (SPI_INTERFACES_COUNT == 1)
Adafruit_FlashTransport_SPI flashTransport(SS, &SPI);
#else
Adafruit_FlashTransport_SPI flashTransport(SS1, &SPI1);
#endif
#endif
Adafruit_SPIFlash flash(&flashTransport);
FatVolume filesys;
Adafruit_ImageReader reader(filesys);  // Image-reader, pass in flash filesys
#endif

Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
Adafruit_Image img;  // An image loaded into RAM
int32_t width = 0,   // BMP image dimensions
  height = 0;

void setup(void) {

  ImageReturnCode stat;  // Status from image-reading functions

  Serial.begin(9600);
#if !defined(ESP32)
  while (!Serial)
    ;  // Wait for Serial Monitor before continuing
#endif

  tft.initR(INITR_MINI160x80);  // Initialize screen
  Serial.println(F("TFT initialized."));

  Serial.print(F("Initializing filesystem..."));
#if defined(USE_SD_CARD)
  // SD card is pretty straightforward, a single call...
  if (!SD.begin(SD_CS, SD_SCK_MHZ(10))) {  // Breakouts require 10 MHz limit due to longer wires
    Serial.println(F("SD begin() failed"));
    for (;;)
      ;  // Fatal error, do not continue
  }
#else
  // SPI or QSPI flash requires two steps, one to access the bare flash
  // memory itself, then the second to access the filesystem within...
  if (!flash.begin()) {
    Serial.println(F("flash begin() failed"));
    for (;;)
      ;
  }
  if (!filesys.begin(&flash)) {
    Serial.println(F("filesys begin() failed"));
    for (;;)
      ;
  }
#endif
  Serial.println(F("OK!"));

  tft.fillScreen(ST77XX_BLACK);
  tft.setRotation(1);
  tft.invertDisplay(1);

  reader.drawBMP("/1.bmp", tft, 0, 0);


  void loop() {

    tft.setRotation(1);
    int voltage = analogRead(A0);                    // read the voltage input from analog pin A0
    int imageIndex = map(voltage, 0, 1023, 0, 219);  // map the voltage value to an image index (assuming there are 5 images on the SD card)

    File imageFile = SD.open("image" + String(imageIndex) + ".bmp");  // open the image file on the SD card
    tft.drawBitmap(0, 0, imageFile);                                  // draw the image on the ST7735 display
    imageFile.close();                                                // close the image file

    delay(10);  // wait for 1 second before displaying the next image
  }}

I have tried a variety of {} combinations around the loop with no success. The error is coming up referencing the void loop line.

You're missing a closing } before the loop function to close the setup() function.

Here only one } is needed to close the loop function.

  • Every } needs a friend {
  • Suggest you always place { and } on lines by themselves.

To make it easier for me to follow I always add the comment at the closing } // End of function xx

I sometimes also do it with in the functions especially if I am having problems. I also agree the ‘}’ needs to be on a line by itself, much easier to debug.

Make it a habit to reformat your code often. And then again. Soon enough you will be able to spot these pesky common mistakes

Use ctrl-T on Windows, command-T on Mac