//Serial Comment Out
#define DEBUG 1 //switch to 1 to print to serial
#if DEBUG == 1
#define debug(x) Serial.print(x)
#define debugln(x) Serial.println(x)
#else
#define debug(x)
#define debugln(x)
#endif
//Libraries
#include <SPI.h>
#include <SdFat.h>
#include <OneWire.h> //DS18 temp
#include <DallasTemperature.h> //DS18 temp
//#include <SFE_BMP180.h> //BMP180 pressure
//Time
uint32_t timeStamp = 0;
int loopDelay = 3000;
//Document Info
String header0;
String header1; //update below
String header2;
//LED
const int ledPower = 3;
const int ledWrite = 2;
const int ledLight = 9; //must be PWM
// PIN 10 for SD card ONLY
//SD Card
SdFat SD; //SD instance (needs SdFat.h library)
File myFile; //name of file object as .txt
const int cS = 10; //SD card chip select, change to 8 if ya want DO NOT USE 10 for anything else
int fileNameNumber = 0; //incremented when a measurement is stopped
String SDFileName = "bmptest" + (String)fileNameNumber; //1st file is Log0...
//Photoresistor
const int lightPin = 0; //Analog 0, voltage divided wire
int lightLevel, high = 0, low = 1023; //light=no r, dark=high r, will be constrained to 0–255
float voltBytes = 4.01176471; //lightlevel (0–255) * this = 0–1023 resolution
float milliVolt = 0.00488759; //5v÷1023 resolution
//Temp DS18
#define ONE_WIRE_BUS 4 // Data wire is plugged into port 4 on the Arduino
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
//BMP180 Definitions
//SFE_BMP180 pressure;
double baseline; // baseline pressure
void setup() {
Serial.begin(115200);
//LED
pinMode(ledPower, OUTPUT); //Exterior Arduino On Power Up
digitalWrite(ledPower, HIGH); //Arduino On
pinMode(ledWrite, OUTPUT); //Exterior loop light
pinMode(10, OUTPUT); //Required for SD card module, do not use
//SD CARD
debug(F("Initializing SD card..."));
if (!SD.begin(cS)) { // see if the card is present and can be initialized:
debugln(F("Card not present"));
while (1) // don't do anything more:
;
}
debugln(F("card initialized."));
while (SD.exists(SDFileName)) {
fileNameNumber++;
SDFileName = "bmptest" + (String)fileNameNumber;
}
//Header
header0 = (F("Setup End"));
//Print initial startup
myFile = SD.open(SDFileName, FILE_WRITE);
if (myFile) {
//Temp DS18 Setup
sensors.begin(); // Start up the library
numberOfDevices = sensors.getDeviceCount(); // Grab a count of devices on the wire
myFile.print(F("# of DS18 Temp Sensors Found: "));
myFile.println(numberOfDevices, DEC);
debug(F("Found: "));
Serial.println(numberOfDevices, DEC); //leave as-is, buggy line with serial.print
//Headers
myFile.println(header0);
debugln(header0);
header1 = ("Payload Data for " + SDFileName);
header2 = (F("Time,°C1,°F1,°C2,°F2,relaltm,relaltft,[update:Altm(1013.25mb),Altft(1013.25mb),rAltm(1022.6mb),rAltft(1022.6mb)],LightLevel,Bytes,Volts")); //update as needed
myFile.println(header1);
debugln(header1);
/* if (pressure.begin())
debugln("BMP180 init success");
else{
debugln(F("BMP180 init fail (disconnected?)\n\n"));
}
baseline = getPressure(); // Get the baseline pressure:
*/
myFile.print(F("Baseline pressure (mb): "));
myFile.print(baseline);
debug(F("Baseline pressure (mb): "));
debug(baseline);
}
myFile.println(header2);
debugln(header2);
//Close SD
myFile.close();
//LED
ledWriteFlash();
}
//} //END setup
void loop() {
timeStamp = millis(); // this declaration must be in void loop
//PHOTORESISTOR
lightLevel = analogRead(lightPin);
autoTune(); // have the Arduino do the work for us!
analogWrite(ledLight, lightLevel); //nightlight: add 255 - before lightlevel
// SD CARD WRITE
myFile = SD.open(SDFileName, FILE_WRITE);
if (myFile) {
//Time
myFile.print(timeStamp);
debug(timeStamp);
printComma();
//DS18 Temp – should be inside SD write
sensors.requestTemperatures(); // Send the command to get temperatures
for(int i=0;i<numberOfDevices; i++) { // Loop through each device, print out temperature data
if(sensors.getAddress(tempDeviceAddress, i)){ // Search the wire for address
float tempC = sensors.getTempC(tempDeviceAddress);
myFile.print(tempC); //Celsius for first device
debug(tempC);
printComma();
myFile.print(tempC*(9.0/5.0)+32); //Fahrenheit, then loops back for next device
debug(tempC*(9.0/5.0)+32);
printComma();
}
}
//BMP Temp & Pressure
double a,P;
P = getPressure(); // Get a new pressure reading:
//a = pressure.altitude(P,baseline); // Show the relative altitude difference between the new reading and the baseline reading:
//for header2: relaltm,relaltft
//relative altitude (in m and ft):
if (a >= 0.0) myFile.print(" "); // add a space for positive numbers
myFile.print(a,1);
if (a >= 0.0) debug(" "); // add a space for positive numbers
Serial.print(a,1); //leave as-is
printComma();
if (a >= 0.0) myFile.print(" "); // add a space for positive numbers
myFile.print(a*3.28084,0);
if (a >= 0.0) debug(" "); // add a space for positive numbers
Serial.print(a*3.28084,0);
printComma();
//Photoresistor
char bytesStr[7]; //99999.9 and null
float Bytes = lightLevel*voltBytes;
dtostrf(Bytes,1,2,bytesStr); //convert to text
char voltsStr[5];
float Voltage = lightLevel*voltBytes*milliVolt;
dtostrf(Voltage,1,2,voltsStr);
char photo1[50];
sprintf(photo1,"%d,%s,%s",lightLevel,bytesStr,voltsStr); //photoresistor data string
myFile.print(photo1); // send to card
debug(photo1); //send to screen
printComma();
//New Line
myFile.println();
debugln();
myFile.close();
} else {
debugln("error opening bmptest");
} //END SD card write
//LED flash
ledWriteFlash();
delay(loopDelay);
} //END loop
double getPressure() {
char status;
double T,P,p0,a;
//status = pressure.startTemperature();
if (status != 0) { // Wait for the measurement to complete:
delay(status);
// status = pressure.getTemperature(T);
if (status != 0) {
// status = pressure.startPressure(3);
if (status != 0) { // Wait for the measurement to complete:
delay(status);
// status = pressure.getPressure(P,T);
if (status != 0) {
return(P);
} else debugln("error retrieving pressure measurement\n");
} else debugln("error starting pressure measurement\n");
} else debugln("error retrieving temperature measurement\n");
} else debugln("error starting temperature measurement\n");
}
void ledWriteFlash(){
digitalWrite(ledWrite, HIGH);
delay(50);
digitalWrite(ledWrite, LOW);
delay(100);
digitalWrite(ledWrite, HIGH);
delay(50);
digitalWrite(ledWrite, LOW);
delay(50);
}
void printComma() //from arduino help
{
myFile.print(F(","));
debug(F(","));
}
void manualTune(){ //necessary, though not used
lightLevel = map(lightLevel, 0, 1023, 0, 255);
lightLevel = constrain(lightLevel, 0, 255);
}
void autoTune(){
if (lightLevel < low) {
low = lightLevel;
}
if (lightLevel > high) {
high = lightLevel;
}
lightLevel = map(lightLevel, low+30, high-30, 0, 255); //can always mess with +numbers
lightLevel = constrain(lightLevel, 0, 255);
} //END autotune