Skip to content

Instantly share code, notes, and snippets.

@sighrobot
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sighrobot/9729602 to your computer and use it in GitHub Desktop.
Save sighrobot/9729602 to your computer and use it in GitHub Desktop.
Solar-Powered Data Logger
/*
* 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