Compiling problem

Hello gang - I am new to the Arduino (after years of it sitting in a box on my shelf). I have been using it to teach myself 'programming' in terms of C/C++. I have done lots of BASIC (multiple versions including some really old, obscure versions) and I have done 8085/Z80 assembler (that was a LOT of years ago).

I understand the basic concepts of coding including includes, variable declaration and their scope, etc. etc. Now it's just learning to understand how 'C' works - especially in the Arduino world.

I have a Mega 2650 R3 and an SD Shield 3.0 for an SD card. I have a set of 37 different sensors of which I am using 2 simple ones - temperature (NTC) and Optical.

Working through the examples and libraries - I figured out how to implement these 2 sensors (basically the same) and collect readings from them. I worked through the Time library and learned how to set and then display the time. I combined these two skills and sketches, breadboarded both sensors, and collected data every 5 seconds through a Putty session all weekend. The goal being to capture the input into a spreadsheet and graph it - expecting to see a pattern of lights on/lights off and heat on/heat off over the weekend. All good.

Now - I hooked up my SD shield and worked through the examples and was able interact with different SD cards I have - reading & writing.

Once again - I combined my learning, taking my simple sensor data logging sketches, enhancing with SD file opening and trying to log the data to an SD card instead of the serial port of the computer.

When I compile my code - I get an error that "'myFile' does not name a type". When I compare this code to my other sketch (basically copied from examples on the Arduino page - modified the name of the file - just for practice) - it appears to be constructed correctly. I even had a C-programmer coworker look at the 2 sketches and see if I did something blatantly wrong.

I searched through this forum, and saw a posting stating that the user needed to include the .cpp and .h files into the directory for Arduino to find/include them. And I think that is my problem, except... I can't find that file at all in my Libraries directory.

Within the Arduino IDE using the Manage Libraries function - the SD library is listed as Built-in and shows as 'INSTALLED'. I've tried looking for a library download - but Google is not giving me anything.

Here is my code... (I hope I did this right)
(Almost - code tags need [ ], vs < > . Moderator)

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

File myFile;

#define TIME_HEADER  "T"   // Header tag for serial time sync message
#define TIME_REQUEST  7    // ASCII bell character requests a time sync message 

int Minute = 26;
int Printed = 0; 
int LED13 = 13;
int tempSensorPin = A5; // input pin to be used
int refPin = A4; // hook this pin to 3.3V
int ltSensorPin = A3;

void setup() {
 // put your setup code here, to run once:
pinMode(LED_BUILTIN,OUTPUT);
 Serial.begin(9600);
 setSyncProvider( requestSync);  //set function to call when sync required
   while (!Serial) {
   ; // wait for serial port to connect. Needed for native USB port only
 }
   Serial.print("Initializing SD card...");

 if (!SD.begin(4)) {
   Serial.println("initialization failed!");
   return;
 }
 Serial.println("initialization done.");
 Serial.println("Waiting for sync message");
}
 myFile = SD.open("test.txt", FILE_WRITE);
 if (!myFile) {
 Serial.println("Error opening file");
}

void loop() {
 // put your main code here, to run repeatedly:  
 if (Serial.available()) {
   processSyncMessage();
 }
 Minute=second();
 if ((Minute % 5) == 0) {
   myFile.print("Temp|");
   myFile.print(analogRead(A5));
   myFile.print("|RefV|");
   myFile.print(analogRead(A4));
   myFile.print("|Light|");
   myFile.print(analogRead(A3));
   myFile.print("|Time|");
   digitalClockDisplay();
  
 Serial.print("Seconds = ");
 Serial.println(Minute);
 Serial.print("Modulo = ");
 Serial.println(Minute % 5);
 Serial.print("timeStatus = ");
 Serial.println(timeStatus());

 }

delay (1000);
if (Printed == 0) {
 Printed = 1;
}
 else {
   Printed = 0;
 }
digitalWrite(LED_BUILTIN, Printed);  
}

void digitalClockDisplay(){
 // digital clock display of the time
 myFile.print(hour());
 printDigits(minute());
 printDigits(second());
 myFile.print(" ");
 myFile.print(day());
 myFile.print(" ");
 myFile.print(month());
 myFile.print(" ");
 myFile.print(year()); 
 myFile.println(); 
}

void printDigits(int digits){
 // utility function for digital clock display: prints preceding colon and leading 0
 myFile.print(":");
 if(digits < 10)
   myFile.print('0');
 myFile.print(digits);
}

void processSyncMessage() {
 unsigned long pctime;
 const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013

 if(Serial.find(TIME_HEADER)) {
    pctime = Serial.parseInt();
    if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
      setTime(pctime); // Sync Arduino clock to the time received on the serial port
    }
 }
}

time_t requestSync()
{
 Serial.write(TIME_REQUEST);  
 return 0; // the time will be sent later in response to serial mesg
}

Bill (teaching an old dog new tricks)

Well the compiler is giving you a huge hint of the pb.
Compiler believes that you use myFile as a type. So the error is when you have a line that could be a declaration of something starting with myFile.

Looking quickly you see that there is one place like this:

myFile = SD.open("test.txt", FILE_WRITE);

Why would the compiler thinks this? Because You have a closing } just above that ends the setup()...

If the compiler error was not obvious to you (im pretty sure it was also telling you which line was bogus) You would find this easily if you indented your code properly - hit ctrl-T in the IDE

Well there you go! I figured it would be something obvious (to someone) - thanks so much for the quick response.

I recognize that my 'code' is really 'butchered' - I only work on this during my lunch hour in attempt to learn as much as I can as quick as I can. But your advice is well taken - I will be more deliberate about my indenting / coding technique in the future..

(I only wish I had thought of this yesterday - I would have liked to have this working for the weekend).

Thank you!!

Bill

Yes sometimes some stuff gets in the way and you stared at your code so much that you can see the obvious only when pointed at it. Don't feel bad - it happens to all of us

Glad it helped! Happy coding