Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
EJBlogger's Accellerologger code
#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.println(sumY / threshold);
Serial.println(sumY / threshold);
}
else {
Serial.println("error opening test.txt");
}
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