Skip to content

Instantly share code, notes, and snippets.

@dwblair
Created May 10, 2016 23:54
Show Gist options
  • Save dwblair/4b9d1534b5ea31e88980cce99dc13ace to your computer and use it in GitHub Desktop.
Save dwblair/4b9d1534b5ea31e88980cce99dc13ace to your computer and use it in GitHub Desktop.
#include <SPI.h>
#include <SD.h>
// Set the pins used
#define cardSelect 4
#define switchPin 12
File logfile;
bool stopPin = true;
// blink out an error code
void error(uint8_t errno) {
while(1) {
uint8_t i;
for (i=0; i<errno; i++) {
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
}
for (i=errno; i<10; i++) {
delay(500);
}
}
}
// This line is not needed if you have Adafruit SAMD board package 1.6.2+
// #define Serial SerialUSB
void setup() {
// connect at 115200 so we can read the GPS fast enough and echo without dropping chars
// also spit it out
Serial.begin(9600);
Serial.println("\r\nAnalog logger test");
pinMode(13, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(cardSelect)) {
Serial.println("Card init. failed!");
error(2);
}
char filename[15];
strcpy(filename, "ANALOG00.TXT");
for (uint8_t i = 0; i < 100; i++) {
filename[6] = '0' + i/10;
filename[7] = '0' + i%10;
// create if does not exist, do not open existing, write, sync after write
if (! SD.exists(filename)) {
break;
}
}
logfile = SD.open(filename, FILE_WRITE);
if( ! logfile ) {
Serial.print("Couldnt create ");
Serial.println(filename);
error(3);
}
Serial.print("Writing to ");
Serial.println(filename);
pinMode(13, OUTPUT);
pinMode(8, OUTPUT);
Serial.println("Ready!");
pinMode(switchPin, INPUT_PULLUP);
logfile.println("time, A0");
Serial.println("time, A0");
}
uint8_t i=0;
void loop() {
while(stopPin != false){
digitalWrite(8, HIGH);
logfile.print(millis()); logfile.print(", "); logfile.println(analogRead(0));
Serial.print(millis()); Serial.print(", "); Serial.println(analogRead(0));
digitalWrite(8, LOW);
stopPin = digitalRead(switchPin);
delay(100);
}
logfile.flush();
error(5); // flash 5 times to indicate stop condition
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment