|
スポンサーリンク 加速度センサ KXR94-2050 †
Arduinoとの接続 †
サンプルコード †
/*
・Kionixの3軸加速度センサモジュール KXR94-2050 のサンプルプログラムです。
・50ms秒ごとに測定し、シリアルモニタに出力します。
・マイコンボードに書き込み後、「ツール」→「シリアルモニタ」を起動してください。
・Arduinoとの接続
1 Vdd 5V
2 Enable 5V
3 GND GND
4 Vmux
5 Self Test GND
6 Out X 任意のアナログ入力(A0~A5)
7 Out Y 任意のアナログ入力(A0~A5)
8 Out Z 任意のアナログ入力(A0~A5)
*/
const int analogInPinX = A0; // X軸アナログ入力ピン(定数)
const int analogInPinY = A1; // Y軸アナログ入力ピン(定数)
const int analogInPinZ = A2; // Z軸アナログ入力ピン(定数)
void setup()
{
Serial.begin(9600); // シリアルモニタを開始
}
void loop()
{
int i;
long x = 0, y = 0, z = 0; // AD値
for (i=0; i < 50; i++) { // 値を50回取得し、平均化する
x += analogRead(analogInPinX) ; // X軸
y += analogRead(analogInPinY) ; // Y軸
z += analogRead(analogInPinZ) ; // Z軸
}
x /= 50 ;
y /= 50 ;
z /= 50 ;
int rotateX = (x-277)/2.48 - 90; //角度を求める式
int rotateY = (y-277)/2.48 - 90;
int rotateZ = (z-277)/2.48 - 90;
Serial.print("X:") ;
Serial.print(x) ;
Serial.print(", ") ;
Serial.print(rotateX) ;
Serial.print(" Y:") ;
Serial.print(y) ;
Serial.print(", ") ;
Serial.print(rotateY) ;
Serial.print(" Z:") ;
Serial.print(z) ;
Serial.print(", ") ;
Serial.println(rotateZ) ;
delay(50) ;
}
実行例 †参考資料 †Total:14061 / Today:1 / Yesterday:3 スポンサーリンク |