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

I am building a flight logger with Arduino, and I have done the entire setup code, but its giving me this error:

/Users/Taran/Documents/Arduino/flight_logger/flight_logger.ino: In function 'void setup()':
flight_logger:64:13: error: a function-definition is not allowed here before '{' token
void loop() {
^
flight_logger:64:13: error: expected '}' at end of input
Multiple libraries were found for "Adafruit_SSD1306.h"
Used: /Users/Taran/Documents/Arduino/libraries/Adafruit_SSD1306
Not used: /Users/Taran/Documents/Arduino/libraries/Adafruit_SSD1306_Wemos_Mini_OLED
exit status 1
a function-definition is not allowed here before '{' token

I am also attaching my code as well for your reference.


#include <SPI.h>
#include <Wire.h>
#include <TinyGPS.h>
#include <SoftwareSerial.h>
#include <SD.h>
#include <Adafruit_SSD1306.h>

// --------------------------------------------------------------------------initialising the display------------------------------------------------------------------------------------------------------------------------------------------------------

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define display_RESET -1 // since sharing reset pin with Arduino
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, display_RESET);



const int cs_sd = 2;
static const int RX = 3, TX = 4;
static const uint32_t gps_baud = 9600; // might need to change later on depending on sensitivity and speed requirements. 
TinyGPS gps;
SoftwareSerial ss(RX, TX);

int speedMax = 0, speedCheck = 0;
int heightMax = 0, heightCheck = 0;
int satMax = 0, satCheck = 0;


void setup() {
  Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
 
    
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(15, 40);
  display.write("VP-DK Avionics");
  display.write("Flight Logger ver1.0");
  delay(2000);
  display.clearDisplay();
  
  Wire.begin();
  ss.begin(gps_baud);

  if(!SD.begin(cs_sd));
    display.clearDisplay();
    display.write("Default Card.");
    delay(500);
    display.clearDisplay();
    
  File data = SD.open("Flight Logger.txt", FILE_WRITE);
  data.println(" ");
  data.println("Data acquisition started");
  data.close();
  
}

void loop() {

please do let me know what the error is, I have tried effectively everything. Probably I'm missing out on something very tiny.

regards,
Taran

A very helpful troubleshooting tool is the Auto Format feature (Tools > Auto Format in the Arduino IDE or Ctrl+B in the Arduino Web Editor). If you do an Auto Format and then compare the resulting indentation to your intended program structure, it will quickly point you to where there is a missing or extra brace. I think if you do this the problem with your code will become clear.

Another useful feature of the Arduino IDE/Arduino Web Editor is that when you place the cursor next to one bracket, it puts a box around the matching bracket. In the Arduino IDE, if the cursor is next to the closing bracket and the opening bracket is off the screen then it will show the opening bracket line in a tool tip after a short delay.

I found the problem, it was the second brace after the loop function, thanks for the prompt replies.

Didn't know about the auto format, will use it further on.

regards,
Taran

You're welcome. I'm glad to hear it's working now. Enjoy!
Per

Really? I think there are some more problems with the braces.

The closing brace is missing here:

  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
 
    
  display.clearDisplay();

And here the braces are missing completely. The semicolon at the end of the if statement makes it useless:

  if(!SD.begin(cs_sd));
    display.clearDisplay();
    display.write("Default Card.");
    delay(500);
    display.clearDisplay();
    
  File data = SD.open("Flight Logger.txt", FILE_WRITE);

I guess the autoformat sorted it out.

I've never seen that the autoformat sorted something out ... :roll_eyes:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.