I just started messing with some prgramming and using the arduino. I have figured out how to read current and temperature. Now, I am trying to write them onto an SD card. For the current and temperature, I watched some tutorials and found some code that worked after a little tweaking. I am going over the SD example and tying to add temperature and current to work with it. I have taken a shot at it, but am getting an error. I am a little lost and some help would be much appreciated. The code and error are below:
/*
SD card datalogger
This example shows how to log data from three analog sensors
to an SD card using the SD library.
The circuit:
- analog sensors on analog ins 0, 1, and 2
- SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created 24 Nov 2010
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SD.h>
#include <math.h> //loads the more advanced math functions
#include <EmonLib.h>
EnergyMonitor emon1; // Create an instance
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 10;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
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:
return;
}
Serial.println("card initialized.");
}
//Temperature setup
double Thermister(int RawADC) { //Function to perform the fancy math of the Steinhart-Hart equation
double Temp;
Temp = log(((10240000/RawADC) - 10000));
Temp = 1 / (1.133E-3 + ( 2.334E-4 + ( 9.056E-8 * Temp * Temp ))* Temp );
Temp = Temp - 273.15; // Convert Kelvin to Celsius
Temp = (Temp * 9.0)/ 5.0 + 32.0; // Celsius to Fahrenheit - comment out this line if you need Celsius
return Temp;
}
//Current Setup
{
** emon1.current(1, 1.82); // Current: input pin, calibration.**
}
void loop()
{
// make a string for assembling the data to log:
String dataString = "Temp, Current";
//Temperature Read
int val; //Create an integer variable
double temp; //Variable to hold a temperature value
val=analogRead(0); //Read the analog port 0 and store the value in val
temp=Thermister(val); //Runs the fancy math on the raw analog value
Serial.println(temp); //Print the value to the serial port
delay(1000); //Wait one second before we do it again
//Cur
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
The error is "expected unqualified-id before '{' token". I have bolded where the error is at.