Created
July 1, 2019 00:22
-
-
Save futureshocked/1b43273da9269dd693a595faabbf1b66 to your computer and use it in GitHub Desktop.
Arduino 101 datalogger example
This file contains hidden or 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
| // Written by Peter Dalmaris. | |
| // This is an example of how to record data from the Arduino 101 accelerometer to an SD card | |
| // Part of a demonstration for the Arduino Bootcamp for Teachers Extension Project | |
| #include <SPI.h> | |
| #include <SD.h> | |
| #include "CurieIMU.h" | |
| const int chipSelect = 4; | |
| void setup() { | |
| // Open serial communications and wait for port to open: | |
| Serial.begin(9600); | |
| while (!Serial) { | |
| ; // wait for serial port to connect. Needed for native USB port only | |
| } | |
| Serial.println("Initializing IMU device..."); | |
| CurieIMU.begin(); | |
| // Set the accelerometer range to 2G | |
| CurieIMU.setAccelerometerRange(2); | |
| 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("card initialized."); | |
| } | |
| void loop() { | |
| // make a string for assembling the data to log: | |
| String dataString = ""; | |
| float ax, ay, az; //scaled accelerometer values | |
| // read accelerometer measurements from device, scaled to the configured range | |
| CurieIMU.readAccelerometerScaled(ax, ay, az); | |
| // Append sensor data to the string: | |
| dataString += String(ax); | |
| dataString += ","; | |
| dataString += String(ay); | |
| dataString += ","; | |
| dataString += String(az); | |
| // 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("datalog.txt", 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); | |
| } | |
| // if the file isn't open, pop up an error: | |
| else { | |
| Serial.println("error opening datalog.txt"); | |
| } | |
| delay(10); // Take a measurement every 10 msec. | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment