Created
July 27, 2016 17:15
-
-
Save dwblair/2657ef9a34da0076c1770364c2ddbde9 to your computer and use it in GitHub Desktop.
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
/* | |
Copyright under GPL | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
This file reads data from a BMA020 3-achses acceleration sensor | |
and prints it via Serial-library over USB | |
*/ | |
#include <SPI.h> | |
#include<stdlib.h> | |
#include <SD.h> | |
//led | |
#define led 9 | |
#define chipSelect 7 | |
#define SDpower 6 | |
#include <Wire.h> | |
#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 | |
void setup() { | |
// initialize the serial communications: | |
Serial.begin(19200); | |
Wire.begin(); | |
//set acc sensor to update data with 1.5KHz | |
byte bma_settings = accRead(0x14); | |
accWrite((bma_settings|6)); | |
pinMode(SDpower,OUTPUT); | |
digitalWrite(SDpower,LOW); | |
delay(1000); | |
if (!SD.begin(chipSelect)) { | |
if (debug) Serial.println("Card failed, or not present"); | |
// indicate SD problem with fast blink | |
while(1) { | |
digitalWrite(led,HIGH); | |
delay(200); | |
digitalWrite(led,LOW); | |
delay(200); | |
} | |
} | |
} | |
void loop() { | |
// print the sensor values: | |
int x = printXVal(); | |
Serial.print("\t\t"); | |
int y = printYVal(); | |
Serial.print("\t\t"); | |
int z = printZVal(); | |
Serial.print("\t\t"); | |
Serial.println(); | |
// delay before next reading: | |
delay(5); | |
String dataString = ""; | |
dataString += x; | |
dataString += " "; | |
dataString += y; | |
dataString += " "; | |
dataString += z; | |
if(debug) Serial.println(dataString); | |
// 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.csv", FILE_WRITE); | |
// if the file is available, write to it: | |
if (dataFile) { | |
dataFile.println(dataString); | |
dataFile.close(); | |
// print to the serial port too: | |
//indicate successful write with short blink | |
digitalWrite(led, HIGH); | |
delay(40); | |
digitalWrite(led, LOW); | |
} | |
// if the file isn't open, pop up an error: | |
else { | |
if (debug) Serial.println("error opening datalog.txt"); | |
} | |
delay(100); | |
} | |
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