Created
January 30, 2014 14:15
-
-
Save eccyan/8709265 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
#include <SPI.h> | |
const int MINIMUM = 1000; | |
const int X_MINUS_PIN = 7; | |
const int X_PLUS_PIN = 6; | |
const int Y_MINUS_PIN = 5; | |
const int Y_PLUS_PIN = 4; | |
const int Z_MINUS_PIN = 3; | |
const int Z_PLUS_PIN = 2; | |
// LEDを消す | |
void turnOffLED(int pin) { | |
digitalWrite(pin, LOW); | |
pinMode(pin, INPUT); | |
} | |
// LEDを付ける | |
void turnOnLED(int pin) { | |
pinMode(pin, OUTPUT); | |
digitalWrite(pin, HIGH); | |
} | |
//Register Addresses: | |
const int OUT_X_L = 0x28; //X-axis acceleration data | |
const int OUT_X_H = 0x29; | |
const int OUT_Y_L = 0x2A; //Y-axis acceleration data | |
const int OUT_Y_H = 0x2B; | |
const int OUT_Z_L = 0x2C; //Z-axis acceleration data | |
const int OUT_Z_H = 0x2D; | |
const int CTRL_REG1 = 0x20; //Register that can enable,disable axes | |
const int CTRL_REG2 = 0x21; | |
const int CTRL_REG3 = 0x22; | |
const int CTRL_REG4 = 0x23; | |
const int CTRL_REG5 = 0x24; | |
const int CTRL_REG6 = 0x25; | |
const int WHO_AM_I = 0x0F; //Dummy register | |
const byte READ = 0b10000000; // LIS3DH's read command | |
const byte WRITE = 0b00111111; // LIS3DH's write command | |
const byte TEMP_CFG_REG = 0x1F; //Temperature sensor | |
//Axis definitions | |
int x_axis = 1; | |
int y_axis = 2; | |
int z_axis = 3; | |
//Arduino ss pin10 | |
const int ss = 10; | |
int old_x = 0; | |
int old_y = 0; | |
int old_z = 0; | |
int old_diff_x = 0; | |
int old_diff_y = 0; | |
int old_diff_z = 0; | |
void setup() { | |
pinMode(ss, OUTPUT); // we use this for the SS at pin 10 | |
SPI.begin(); //wake up the SPI bus | |
SPI.setBitOrder(MSBFIRST); | |
SPI.setDataMode(SPI_MODE3); | |
SPI.setClockDivider(SPI_CLOCK_DIV2); | |
Serial.begin(115200); | |
writeRegister(CTRL_REG1, 0x47); | |
if (readRegister(WHO_AM_I) == 0x33) { | |
Serial.println('### OK'); | |
} | |
turnOnLED(X_PLUS_PIN);turnOnLED(X_MINUS_PIN); | |
turnOnLED(Y_PLUS_PIN);turnOnLED(Y_MINUS_PIN); | |
delay(100); | |
turnOffLED(X_PLUS_PIN);turnOffLED(X_MINUS_PIN); | |
turnOffLED(Y_PLUS_PIN);turnOffLED(Y_MINUS_PIN); | |
} | |
int getValue(int axis) { | |
int Val = 0; | |
int h = 0,l = 0; | |
if (axis == 1) { | |
l = readRegister(OUT_X_L); | |
h = readRegister(OUT_X_H); | |
} else if (axis == 2) { | |
l = readRegister(OUT_Y_L); | |
h = readRegister(OUT_Y_H); | |
} else if (axis == 3) { | |
l = readRegister(OUT_Z_L); | |
h = readRegister(OUT_Z_H); | |
} | |
Val = ((h << 8) | l); | |
return Val; | |
} | |
unsigned int readRegister(byte thisRegister) { | |
unsigned int result = 0; // result to return | |
byte dataToSend = thisRegister | READ; | |
digitalWrite(ss, LOW); | |
SPI.transfer(dataToSend); | |
result = SPI.transfer(0x00); | |
digitalWrite(ss, HIGH); | |
return result; | |
} | |
void writeRegister(byte thisRegister, byte thisValue) { | |
byte dataToSend = thisRegister & WRITE; | |
digitalWrite(ss,LOW); | |
SPI.transfer(dataToSend); | |
SPI.transfer(thisValue); | |
digitalWrite(ss,HIGH); | |
} | |
void loop () { | |
turnOffLED(X_PLUS_PIN); turnOffLED(X_MINUS_PIN); | |
turnOffLED(Y_PLUS_PIN); turnOffLED(Y_MINUS_PIN); | |
int diff_x = old_x - getValue(x_axis); | |
if (diff_x > MINIMUM || diff_x < -MINIMUM) { | |
if (diff_x < 0) { | |
if (old_diff_x < 0) { | |
turnOnLED(X_MINUS_PIN); | |
} | |
} | |
else { | |
if (old_diff_x >= 0) { | |
turnOnLED(X_PLUS_PIN); | |
} | |
} | |
Serial.print(" x = "); | |
Serial.print(diff_x); | |
Serial.println(); | |
old_diff_x = diff_x; | |
} | |
old_x = getValue(x_axis); | |
int diff_y = old_y - getValue(y_axis); | |
if (diff_y > MINIMUM || diff_y < -MINIMUM) { | |
if (diff_y < 0) { | |
if (old_diff_y < 0) { | |
turnOnLED(Y_MINUS_PIN); | |
} | |
} | |
else { | |
if (old_diff_y >= 0) { | |
turnOnLED(Y_PLUS_PIN); | |
} | |
} | |
Serial.print(" y = "); | |
Serial.print(diff_y); | |
Serial.println(); | |
old_diff_y = diff_y; | |
} | |
old_y = getValue(y_axis); | |
int diff_z = old_z - getValue(z_axis); | |
if (diff_z > MINIMUM || diff_z < -MINIMUM) { | |
if (diff_z < 0) { | |
if (old_diff_z < 0) { | |
turnOnLED(Z_MINUS_PIN); | |
} | |
} | |
else { | |
if (old_diff_z >= 0) { | |
turnOnLED(Z_PLUS_PIN); | |
} | |
} | |
Serial.print(" z = "); | |
Serial.print(diff_z); | |
Serial.println(); | |
old_diff_z = diff_z; | |
} | |
old_z = getValue(z_axis); | |
delay(100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment