New to programming and '}' is already getting the better of me.

Hello all!

I've been introduced to the wonderful world of programming and I plan to make it a hobby. However being new to programming - other than the vague classes in college making a LED blink - it brings some stupid mistakes with it.

Here's what I try to do;

I bought the following things:

Arduino uno
Grove base shield
Grove Air quality sensor v1.3
Grove OLED Display 0.96" (SSD1315)

I downloaded both libraries and tried to put them together so the message would appear on my OLED.

The error I get is on line 39: expected '}' before 'else'

Putting a '}' there messes up the else if configuration of the code and one problem follows after the other.

// Grove sensor libraries
#include"AirQuality.h"
#include"Arduino.h"
AirQuality airqualitysensor;
int current_quality =-1;

// Oled libraries
#include <Arduino.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

U8G2_SSD1306_128X64_ALT0_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

void setup()
{
    u8g2.begin();  
    Serial.begin(9600);
    airqualitysensor.init(A0); 
}
void loop()
{
    current_quality=airqualitysensor.slope();
    if (current_quality >= 0)// if a valid data returned.
    {
        if (current_quality==0)
            Serial.println("Very high pollution");
              u8g2.firstPage();
            do {
               u8g2.setFont(u8g2_font_ncenB10_tr);
                u8g2.drawStr(0,24,"Very High pollution");
            } while ( u8g2.nextPage() );                     // First condition - Very high pollution

                      
           else if (current_quality==1)
            Serial.println("High pollution!");
              u8g2.firstPage();
            do {
               u8g2.setFont(u8g2_font_ncenB10_tr);
                u8g2.drawStr(0,24,"High pollution");
           } while ( u8g2.nextPage() );                   // Second condition - High Pollution

                     
           else if (current_quality==2)
            Serial.println("Low pollution");
              u8g2.firstPage();
            do {
               u8g2.setFont(u8g2_font_ncenB10_tr);
                u8g2.drawStr(0,24,"Low pollution");
           } while ( u8g2.nextPage() );                  // Third condition - Low pollution

           
           else if (current_quality ==3)
            Serial.println("Fresh air");
              u8g2.firstPage();
            do {
               u8g2.setFont(u8g2_font_ncenB10_tr);
                u8g2.drawStr(0,24,"Fresh air");
           } while ( u8g2.nextPage() );                 // Fourth condition - Fresh Air
    }
}
ISR(TIMER2_OVF_vect)
{
    if(airqualitysensor.counter==122)//set 2 seconds as a detected duty
    {
        airqualitysensor.last_vol=airqualitysensor.first_vol;
        airqualitysensor.first_vol=analogRead(A0);
        airqualitysensor.counter=0;
        airqualitysensor.timer_index=1;
        PORTB=PORTB^0x20;
    }
    else
    {
        airqualitysensor.counter++;
    }
}

I am very eager to learn so if anyone could explain how to fix this, that would be very awesome!

See if the IDE's auto format tool (ctrl-T) can give you some clues.

Hi there!

I didn't know about that feature yet! It says that there are no changes necessary.

jonasbraat:
It says that there are no changes necessary.

I think I disagree

Please cut and paste the entire, actual error log here.

Hi aarg!

Here it is:

C:\Users\Jonas\Documents\Arduino\Air_quality_with_display\Air_quality_with_display.ino: In function 'void loop()':
Air_quality_with_display:39:5: error: expected '}' before 'else'
else if (current_quality == 1)
^~~~
Air_quality_with_display:48:5: error: 'else' without a previous 'if'
else if (current_quality == 2)
^~~~
Air_quality_with_display:57:5: error: 'else' without a previous 'if'
else if (current_quality == 3)
^~~~
C:\Users\Jonas\Documents\Arduino\Air_quality_with_display\Air_quality_with_display.ino: At global scope:
Air_quality_with_display:65:1: error: expected declaration before '}' token
}
^
exit status 1
expected '}' before 'else'

My -guess- is you missed a { here

if (current_quality == 0)
      Serial.println("Very high pollution");

-jim lee

Unfortunately, it isn't working :sob: :sob:

My idea was to open the 2 seperate codes which work fine on their own and put integrate the OLED code into the air quality code. So instead of Serial.print( ... ) I would also add the code for OLED beneath it.

Please post your updated sketch. Make sure to do a Tools > Auto Format first.

All of the block of code between IF and ELSE must be surrounded by { } like this

       if (current_quality==0) {  // opening brace
            Serial.println("Very high pollution");
              u8g2.firstPage();
            do {
               u8g2.setFont(u8g2_font_ncenB10_tr);
                u8g2.drawStr(0,24,"Very High pollution");
            } while ( u8g2.nextPage() );                     // First condition - Very high pollution

           }     // closing brace      
           else if (current_quality==1)

and the same for each ELSE IF block of code

...R

Omg omg omg! thank you so much! it worked!

If anyone can reccomend a good programming book for the arduino, i'll definitely buy it!

Again, thank you so much!!

jonasbraat:
Omg omg omg! thank you so much! it worked!

If anyone can reccomend a good programming book for the arduino, i'll definitely buy it!

Again, thank you so much!!

Book? What's a book?

The best way to learn to program on the arduino is to make mistakes. In the IDE there's dozens of example programs; play with them. Buy an Arduino Sensor kit and experiment with making them work. When something doesn't work as expected, that's what we are here for.

"The best way to learn to program on the arduino is to make mistakes. "

I can only go as far as that it is a good way, and people do certainly seem to succeed taking what can only be called a variety of approaches.

In the case of the OP's difficulties, however, I wonder where and when people catch on to the basic syntactic structure of programs and statements, like how { and } come in pairs, that if/else is

if | if else

and that a might be many smaller statements grouped with { }… even if/else statements, whoa! Light bulb.

Excuse my informal BNF here, you get the idea - seems like a bit of not-so-hard work at the very beginning, from a teaching resource that matches one's learning style, would save a lot of head scratching.

The kind of thing I would expect to find in a book. The kind of thing it may be hard to google up starting from knowing nothing that would inform the selection of search criteria.

When you have people posting things like "I tried an "if" loop and that didn't work, maybe I need "for" looping my void" you can see the thing going off the rails.

Probably cutting both ways is the ease with which a noob can start getting real results. Soon reach exceeds grasp; I am sure more than one person who might have become a competent hobbyist just gives up in frustration.

a7

I bought the MKR1000 iot kit from the Arduino shop + some other things so I am settled. I did burn my temperature sensor tho by reversing the +/- so there's that mistake I learned from! :')

To get a good sense from when to use where in coding is going to be a hard one. So I'll need a good class that can teach me the basics of everything.

A huge thanks to everyone for replying!

Cheers!

You can learn about if else and {} (curly braces)

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