Skip to content

Instantly share code, notes, and snippets.

@dwblair
Created July 27, 2016 20:18
Show Gist options
  • Save dwblair/6d7c89f746e6ebb4cb7082e7268890ac to your computer and use it in GitHub Desktop.
Save dwblair/6d7c89f746e6ebb4cb7082e7268890ac to your computer and use it in GitHub Desktop.
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
// accelerometer
#define ACCELEROMETER 0x38
#define X_OUT1 0x02
#define X_OUT2 0x03
#define Y_OUT1 0x04
#define Y_OUT2 0x05
#define Z_OUT1 0x06
#define Z_OUT2 0x07
#define debug 1 // 0: don't print anything out; 1: print out debugging statements
// logger
// Set the pins used
#define cardSelect 4
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 so we can read the GPS fast enough and echo without dropping chars
// also spit it out
Serial.begin(115200);
if (debug) Serial.println("\r\nAnalog logger test");
pinMode(13, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(cardSelect)) {
if (debug) 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 ) {
if(debug) Serial.print("Couldnt create ");
if (debug) Serial.println(filename);
error(3);
}
if (debug) {
Serial.print("Writing to ");
Serial.println(filename);
}
pinMode(13, OUTPUT);
pinMode(8, OUTPUT);
if (debug) Serial.println("Ready!");
// accel
Wire.begin();
//set acc sensor to update data with 1.5KHz
byte bma_settings = accRead(0x14);
accWrite((bma_settings|6));
}
uint8_t i=0;
void loop() {
// acel
// print the sensor values:
int x = printXVal();
// Serial.print("\t\t");
int y = printYVal();
// Serial.print("\t\t");
int z = printZVal();
String dataString = "";
dataString += x;
dataString += " ";
dataString += y;
dataString += " ";
dataString += z;
digitalWrite(8, HIGH);
/*
logfile.print(millis());
logfile.print("\t");
logfile.print(x);
logfile.print("\t");
logfile.print(y);
logfile.print("\t");
logfile.println(z);
*/
logfile.println(dataString);
if (debug) Serial.println(dataString);
//logfile.print("A0 = "); logfile.println(analogRead(0));
//Serial.print("A0 = "); Serial.println(analogRead(0));
digitalWrite(8, LOW);
delay(50);
}
int printXVal() {
// Serial.print("x: ");
int raw_x1 = (int)accRead(X_OUT1);
int raw_x2 = (int)accRead(X_OUT2);
byte low_bits[8] = {0,0,0,0,0,0,0,0};
byte high_bits[8] = {0,0,0,0,0,0,0,0};
uint2bitAry(raw_x1, low_bits);
uint2bitAry(raw_x2, high_bits);
int negative_flag = high_bits[7];
int raw_acceleration = negative_flag<<15;
if(negative_flag) raw_acceleration >>= 6;
byte value_ary[9] = {low_bits[6], low_bits[7], high_bits[0], high_bits[1], high_bits[2], high_bits[3], high_bits[4], high_bits[5], high_bits[6]};
int acceleration_value = bitarray2int(value_ary, 9);
raw_acceleration |= acceleration_value;
//Serial.print(raw_acceleration);
return raw_acceleration;
}
int printYVal() {
//Serial.print("y: ");
int raw_y1 = (int)accRead(Y_OUT1);
int raw_y2 = (int)accRead(Y_OUT2);
byte low_bits[8] = {0,0,0,0,0,0,0,0};
byte high_bits[8] = {0,0,0,0,0,0,0,0};
uint2bitAry(raw_y1, low_bits);
uint2bitAry(raw_y2, high_bits);
int negative_flag = high_bits[7];
int raw_acceleration = negative_flag<<15;
if(negative_flag) raw_acceleration >>= 6;
byte value_ary[9] = {low_bits[6], low_bits[7], high_bits[0], high_bits[1], high_bits[2], high_bits[3], high_bits[4], high_bits[5], high_bits[6]};
int acceleration_value = bitarray2int(value_ary, 9);
raw_acceleration |= acceleration_value;
//Serial.print(raw_acceleration);
return raw_acceleration;
}
int printZVal() {
//Serial.print("z: ");
int raw_z1 = (int)accRead(Z_OUT1);
int raw_z2 = (int)accRead(Z_OUT2);
byte low_bits[8] = {0,0,0,0,0,0,0,0};
byte high_bits[8] = {0,0,0,0,0,0,0,0};
uint2bitAry(raw_z1, low_bits);
uint2bitAry(raw_z2, high_bits);
int negative_flag = high_bits[7];
int raw_acceleration = negative_flag<<15;
if(negative_flag) raw_acceleration >>= 6;
byte value_ary[9] = {low_bits[6], low_bits[7], high_bits[0], high_bits[1], high_bits[2], high_bits[3], high_bits[4], high_bits[5], high_bits[6]};
int acceleration_value = bitarray2int(value_ary, 9);
raw_acceleration |= acceleration_value;
//Serial.print(raw_acceleration);
return raw_acceleration;
}
void uint2bitAry(unsigned int number, byte* ary) {
unsigned int original_number = number;
byte i = 0;
for (i = 0; i < 8; i++) {
number = original_number;
byte bit = (number & (1<<i)) >> i;
ary[i] = bit;
}
}
int bitarray2int(byte* ary, byte length) {
int number = 0;
byte i = 0;
for (i = 0; i < length; i++) {
if( ary[i] ) {
number += 1<<i;
}
}
return number;
}
byte accRead(byte address) {
byte val = 0x00;
Wire.beginTransmission(ACCELEROMETER);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(ACCELEROMETER, 1);
val = Wire.read();
Wire.endTransmission();
return val;
}
void accWrite(byte value) {
Wire.beginTransmission(ACCELEROMETER);
Wire.write(value);
Wire.endTransmission();
}
String padInt(int x, int pad) {
String strInt = String(x);
String str = "";
if (strInt.length() >= pad) {
return strInt;
}
for (int i=0; i < (pad-strInt.length()); i++) {
str += "0";
}
str += strInt;
return str;
}
String int2string(int x) {
// formats an integer as a string assuming x is in 1/100ths
String str = String(x);
int strLen = str.length();
if (strLen <= 2) {
str = "0." + str;
} else if (strLen <= 3) {
str = str.substring(0, 1) + "." + str.substring(1);
} else if (strLen <= 4) {
str = str.substring(0, 2) + "." + str.substring(2);
} else {
str = "-9999";
}
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment