Solar-Powered Data Logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Energy - ITP - NYU | |
* Spring 2014 - Jeff Feddersen | |
* Solar challenge - Decibel meter | |
* Abe Rubenstein | |
* | |
* Arduino Uno | |
* Sparkfun microSD Shield | |
* Maxim MAX4466 Electret Microphone | |
*/ | |
#include <SD.h> | |
File myFile; | |
char fileName[] = "log2.txt"; | |
// Sample window width in mS (50 mS = 20Hz) | |
const int sampleWindow = 50; | |
unsigned int sample; | |
void setup() { | |
Serial.begin(9600); | |
while (!Serial) { ; } | |
Serial.print("Initializing SD card..."); | |
pinMode(10, OUTPUT); | |
if (!SD.begin(4)) { | |
Serial.println("Initialization failed."); | |
return; | |
} | |
Serial.println("Initialization done."); | |
} | |
void loop() { | |
unsigned long startMillis= millis(); // Start of sample window | |
unsigned int peakToPeak = 0; // peak-to-peak level | |
unsigned int signalMax = 0; | |
unsigned int signalMin = 1024; | |
// collect data for 50 mS | |
while (millis() - startMillis < sampleWindow) { | |
sample = analogRead(0); | |
if (sample < 1024) { | |
// save just the max levels | |
if (sample > signalMax) signalMax = sample; | |
// save just the min levels | |
else if (sample < signalMin) signalMin = sample; | |
} | |
} | |
// max - min = peak-peak amplitude | |
peakToPeak = signalMax - signalMin; | |
double volts = (peakToPeak * 3.68) / 1024; // to volts | |
myFile = SD.open(fileName, FILE_WRITE); | |
if (myFile) { | |
Serial.print("Writing to "); | |
Serial.print(fileName); | |
Serial.print("..."); | |
myFile.print(millis()); | |
myFile.print(","); | |
myFile.println(volts); | |
myFile.close(); | |
Serial.println("Done."); | |
} | |
else { | |
Serial.print("Error opening "); | |
Serial.println(fileName); | |
} | |
delay(200); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment