Last active
September 9, 2017 10:02
-
-
Save sinewalker/d552648e5835f557898f8e32b7eb4dab to your computer and use it in GitHub Desktop.
EJBlogger's Accellerologger code
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
#include <SparkFun_ADXL345.h> | |
#include <SD.h> | |
#include <SPI.h> | |
//ADXL345 adxl = ADXL345(10); For SPI Cumunication | |
ADXL345 adxl = ADXL345(); | |
File myFile; | |
const int threshold = 1499; | |
const int switchPin = 7; | |
int switchState; | |
int ctr = 0; | |
int pinCS = 10; | |
int x, y, z; | |
int sumX, sumY, sumZ; | |
void setup() { | |
Serial.begin(9600); | |
Serial.println(); | |
pinMode(pinCS, OUTPUT); | |
pinMode(switchPin, OUTPUT); | |
adxl.powerOn(); | |
adxl.setRangeSetting(16); | |
adxl.setSpiBit(0); | |
myFile = SD.open("test.txt", FILE_WRITE); | |
} | |
void loop() { | |
//define int x, y, z in setup instead | |
// also define int sumX, sumY, sumZ in setup | |
adxl.readAccel(&x, &y, &z); | |
sumX += x; | |
sumY += y; | |
sumZ += z; | |
switchState = digitalRead(switchPin); | |
if (switchState == HIGH) { | |
ctr++; | |
if (ctr > threshold) { | |
if (myFile) { | |
myFile.print(sumX / threshold); myFile.print(","); | |
myFile.print(sumY / threshold); myFile.print(","); | |
myFile.println(sumZ / threshold); | |
} | |
else { | |
Serial.println("error opening test.txt"); | |
} | |
Serial.print(sumX / threshold); Serial.print(","); | |
Serial.print(sumY / threshold); Serial.print(","); | |
Serial.println(sumZ / threshold); | |
sumX = 0; | |
sumY = 0; | |
sumZ = 0; | |
ctr = 0; | |
} | |
} else //switchState != HIGH | |
{ | |
myFile.close(); | |
Serial.println("closed"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment