Hello I have managed to get both the Adafruit Datalogging shield walk-through and this individual code working http://www.learn.parallax.com/reed-switch-arduino-demo separately, but trying to combine the two together I get these errors. I think I have it all arranged correctly but any help would be greatly appreciated. Board Arduino Uno and Adafruit Datalogger shield.


Temp_and_Reed_Switch_data_logging_shield.ino:55:1: error: 'Serial' does not name a type
Temp_and_Reed_Switch_data_logging_shield.ino:58:8: error: expected constructor, destructor, or type conversion before '(' token
Temp_and_Reed_Switch_data_logging_shield.ino:61:1: error: expected unqualified-id before 'if'
Temp_and_Reed_Switch_data_logging_shield.ino:66:1: error: 'Serial' does not name a type
Temp_and_Reed_Switch_data_logging_shield.ino:70:1: error: expected unqualified-id before 'for'
Temp_and_Reed_Switch_data_logging_shield.ino:70:21: error: 'i' does not name a type
Temp_and_Reed_Switch_data_logging_shield.ino:70:30: error: 'i' does not name a type
Temp_and_Reed_Switch_data_logging_shield.ino:80:1: error: expected unqualified-id before 'if'
Temp_and_Reed_Switch_data_logging_shield.ino:84:1: error: 'Serial' does not name a type
Temp_and_Reed_Switch_data_logging_shield.ino:85:1: error: 'Serial' does not name a type
Temp_and_Reed_Switch_data_logging_shield.ino:87:1: error: 'Wire' does not name a type
Temp_and_Reed_Switch_data_logging_shield.ino:88:1: error: expected unqualified-id before 'if'
Temp_and_Reed_Switch_data_logging_shield.ino:95:1: error: 'logfile' does not name a type
Temp_and_Reed_Switch_data_logging_shield.ino:97:1: error: 'serial' does not name a type
Temp_and_Reed_Switch_data_logging_shield.ino:98:1: error: expected unqualified-id before 'if'
Temp_and_Reed_Switch_data_logging_shield.ino:104:8: error: expected constructor, destructor, or type conversion before '(' token
Temp_and_Reed_Switch_data_logging_shield.ino:105:8: error: expected constructor, destructor, or type conversion before '(' token
Temp_and_Reed_Switch_data_logging_shield.ino:107:8: error: expected constructor, destructor, or type conversion before '(' token
Temp_and_Reed_Switch_data_logging_shield.ino:108:13: error: expected constructor, destructor, or type conversion before '(' token
Temp_and_Reed_Switch_data_logging_shield.ino:114:1: error: expected declaration before '}' token
Multiple libraries were found for "SD.h"

 Used: C:\Users\Andrew\Documents\Arduino\libraries\SD

 Not used: C:\Program Files (x86)\Arduino\libraries\SD

'Serial' does not name a type


//  SD card datalogger
// SD card attached to SPI bus as follows:
// MOSI - pin 11
// MISO - pin 12
// CLK - pin 13
// CS - pin 10

#include "SD.h"
#include <Wire.h>
#include "RTClib.h"
#include <SPI.h>

#define LOG_INTERVAL  1000 // mills between entries
#define ECHO_TO_SERIAL   1 // echo data to serial port
#define WAIT_TO_START    0 // Wait for serial input in setup()

// the digital pins that connect to the LEDs
#define redLEDpin 4
#define greenLEDpin 5

// The analog pins that connect to the sensors

#define tempPin 1                // analog 1

RTC_DS1307 RTC; // define the Real Time Clock object
const int chipSelect = 10;
const int switchPin = 2;     //     // Reed switch to digital pin 2

// the logging file
File logfile;

void error(char *str)
  Serial.print("error: ");

  // red LED indicates error
  digitalWrite(redLEDpin, HIGH);

  while (1);
void setup() {
  // Open serial communications and wait for port to open:
  //If we set WAIT_TO_START to anything but 0, the Arduino will wait until the user types something in.
  Serial.println("Type any character to start");
  while (!Serial.available());
#endif //WAIT_TO_START

// initialize the SD card
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);

// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
  Serial.println("Card failed, or not present");
  // don't do anything more:
Serial.println("card initialized.");

// create a new file on SD Card called Logger number
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
  filename[6] = i / 10 + '0';
  filename[7] = i % 10 + '0';
  if (! SD.exists(filename)) {
    // only open a new file if it doesn't exist
    logfile = SD.open(filename, FILE_WRITE);
    break;  // leave the loop!

if (! logfile) {
  error("couldnt create file");

Serial.print("Logging to: ");

if (!RTC.begin()) {
  logfile.println("RTC failed");
  Serial.println("RTC failed");
#endif  //ECHO_TO_SERIAL

#if ECHO_TO_SERIAL // attempt to write out the header to the file
if (logfile.writeError || !logfile.sync()) {
  error("write header");
#endif   //ECHO_TO_SERIAL

pinMode(redLEDpin, OUTPUT);
pinMode(greenLEDpin, OUTPUT);

pinMode(switchPin, INPUT);        // switchPin is an input
digitalWrite(switchPin, HIGH);    // Activate internal pullup resistor
long lastDebounce1 = 0;
long debounceDelay = 500;    // Ignore bounces under 1/2 second

// If you want to set the aref to something other than 5v

void loop(void)
  DateTime now;

  // delay for the amount of time we want between readings
  delay((LOG_INTERVAL - 1) - (millis() % LOG_INTERVAL));

  digitalWrite(greenLEDpin, HIGH);

  // log milliseconds since starting
  uint32_t m = millis();
  logfile.print(m);           // milliseconds since start
  logfile.print(", ");
  Serial.print(m);         // milliseconds since start
  Serial.print(", ");

  // fetch the time
  now = RTC.now();
  // log time
  logfile.print(now.get()); // seconds since 2000
  logfile.print(", ");
  logfile.print(now.year(), DEC);
  logfile.print(now.month(), DEC);
  logfile.print(now.day(), DEC);
  logfile.print(" ");
  logfile.print(now.hour(), DEC);
  logfile.print(now.minute(), DEC);
  logfile.print(now.second(), DEC);
  Serial.print(now.get()); // seconds since 2000
  Serial.print(", ");
  Serial.print(now.year(), DEC);
  Serial.print(now.month(), DEC);
  Serial.print(now.day(), DEC);
  Serial.print(" ");
  Serial.print(now.hour(), DEC);
  Serial.print(now.minute(), DEC);
  Serial.print(now.second(), DEC);
#endif //ECHO_TO_SERIAL 

  int tempReading = analogRead(tempPin);

  // converting that reading to voltage, for 3.3v arduino use 3.3
  float voltage = tempReading * 5.0 / 1024;
  float temperatureC = (voltage - 0.5) * 100 ;

  logfile.print(", ");
  logfile.print(", ");

  Serial.print(", ");
  Serial.print(", ");

  digitalWrite(greenLEDpin, LOW);

2 Answers 2


Everything you have written between the end of setup() (line 55) to the start of loop() (line 117) is not in a function.

If you are struggling to combine two or more programs, start small and work your way to the full code.


Building furhter onto @CharlieHanson's answer, what you should do in order to fix this is probably following changes (please note that I wrote this in order to try to get rid of the errors, the flow of the program may still not be correct, that depends entirely on how you put together the code from the files):

1. Remove the bracket at line 52, just a few lines down in the setup()-method, below this code:

#endif //WAIT_TO_START

2. (Maybe not nessecary, but for manageability) Move this code:

long lastDebounce1 = 0;
long debounceDelay = 500;

-from where it currently is (just a few lines above the beginning of the loop()-method) to the other variable declarations (above the error()-method).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.