Created
July 6, 2013 02:07
-
-
Save creationix/5938328 to your computer and use it in GitHub Desktop.
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
// Reads accel data and logs to serial. then flashes lights. When x is level, it goes faster. | |
#include <Wire.h> | |
#define BMA250_I2CADDR 0x18 | |
#define BMA250_RANGE 0x03 // 0x03 = 2g, 0x05 = 4g, 0x08 = 8g, 0x0C = 16g | |
#define BMA250_BW 0x08 // 7.81Hz (update time of 64ms) | |
int AccelX; | |
int AccelY; | |
int AccelZ; | |
float AccelTemperature; | |
void setup() | |
{ | |
Wire.begin(); | |
Serial.begin(9600); | |
BMA250Init(); | |
pinMode(5, INPUT); | |
pinMode(6, INPUT); | |
pinMode(7, INPUT); | |
pinMode(8, INPUT); | |
pinMode(9, INPUT); | |
} | |
void loop() | |
{ | |
int byCount = 0; | |
int byDir = 0; | |
while( 1 ) | |
{ | |
BMA250ReadAccel(); | |
// Print out the accelerometer data | |
Serial.print("x: "); | |
Serial.print(AccelX); | |
Serial.print(", y: "); | |
Serial.print(AccelY); | |
Serial.print(", z:"); | |
Serial.print(AccelZ); | |
Serial.print(", t: "); | |
Serial.print(AccelTemperature); | |
Serial.println("degC"); | |
if( !byDir ) | |
{ | |
byCount++; | |
if( byCount >= 16 ) | |
{ | |
byDir = 1; | |
} | |
} | |
else | |
{ | |
byCount--; | |
if( byCount == 1 ) | |
{ | |
byDir = 0; | |
} | |
} | |
LedOn( byCount ); | |
delay( abs(AccelX) + 1 ); | |
} | |
} | |
void LedOn( int iLedNum ) | |
{ | |
if( iLedNum == 1 ) | |
{ | |
digitalWrite( 5, HIGH ); | |
digitalWrite( 6, LOW ); | |
pinMode(5, OUTPUT); | |
pinMode(6, OUTPUT); | |
pinMode(7, INPUT); | |
pinMode(8, INPUT); | |
pinMode(9, INPUT); | |
} | |
else if( iLedNum == 2 ) | |
{ | |
digitalWrite( 5, LOW ); | |
digitalWrite( 6, HIGH ); | |
pinMode(5, OUTPUT); | |
pinMode(6, OUTPUT); | |
pinMode(7, INPUT); | |
pinMode(8, INPUT); | |
pinMode(9, INPUT); | |
} | |
else if( iLedNum == 3 ) | |
{ | |
digitalWrite( 5, HIGH ); | |
digitalWrite( 7, LOW ); | |
pinMode(5, OUTPUT); | |
pinMode(6, INPUT); | |
pinMode(7, OUTPUT); | |
pinMode(8, INPUT); | |
pinMode(9, INPUT); | |
} | |
else if( iLedNum == 4 ) | |
{ | |
digitalWrite( 7, HIGH ); | |
digitalWrite( 5, LOW ); | |
pinMode(5, OUTPUT); | |
pinMode(6, INPUT); | |
pinMode(7, OUTPUT); | |
pinMode(8, INPUT); | |
pinMode(9, INPUT); | |
} | |
else if( iLedNum == 5 ) | |
{ | |
digitalWrite( 6, HIGH ); | |
digitalWrite( 7, LOW ); | |
pinMode(5, INPUT); | |
pinMode(6, OUTPUT); | |
pinMode(7, OUTPUT); | |
pinMode(8, INPUT); | |
pinMode(9, INPUT); | |
} | |
else if( iLedNum == 6 ) | |
{ | |
digitalWrite( 7, HIGH ); | |
digitalWrite( 6, LOW ); | |
pinMode(5, INPUT); | |
pinMode(6, OUTPUT); | |
pinMode(7, OUTPUT); | |
pinMode(8, INPUT); | |
pinMode(9, INPUT); | |
} | |
else if( iLedNum == 7 ) | |
{ | |
digitalWrite( 6, HIGH ); | |
digitalWrite( 8, LOW ); | |
pinMode(5, INPUT); | |
pinMode(6, OUTPUT); | |
pinMode(7, INPUT); | |
pinMode(8, OUTPUT); | |
pinMode(9, INPUT); | |
} | |
else if( iLedNum == 8 ) | |
{ | |
digitalWrite( 8, HIGH ); | |
digitalWrite( 6, LOW ); | |
pinMode(5, INPUT); | |
pinMode(6, OUTPUT); | |
pinMode(7, INPUT); | |
pinMode(8, OUTPUT); | |
pinMode(9, INPUT); | |
} | |
else if( iLedNum == 9 ) | |
{ | |
digitalWrite( 5, HIGH ); | |
digitalWrite( 8, LOW ); | |
pinMode(5, OUTPUT); | |
pinMode(6, INPUT); | |
pinMode(7, INPUT); | |
pinMode(8, OUTPUT); | |
pinMode(9, INPUT); | |
} | |
else if( iLedNum == 10 ) | |
{ | |
digitalWrite( 8, HIGH ); | |
digitalWrite( 5, LOW ); | |
pinMode(5, OUTPUT); | |
pinMode(6, INPUT); | |
pinMode(7, INPUT); | |
pinMode(8, OUTPUT); | |
pinMode(9, INPUT); | |
} | |
else if( iLedNum == 11 ) | |
{ | |
digitalWrite( 8, HIGH ); | |
digitalWrite( 7, LOW ); | |
pinMode(5, INPUT); | |
pinMode(6, INPUT); | |
pinMode(7, OUTPUT); | |
pinMode(8, OUTPUT); | |
pinMode(9, INPUT); | |
} | |
else if( iLedNum == 12 ) | |
{ | |
digitalWrite( 7, HIGH ); | |
digitalWrite( 8, LOW ); | |
pinMode(5, INPUT); | |
pinMode(6, INPUT); | |
pinMode(7, OUTPUT); | |
pinMode(8, OUTPUT); | |
pinMode(9, INPUT); | |
} | |
else if( iLedNum == 13 ) | |
{ | |
digitalWrite( 9, HIGH ); | |
digitalWrite( 7, LOW ); | |
pinMode(5, INPUT); | |
pinMode(6, INPUT); | |
pinMode(7, OUTPUT); | |
pinMode(8, INPUT); | |
pinMode(9, OUTPUT); | |
} | |
else if( iLedNum == 14 ) | |
{ | |
digitalWrite( 7, HIGH ); | |
digitalWrite( 9, LOW ); | |
pinMode(5, INPUT); | |
pinMode(6, INPUT); | |
pinMode(7, OUTPUT); | |
pinMode(8, INPUT); | |
pinMode(9, OUTPUT); | |
} | |
else if( iLedNum == 15 ) | |
{ | |
digitalWrite( 9, HIGH ); | |
digitalWrite( 8, LOW ); | |
pinMode(5, INPUT); | |
pinMode(6, INPUT); | |
pinMode(7, INPUT); | |
pinMode(8, OUTPUT); | |
pinMode(9, OUTPUT); | |
} | |
else if( iLedNum == 16 ) | |
{ | |
digitalWrite( 8, HIGH ); | |
digitalWrite( 9, LOW ); | |
pinMode(5, INPUT); | |
pinMode(6, INPUT); | |
pinMode(7, INPUT); | |
pinMode(8, OUTPUT); | |
pinMode(9, OUTPUT); | |
} | |
} | |
void BMA250Init() | |
{ | |
// Setup the range measurement setting | |
Wire.beginTransmission(BMA250_I2CADDR); | |
Wire.write(0x0F); | |
Wire.write(BMA250_RANGE); | |
Wire.endTransmission(); | |
// Setup the bandwidth | |
Wire.beginTransmission(BMA250_I2CADDR); | |
Wire.write(0x10); | |
Wire.write(BMA250_BW); | |
Wire.endTransmission(); | |
} | |
int BMA250ReadAccel() | |
{ | |
uint8_t ReadBuff[8]; | |
// Read the 7 data bytes from the BMA250 | |
Wire.beginTransmission(BMA250_I2CADDR); | |
Wire.write(0x02); | |
Wire.endTransmission(); | |
Wire.requestFrom(BMA250_I2CADDR,7); | |
for(int i = 0; i < 7;i++) | |
{ | |
ReadBuff[i] = Wire.read(); | |
} | |
AccelX = ReadBuff[1] << 8; | |
AccelX |= ReadBuff[0]; | |
AccelX >>= 6; | |
AccelY = ReadBuff[3] << 8; | |
AccelY |= ReadBuff[2]; | |
AccelY >>= 6; | |
AccelZ = ReadBuff[5] << 8; | |
AccelZ |= ReadBuff[4]; | |
AccelZ >>= 6; | |
AccelTemperature = (ReadBuff[6] * 0.5) + 24.0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment