SD Data Logger 'Myfile' Does not name type

Hello,

I am trying to create a simple data logger with an SD card shield. I have followed a tutorial and examined the example scripts in the SD library examples folder. However my code does not work and I cannot see what the issue is.

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

File myFile;

int pinCS = 10;

void setup() {
 Serial.begin (9600);
 pinMode(pinCS, OUTPUT);

 //SD Card Initialization
 if (SD.begin())
 {
  Serial.println("SD card is ready to use");
 } else
  {
    Serial.println("SD card initialization failed");
    return;
  }
    }
 // Create/Open file
 myFile = SD.open("text.txt", FILE_WRITE);

 //if the file opened okay write to it
 if (myFile) {
  Serial.println("writing to file...");
  //write to file
  myFile.println("Testing text 1,2,3...");
  myFile.close(); // Close the file
  Serial.println("Done");
 }
 //if the file didnt open, print an error
 else {
  Serial.println("error opening test file");
 }

//reading the file
myFile = SD.open("test.txt");
if (myFile) {
  Serial.println("Read");
  //Reading the whole file
  while (myFile.available())
  {
  Serial.write(myFile.read());
}
myFile.close();
}
else {
  Serial.println("error opening test.txt");
}
}

The error appears as:

Build options changed, rebuilding all
sketch_aug01a:23: error: ‘myFile’ does not name a type

myFile = SD.open(“text.txt”, FILE_WRITE);

^

sketch_aug01a:26: error: expected unqualified-id before ‘if’

if (myFile) {

^

sketch_aug01a:34: error: expected unqualified-id before ‘else’

else {

^

sketch_aug01a:39: error: ‘myFile’ does not name a type

myFile = SD.open(“test.txt”);

^

sketch_aug01a:40: error: expected unqualified-id before ‘if’

if (myFile) {

^

Multiple libraries were found for “SD.h”
Used: E:\Documents\Arduino\libraries\SD
Not used: E:\Arduino\libraries\SD
exit status 1
‘myFile’ does not name a type

Can anyone tell me why it cannot “name a type”? I have imported both the libraries and used the naming convention from the SD library example “myFile”.

From looking around I understand that the issue usually revolves around the library not being installed correctly. Both these libraries come with the Arduino software so I am unsure how this could be the problem.

Any advice is greatly appreciated.

Use Control-T to format your code and line up the blocks. Since the IDE will do it for you, there really is no excuse for improperly formatted code. We don't do that just because it is pretty. It will help you find the brace that is misplaced in your code.

Basically your setup function ends and then the rest of that code is hanging out in no-mans-land outside any function where it is illegal.

There is nothing wrong with your code formatting. If you have taken the care to look, and it looks right by you, it is right, and it looks fine by me too. Autoformatting is more of a liability, and only for use by the utterly incompetent.

Every opening curlybrace must have a closing partner. You can identify them by bringing the cursor up to one and checking the block around its partner. Your problem will be above the pink line, sometimes a bit further away than you might think.

Here you have an extra closing brace which closes the setup prematurely.

   Serial.println("SD card initialization failed");
    return;
  }
  }<-----------------------------------HERE
  // Create/Open file
  myFile = SD.open("text.txt", FILE_WRITE);

so try deleting it.

You also have no loop, which may be just an oversight. I believe you must have one in order to compile properly, even if it does nothing.

void loop()
{
}

Nick_Pyner: There is nothing wrong with your code formatting. If you have taken the care to look, and it looks right by you, it is right, and it looks fine by me too. Autoformatting is more of a liability, and only for use by the utterly incompetent.

It certainly will line up those braces and make it real obvious real quick which one is missing or in the wrong place. In this case you would see that if statement jammed all the way up against the left margin and you'd know where you messed up.

But I guess I'm just utterly incompetent huh? Funny how right after you say that you go on to completely back up my comment by pointing out exactly what autoformat would have. At least to any half way competent programmer.

I think I was wrong about that little bit of stirring. I recognise that auto-format helps identify a bodgie curly, and I didn't know that. I also see that the grouping I was so concerned about is subsequently maintained. I couldn't get that before, some years ago. Maybe it has been improved, maybe it was me.

The incorrect curly bracket was indeed the issue.

I added the loop and removed the extra bracket and the code verifies just fine. Embarrassingly simple.

I also tried without the loop and it failed. So Nick_Pyner you were right on both points.

Delta_G thank you for the tip about auto formatting. I did not know about this and will keep in mind for the future.

I have uploaded the code and it functions as expected.

Thank you both for your advice and your speedy replies.

Delta_G: But I guess I'm just utterly incompetent huh?

I use autoformat quite a bit, for someone like myself, and I accept I am uterly incompetant, I do find it helpful.