arduino error: expected primary-expression before '}' token

Hi All Arduino Users

I’m trying to upload the code to the board and I got this error:

arduino error: expected primary-expression before ' ' token

Code looks fine for me and I don’t know where is problem. Anyone could help me?

Here is all code:

#include <SPI.h>
#include <SD.h>
#include <string.h>
#include <Keyboard.h>

File myFile;
boolean first = true;

void setup() { 
  String dip = ""; // Name of the file that will be opened

  // Sets the given pins as switches for the dip switches
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  
  // Switches are checked, dip string is contructed
  if (digitalRead(6) == LOW){dip += "1";} else {dip += "0";}
  if (digitalRead(7) == LOW){dip += "1";} else {dip += "0";}
  if (digitalRead(8) == LOW){dip += "1";} else {dip += "0";}
  if (digitalRead(9) == LOW){dip += "1";} else {dip += "0";}

  dip += ".txt";


  if (!SD.begin(4)) {
    return;
  }
  
  // Desired file is opened
  myFile = SD.open(dip);
  if (myFile) {
    Keyboard.begin();
    
    String line = "";
    while (myFile.available()) {
      char m = myFile.read();
      if (m == '\n'){
        Line(line);
        line = "";
        }
        else if((int) m != 13)
        {
          line += m;
        }
    }
    Line(line);
    
    myFile.close();
  } else {
  }

  Keyboard.end();
}

void Line(String l)
{
  int space_1 = l.indexOf(" ");
  if (space_1 == -1)
  {
    Press(l);
  }
  else if (l.substring(0,space_1) == "STRING")
  {
    Keyboard.print(l.substring(space_1 + 1));
  }
  else if (l.substring(0,space_1) == "DELAY")
  {
    int delaytime = l.substring(space_1 + 1).toInt();
    delay(delaytime);
  }
  else if(l.substring(0,space_1) == "REM"){}
  else
  {
      String remain = l;

      while(remain.length() > 0)
      {
        int latest_space = remain.indexOf(" ");
        if (latest_space == -1)
        {
          Press(remain);
          remain = "";
        }
        else
        {
          Press(remain.substring(0, latest_space));
          remain = remain.substring(latest_space + 1);
        }
        delay(5);
      }
  }

  Keyboard.releaseAll();
}


void Press(String b)
{
  if(b.length() == 1)
  {
    char c = b[0];
    Keyboard.press(c);
  }
  else if (b.equals("ENTER"))
  {
    Keyboard.press(KEY_RETURN);
  }
  else if (b.equals("CTRL"))
  {
    Keyboard.press(KEY_LEFT_CTRL);
  }
    else if (b.equals("SHIFT"))
  {
    Keyboard.press(KEY_LEFT_SHIFT);
  }
    else if (b.equals("ALT"))
  {
    Keyboard.press(KEY_LEFT_ALT);
  }
    else if (b.equals("GUI"))
  {
    Keyboard.press(KEY_LEFT_GUI);
  }
    else if (b.equals("UP") || b.equals("UPARROW"))
  {
    Keyboard.press(KEY_UP_ARROW);
  }
    else if (b.equals("DOWN") || b.equals("DOWNARROW"))
  {
    Keyboard.press(KEY_DOWN_ARROW);
  }
    else if (b.equals("LEFT") || b.equals("LEFTARROW"))
  {
    Keyboard.press(KEY_LEFT_ARROW);
  }
    else if (b.equals("RIGHT") || b.equals("RIGHTARROW"))
  {
    Keyboard.press(KEY_RIGHT_ARROW);
  }
    else if (b.equals("DELETE"))
  {
    Keyboard.press(KEY_DELETE);
  }
    else if (b.equals("PAGEUP"))
  {
    Keyboard.press(KEY_PAGE_UP);
  }
    else if (b.equals("PAGEDOWN"))
  {
    Keyboard.press(KEY_PAGE_DOWN);
  }
    else if (b.equals("HOME"))
  {
    Keyboard.press(KEY_HOME);
  }
    else if (b.equals("ESC"))
  {
    Keyboard.press(KEY_ESC);
  }
    else if (b.equals("INSERT"))
  {
    Keyboard.press(KEY_INSERT);
  }
    else if (b.equals("TAB"))
  {
    Keyboard.press(KEY_TAB);
  }
    else if (b.equals("END"))
  {
    Keyboard.press(KEY_END);
  }
    else if (b.equals("CAPSLOCK"))
  {
    Keyboard.press(KEY_CAPS_LOCK);
  }
    else if (b.equals("F1"))
  {
    Keyboard.press(KEY_F1);
  }
    else if (b.equals("F2"))
  {
    Keyboard.press(KEY_F2);
  }
    else if (b.equals("F3"))
  {
    Keyboard.press(KEY_F3);
  }
    else if (b.equals("F4"))
  {
    Keyboard.press(KEY_F4);
  }
    else if (b.equals("F5"))
  {
    Keyboard.press(KEY_F5);
  }
    else if (b.equals("F6"))
  {
    Keyboard.press(KEY_F6);
  }
    else if (b.equals("F7"))
  {
    Keyboard.press(KEY_F7);
  }
    else if (b.equals("F8"))
  {
    Keyboard.press(KEY_F8);
  }
    else if (b.equals("F9"))
  {
    Keyboard.press(KEY_F9);
  }
    else if (b.equals("F10"))
  {
    Keyboard.press(KEY_F10);
  }
    else if (b.equals("F11"))
  {
    Keyboard.press(KEY_F11);
  }
    else if (b.equals("F12"))
  }             <--------------------------This line is highlighted
}

void loop() {
  // nothing happens after setup
}

I marked line which is highlighted after uploading and getting error. I tried to remove ‘}’, but it doesnt solved the problem. To be more specific I’m putting all console output below:

[color=orange]
C:\Users\7urtl3\Documents\Arduino\dipswitches\dipswitches.ino: In function 'void Press(String)':

dipswitches:232:3: error: expected primary-expression before '}' token

   }

   ^

C:\Users\7urtl3\Documents\Arduino\dipswitches\dipswitches.ino: At global scope:

dipswitches:233:1: error: expected declaration before '}' token

 }

 ^

Multiple libraries were found for "Keyboard.h"
 Used: C:\Users\7urtl3\Documents\Arduino\libraries\Keyboard
 Not used: C:\Program Files (x86)\Arduino\libraries\Keyboard
Using library SPI at version 1.0 in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SPI
[/color] 
Using library SD at version 1.2.3 in folder: C:\Program Files (x86)\Arduino\libraries\SD 
Using library Keyboard in folder: C:\Users\7urtl3\Documents\Arduino\libraries\Keyboard (legacy)
Using library HID at version 1.0 in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\HID 
[color=orange]
exit status 1
expected primary-expression before '}' token
[/color]

Your if statement for F12 doesn’t have anything associated with it. The compiler is complaining that it was expecting if() Do something. But the something is missing and there’s a random closing brace instead. I expect that what you intended was

  else if (b.equals("F12"))
  {
    Keyboard.press(KEY_F12);
  }

wildbill:
Your if statement for F12 doesn’t have anything associated with it. The compiler is complaining that it was expecting if() Do something. But the something is missing and there’s a random closing brace instead. I expect that what you intended was

  else if (b.equals("F12"))

{
    Keyboard.press(KEY_F12);
  }

Thanks wildbill

Any suggestions how to fix it?

Any suggestions how to fix it?

What do you want to happen if b.equals "F12" if it is not what Bill suggested ?

7urtl3:
Any suggestions how to fix it?

Braces in a C++ program have to be balanced. Every opening { needs a closing }, and vice versa.

In your program you have an obvious closing } with no matching opening {. To fix it you need to balance you braces. What else needs to be done depends on your intent. Nobody knows that besides yourself.

It's not my code and I'm not a coder...I just found curious project and trying to put some code to arduino board, but I have encountered error. Thats why I'm asking experts here for help. If anyone want to have a look on the project, it's there:

https://github.com/Seytonic/Duckduino-microSD

7urtl3:
It's not my code and I'm not a coder...I just found curious project and trying to put some code to arduino board, but I have encountered error. Thats why I'm asking experts here for help. If anyone want to have a look on the project, it's there:

https://github.com/Seytonic/Duckduino-microSD

But the code at the link is different from yours. The code at the link looks fine to me. There's no such error there.

You yourself managed to destroy/damage the text of the program. I don't know how: dropped a pot with petunias onto the keyboard, left a floppy disk in a MRI machine - it doesn't matter. If you can't simply copy a block of text correctly, or notice the difference (even after the compiler explicitly told you where that difference is!), then there's nothing we can do.

7urtl3:
I'm not a coder...

That's what Arduino is all about, helping non-coders become more comfortable with coding, but it does require a little effort.