LoginSignup
1
0

More than 3 years have passed since last update.

加速度をもとに動作するキーボードを作りました!

Last updated at Posted at 2019-12-22

加速度をもとに動作するキーボードを作りました!

はじめに

今この記事を作り始めたのは23時なのですが
東京高専プロコンゼミ② Advent Calendar 2019が自分は明日ということで眠い中記事を書いています…

はじめは、”アドベントのネタ… そうだ!筋肉:muscle:で動くキーボード作ろう!”と思っていてアリエクスプレスから筋電センサーを注文して、
おとといにぐらいに部品がきたのですが、
IMG_20191222_225524.jpg

あれ? なんかうまく動かないぞ??

となったので、急遽テーマを変えることを余儀なくされ、適当に持っていた3軸の加速度センサーで某リングフィット○○~みたいな感じで、
運動できるキーボード(?)を作ることにしました。

 使ったもの

  • ProMicro
  • ADXL345(3軸加速度センサー
  • 適当なスイッチ(モード選択用

 完成品

名前フィットステッキ
IMG_20191222_232946.jpg

 作った機能

 100回棒を振ると "git push" する

なんというか…はい。
IMG_20191222_235619.jpg
振るとこんな感じにメーターがたまっていって…
100回振ると
IMG_20191222_235605.jpg
メーターが消えて"git push"されます。
(Git環境がなかったですごめんなさい)

 棒を左右前後にスナップさせるとその方向に画面(ブラウザなど)が動く

意外と便利かも(怠惰むけ)
IMG_20191222_233910.jpg
こんな感じに持って動かしたい方向にスナップさせるだけでいい感じにブラウザをスクロールしたりできるよ:thinking:

こーど

ゴミコードでごめんなのだ:hamster:

advent.ino
#include <Wire.h>  // Wire library - used for I2C communication
#include "Keyboard.h"
int Bstate = 0, input = 0;
int ADXL345 = 0x53; // The ADXL345 sensor I2C address
float X_out, Y_out, Z_out;  // Outputs
#define SWITCH 9
int count = 0, noise = 30;
float tmp = 100;


void setup() {
  pinMode(9, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  Serial.begin(9600); // Initiate serial communication for printing the results on the Serial monitor
  Wire.begin(); // Initiate the Wire library
  // Set ADXL345 in measuring mode
  Wire.beginTransmission(ADXL345); // Start communicating with the device
  Wire.write(0x2D); // Access/ talk to POWER_CTL Register - 0x2D
  // Enable measurement
  Wire.write(8); // (8dec -> 0000 1000 binary) Bit D3 High for measuring enable
  Wire.endTransmission();
  delay(10);
}


void loop() {
  Wire.beginTransmission(ADXL345);
  Wire.write(0x32); // Start with register 0x32 (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(ADXL345, 6, true); // Read 6 registers total, each axis value is stored in 2 registers
  X_out = ( Wire.read() | Wire.read() << 8); // X-axis value
  X_out = X_out / 256; //For a range of +-2g, we need to divide the raw values by 256, according to the datasheet
  Y_out = ( Wire.read() | Wire.read() << 8); // Y-axis value
  Y_out = Y_out / 256;
  Z_out = ( Wire.read() | Wire.read() << 8); // Z-axis value
  Z_out = Z_out / 256;

  for (int i = 9; i >= 7; i--) {
    input = digitalRead(i);
    if (!input) {
      Bstate = i;
      break;
    }
    input = 0;
  }


  if (Bstate == 0) {
    Serial.print("Xa= ");
    Serial.print(X_out);
    Serial.print("   Ya= ");
    Serial.print(Y_out);
    Serial.print("   Za= ");
    Serial.println(Z_out);
    Keyboard.releaseAll();
  } else if (Bstate == 9) {

    if (tmp >= 0) {
      if (Z_out < -1.5) {
        Serial.print("#");
        tmp = Z_out;
        count++;
        if (count % 10 == 0 && count != 0) {
          Keyboard.write('#');
        }
      }
    } else {
      if (Z_out > 1.5) {
        Serial.print("#");
        tmp = Z_out;
        count++;
        if (count % 10 == 0 && count != 0) {
          Keyboard.write('#');
        }
      }
    }

    if (count == 100) {
      Serial.println();
      Serial.println("good");
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.write('a');
      delay(100);
      Keyboard.releaseAll();
      delay(50);
      Keyboard.write(KEY_DELETE);
      delay(100);
      Keyboard.print("git push");
      Keyboard.write(KEY_RETURN);
      count = 0;
    }
  } else if (Bstate == 8) {
    Serial.println("aiueo");
  } else if (Bstate == 7) {
    if (noise > 20) {
      Serial.println(tmp);
      if (Y_out > 1.8) {
        if (tmp != 0) {
          Keyboard.releaseAll();
          tmp = 0;
          noise = 5;
        } else {
          Keyboard.releaseAll();
          Serial.println("back");
          Keyboard.press(KEY_DOWN_ARROW);
          noise = 0;
          tmp = 1;
        }
      } else if (Y_out < -1.8) {
        if (tmp != 0) {
          Keyboard.releaseAll();
          tmp = 0;
          noise = 5;
        } else {
          Keyboard.releaseAll();
          Serial.println("front");
          Keyboard.press(KEY_UP_ARROW);
          noise = 0;
          tmp = 2;
        }
      } else if (Z_out > 1.9) {
        if (tmp != 0) {
          Keyboard.releaseAll();
          tmp = 0;
          noise = 5;
        } else {
          Keyboard.releaseAll();
          Serial.println("left");
          Keyboard.press(KEY_LEFT_ARROW);
          noise = 5;
          tmp = 3;
        }
      } else if (Z_out < -1.9) {
        if (tmp != 0) {
          Keyboard.releaseAll();
          tmp = 0;
          noise = 5;
        } else {
          Keyboard.releaseAll();
          Serial.println("right");
          Keyboard.press(KEY_RIGHT_ARROW);
          noise = 5;
          tmp = 4;
        }
      }
    } else {
      noise++;
    }

  }
  Bstate = 0;
  delay(10);
}

 最後に

ほかにも機能追加したかったけど、もう15分で日付が変わってしまうので諦めました…

追記

あと10秒のところで投稿できました…

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0