Skip to content

Instantly share code, notes, and snippets.

@set321go
Created May 22, 2019 15:35
Show Gist options
  • Save set321go/2c0245f73ef8a57f4ebb07828c69b99b to your computer and use it in GitHub Desktop.
Save set321go/2c0245f73ef8a57f4ebb07828c69b99b to your computer and use it in GitHub Desktop.
Accelerometer & SD card proptotype
#include <SD.h>
#include <Lucky.h>
const int chipSelect = 4;
void setup() {
Serial.begin(9600);
while(!Serial);
// Start the accelerometer board
Serial.println("Initalize Lucky Board ... ");
lucky.begin();
Serial.println("Initalize Lucky Board... Done");
Serial.print("Initializing SD card...");
// 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:
while (1);
}
Serial.println(" Done");
}
void loop() {
String dataString = "";
//read accelerometer sensor
lucky.accelerometer().read();
dataString += String(lucky.accelerometer().x());
dataString += ",";
dataString += String(lucky.accelerometer().y());
dataString += ",";
dataString += String(lucky.accelerometer().z());
// 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("data.csv", 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);
} else {
Serial.println("cannot open file");
}
// Serial.println(dataString);
// delay before next reading:
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment