Expected } at the end of input error

A common problem, and one that can cause a new programmer to pull their hair out.

However, it’s a problem that always has a simple solution, and with the right tricks it’s also easy to find where the problem lies.

Let’s take the following little code snippet, which throws up this exact error:

byte i = 0;

void doSomething() {
  digitalWrite(3, LOW);
}

void doSomethingElse() {
  digitalWrite(13, HIGH);
}

void setup() {
  // put your setup code here, to run once:
  pinMode(13, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (i == 1) {
    doSomething();
  i++;
}

At first glance that all looks perfectly good. But you compile, and you get:

/home/matt/Arduino/ErrorTest/ErrorTest.ino: In function 'void loop()':
ErrorTest:21: error: expected '}' at end of input
 }
 ^
exit status 1
expected '}' at end of input

Line 21, of course, is the last line of the file. And the error is pretty explicit – it expects a } at the end of the file. However, there is plainly one there already. So what is it on about?

Simply put, there aren’t enough } to balance out the number of { in the file. For every { there must be a } to go with it. On this little file it’s easy to just go through and count them all. Add one for a { and subtract one for a }. If the answer is not 0 then there’s something wrong. But it can take careful looking to see where the missing one should be, and even I have spent hours going cross-eyed trying to find the location in a big project.

There is one little trick, though, that can help you: Auto Format. The Auto Format facility in both the Arduino IDE and UECIDE (and many other IDEs as well) will sort out your indenting for you according to the presence of (amongst other things) { and }.  If the brackets don’t match up the formatting would end up wrong.

So let’s pass this little program through the auto formatting and see what happens:

byte i = 0;

void doSomething() {
  digitalWrite(3, LOW);
}

void doSomethingElse() {
  digitalWrite(13, HIGH);
}

void setup() {
  // put your setup code here, to run once:
  pinMode(13, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (i == 1) {
    doSomething();
    i++;
  }

As you can see the loop() function’s indenting has changed. And if your eyes are sharp enough you’ll already see what was wrong. The “if”, which had an opening bracket, didn’t have a closing one. The “i++” was supposed to be outside the “if” (as can be seen by the original indenting), but actually it wasn’t, and the end of the function was really the end of the “if”, leaving the function unfinished.

So fixing it is now really easy. By auto formatting we can see where the imbalance occurs, and fix it:

void loop() {
  // put your main code here, to run repeatedly:
  if (i == 1) {
    doSomething();
  }
  i++;
}

And now it compiles. But of course that little snippet doesn’t actually do anything useful…

Advertisements

1×10³²⁴ things to do with a chipKIT™ Lenny and a QuickIO. No. 2: Virtual Mouse.

LennyQuickIOSmall.pngNumber two in my series of what to do with a Lenny and QuickIO.

How about a mouse? Yeah, a mouse. Honest 🙂

Well, maybe not an actual mouse, but maybe make it control the mouse pointer in an “almost” usable way…?

The two potentiometers on the QuickIO could control the mouse position, and the buttons can be the mouse buttons for clicking, etc. That should work.

Continue reading

1×10³²⁴ things to do with a chipKIT™ Lenny and a QuickIO. No. 1: Autoclicker.

LennyQuickIOSmall.pngThis is the first part of a new series entitled “1×10³²⁴ things to do with a chipKIT™ Lenny and a QuickIO“. In it, I’m going to be exploring some of the cool things you could do in just a matter of seconds using the two boards combined.

This first one is an Autoclicker.

Continue reading

Vacuum Fluorescent Displays on Arduino

Watch the accompanying YouTube video here.

Vacuum Fluorescent Displays are probably one of the coolest displays of all time. Certainly one of the most popular of recent history. Developed in 1959 by Philips they have endured right through to modern times. You can even still find them in current consumer electronics.

1200px-vacuum_fluorescent_1

An example Vacuum Fluorescent Display from a VCR

Continue reading

Adding An OLED To My Computer

As part of my work I end up with hundreds of small TFT and OLED displays scattered around doing nothing. One of them, the PG25664CG OLED screen (16 shades of green) I figured would be about the same size and shape as a 5.25″ drive bay in a PC.

And guess what? I was right! An absolutely (well, a couple of mm out) perfect match. So I decided I should build one into my computer to display stuff. No idea what stuff yet, but stuff anyway. I’ll decide later when I have written the software for it all. Continue reading

Splitting Up Text in C

A common question I often find is:

How can I split this incoming data into parts?

It’s especially asked in conjunction with reading data through serial. So I thought I’d introduce you to two completely different approaches, each with benefits and drawbacks depending on the kind of data you are splitting.

Continue reading