Created
December 22, 2016 15:38
-
-
Save stuartchilds/8ef03822d7027c1f437cb4176f0be573 to your computer and use it in GitHub Desktop.
Adafruit Adalogger M0 demo 02 - logging Accelerometer and Battery voltage values to SD card
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
/* | |
* Demo sketch for Adafruit Feather Adalogger | |
* Original code by Adafruit | |
* found here: https://gist.githubusercontent.com/ladyada/13efab4022b7358033c7/raw/8387409d8f9b1c2157bf8c9e78dff3c0a3b0007d/adalogger.ino | |
* | |
* Also includes code from MMA8451demo, also from Adafruit | |
* | |
* Modified by SC for simple battery voltage reading and accelerometer values logged to SD card | |
* 22-Dec-16 | |
*/ | |
#include <SPI.h> | |
#include <SD.h> | |
#include <Wire.h> | |
#include <Adafruit_MMA8451.h> | |
#include <Adafruit_Sensor.h> | |
Adafruit_MMA8451 mma = Adafruit_MMA8451(); | |
// Set the pins used | |
#define cardSelect 4 | |
//define the input for battvery voltage reading | |
#define VBATPIN A7 | |
File logfile; | |
// 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(100); | |
digitalWrite(13, LOW); | |
delay(100); | |
} | |
for (i=errno; i<10; i++) { | |
delay(200); | |
} | |
} | |
} | |
// This line is not needed if you have Adafruit SAMD board package 1.6.2+ | |
// #define Serial SerialUSB | |
void setup() { | |
// connect at 115200 | |
Serial.begin(115200); | |
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!"); | |
//additional setup for battery voltage reading | |
//pinMode(9, INPUT); //is this needeD? | |
if (! mma.begin()) { | |
Serial.println("Couldnt start"); | |
while (1); | |
} | |
Serial.println("MMA8451 found!"); | |
mma.setRange(MMA8451_RANGE_2_G); | |
Serial.print("Range = "); Serial.print(2 << mma.getRange()); | |
Serial.println("G"); | |
} | |
uint8_t i=0; | |
void loop() { | |
digitalWrite(8, HIGH); | |
//battery voltage reading + logging | |
float measuredvbat = analogRead(VBATPIN); | |
measuredvbat *= 2; //multiplies reading back to get voltage | |
measuredvbat *= 3.3; // multiply by 3.3V, reference voltage | |
measuredvbat /= 1024; // convert to voltage | |
logfile.print("VBat = "); logfile.println(measuredvbat); | |
//logfile.flush(); //hopefully this fixes data not being written to file | |
Serial.print("Voltage of Battery: "); Serial.println(measuredvbat); | |
// Read the 'raw' data in 14-bit counts | |
mma.read(); | |
logfile.print("X: "); logfile.print(mma.x); | |
logfile.print(", Y: "); logfile.print(mma.y); | |
logfile.print(", Z: "); logfile.print(mma.z); | |
logfile.println(); | |
logfile.println(); | |
/* Get a new sensor event */ | |
sensors_event_t event; | |
mma.getEvent(&event); | |
/* Display the results (acceleration is measured in m/s^2) */ | |
logfile.print("X: "); logfile.print(event.acceleration.x); logfile.print(", "); | |
logfile.print("Y: "); logfile.print(event.acceleration.y); logfile.print(", "); | |
logfile.print("Z: "); logfile.print(event.acceleration.z); logfile.print(", "); | |
logfile.println("m/s^2 "); | |
logfile.println(); | |
/* Get the orientation of the sensor */ | |
uint8_t o = mma.getOrientation(); | |
switch (o) { | |
case MMA8451_PL_PUF: | |
logfile.println("Portrait Up Front"); | |
break; | |
case MMA8451_PL_PUB: | |
logfile.println("Portrait Up Back"); | |
break; | |
case MMA8451_PL_PDF: | |
logfile.println("Portrait Down Front"); | |
break; | |
case MMA8451_PL_PDB: | |
logfile.println("Portrait Down Back"); | |
break; | |
case MMA8451_PL_LRF: | |
logfile.println("Landscape Right Front"); | |
break; | |
case MMA8451_PL_LRB: | |
logfile.println("Landscape Right Back"); | |
break; | |
case MMA8451_PL_LLF: | |
logfile.println("Landscape Left Front"); | |
break; | |
case MMA8451_PL_LLB: | |
logfile.println("Landscape Left Back"); | |
break; | |
} | |
logfile.println(); | |
logfile.println(); | |
Serial.println("accelerometer data logged"); | |
logfile.flush(); | |
digitalWrite(8, LOW); | |
//adjust interval between each reading | |
delay(100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment