Can't Compile Because of Curly brackets!!

I've been trying my best for the past week or so, but I just cannot work out where to put the }s - the nested if statements are getting too complicated for me to understand where to put }, so I keep going round in circles no matter what I try!
Is there anyone that is willing to help me/work it out themselves if they're bored enough!
(( BTW all my problems are coming from inside void loop(): ))

WiFi_Analog_Send.ino (3.63 KB)

webpage.h (115 Bytes)

My advice would be to put each { and } on a line of its own then to Auto Format the code in the IDE. Then check each pair of braces

This section certainly looks wrong as else should be the final part of an if clause and not be followed by else if

          }
          else
          {
            currentLine = "";
          }
          else if (c != '\r')      // if you got anything else but a carriage return character,
          {
            currentLine += c;      // add it to the end of the currentLine
          }

Thanks for the advice - you're right, putting each {} on the same vertical line helps with clarity but once again I'm not making progress.
I get "error: expected declaration before '}' token" and in trying to fix that get "'c' was not declared in this scope", I assume by putting this outside of the clause where c was declared.

You probably should step back a bit and read a C syntax guide to understand when and where you need {} (and the consequence of defining variables inside {} - read about scope)
Once you get that then it will all seem easy

Tools > Auto Format is an extremely useful tool. Not only for making your code readable but also for finding problems with it. If you're using the Arduino Web Editor you will not have access to this tool. I recommend using the standard IDE if possible, otherwise finding another external C++ formatting tool you can run your code through.

Do a Tools > Auto Format and then check the indentation of your code to see if it matches your intended program structure. Hint: every function should start and end with no indentation.

After using the auto-format (mentioned by pert), all function definitions should start at the beginning of a new line.

You will see that your void printWifiStatus() { does not start at the beginning of a new line which indicates that you have this function defined inside the previous function ( loop() ); so loop is missing a } somewhere.

Hi,

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

J-M-L , pert and sterretje thanks for the advice.
I'll go back to basics.
I am not using the Web Editor.
I've used Auto Format.

TomGeorge , I know how to do this but thought in this instance it would be wise to include the entire document in case anyone wanted to download and compile for themselves.

Hi,
Ops code;

/*
  Created 25 Nov 2012
  by Tom Igoe
*/
#include <SPI.h>
#include <WiFi101.h>

char ssid[] = "0000000";  //  your network SSID (name)
char pass[] = "0000000";   // your network password
int keyIndex = 0;              // your network key Index number (needed only for WEP)

int LDRpin = A0;
int LDRval = 0;
int TEMPpin = A1;
int TEMPval = 0;

int status = WL_IDLE_STATUS;
WiFiServer server(80);

const char *webpage =
#include "webpage.h"
  ;
void setup() {
  Serial.begin(9600);
  pinMode(LDRpin, INPUT);
  pinMode(TEMPpin, INPUT);
  TEMPval = analogRead(TEMPpin);
  LDRval = analogRead(LDRpin);

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while (true);       // don't continue
  }

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(7000);
  }
  server.begin();                           // start the web server on port 80
  //printWifiStatus();                        // you're connected now, so print out the status
}


void loop() {
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                           // if you get a client,
    Serial.println("new client");         // print a message out the serial port
    String currentLine = "";              // make a String to hold incoming data from the client
    while (client.connected()) {          // loop while the client's connected
      if (client.available()) {           // if there's bytes to read from the client,
        char c = client.read();           // read a byte, then
        Serial.write(c);                  // print it out the serial monitor


        if (c == '\n') {                    // if the byte is a newline character
          while (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.println(webpage);
            client.println(LDRval);
            client.println("
");
            client.println(TEMPval);
            client.println("</h2>");
            client.println("</body>");
            client.println("</html>");
            client.println();
            break;
          }
          else {
            currentLine = "";
          }
          else if (c != '\r') {    // if you got anything else but a carriage return character,
            currentLine += c;      // add it to the end of the currentLine
          }
        }
      }


      delay(50);
      client.stop(); //close the connection:
      Serial.println("client disonnected");
    }
  }



  void printWifiStatus() {
    // print the SSID of the network you're attached to:
    Serial.print("SSID: ");
    Serial.println(WiFi.SSID());

    // print your WiFi shield's IP address:
    IPAddress ip = WiFi.localIP();
    Serial.print("IP Address: ");
    Serial.println(ip);

    // print the received signal strength:
    long rssi = WiFi.RSSI();
    Serial.print("signal strength (RSSI):");
    Serial.print(rssi);
    Serial.println(" dBm");
    // print where to go in a browser:
    Serial.print("To see this page in action, open a browser to http://");
    Serial.println(ip);
  }

To put your code iinto IDE from code tags, you left click Code "Select" to highlight entire code, then right click the highlighted code and "copy" and then paste it into IDE.
Tom... :slight_smile:

The fact that the final brace is not on the left margin is a bit of a giveaway that the braces are messed up. Where does the loop() function end ?

Hi,
If you place the cursor beside any left facing curly brackets, the IDE will tell you where is right facing mate is.
brackets.jpg

Tom... :slight_smile:

If you place the cursor beside any left facing curly brackets, the IDE will tell you where is right facing mate is.

It's a shame that the yellow box does not always appear, although the corresponding brace to the one clicked does get highlighted by a box.

Putting the cursor in the gutter on the left of the editor also shows the extent of a set of braces, which can be helpful and you can collapse a section of code if code folding is turned on.

Hi,
It would be good if the IDE had the Auto-Format really automatic.
Each time it sees a } (return) it formats the indent to be level with its mate if it can find one.

Make it so it can be configured back to the normal "manual" Auto-Format.

Tom.... :o :o :o :o

I don't know whether you can make it do what you suggest, but you can change the way that Auto Format works. Look at formatter.conf in the same folder as preferences.txt in the IDE

I have changed mine so that each brace is put on its own line and blank lines in functions are removed. More options are described at # Artistic Style

UKHeliBob:
I don't know whether you can make it do what you suggest, but you can change the way that Auto Format works. Look at formatter.conf in the same folder as preferences.txt in the IDE

I have changed mine so that each brace is put on its own line and blank lines in functions are removed. More options are described at # Artistic Style

Sorry the link doesn't work.
Tom.. :slight_smile:

TomGeorge:
Sorry the link doesn't work.
Tom.. :slight_smile:

This one should:

TomGeorge:
Sorry the link doesn't work.
Tom.. :slight_smile:

My fault for copying the comment character at the start.