가속도를 바탕으로 동작하는 키보드를 만들었습니다!
6743 단어 AdventCalendarArduinoProMicro
가속도를 바탕으로 동작하는 키보드를 만들었습니다!
소개
지금 이 기사를 만들기 시작한 것은 23시입니다만
도쿄 고전 프로콘세미 ② Advent Calendar 2019 내가 내일에 졸린 동안 기사를 쓰고 있습니다 ...
처음에는 "어드벤트의 재료 ... 그렇다! 근육
에서 움직이는 키보드 만들자!"라고 생각하고 의 Aliexpress
어쩌면 정도로 부품이 왔습니다만,

어라? 뭔가 잘 움직이지 않아? ?
가 되었으므로, 급거 테마를 바꾸는 것을 강요당하고, 적당히 가지고 있던 3축의 가속도 센서로 모 링 피트 ○○~같은 느낌으로,
운동할 수 있는 키보드(?)를 만들기로 했습니다.
사용한 것
완성품
이름 맞는 스테크

만든 기능
100회 막대기를 흔들면 "git push" 한다
뭐라고 할까… 네.

흔들면 이런 느낌에 미터가 쌓여 가서…
100회 흔들면

미터가 사라지고 "git push"됩니다.
(Git 환경이 없어서 미안해)
막대를 좌우 전후에 스냅시키면 그 방향으로 화면(브라우저 등)이 움직인다
의외로 편리할지도 (게으름)

이런 식으로 가지고 움직이고 싶은 방향으로 스냅시키는 것만으로 좋은 느낌으로 브라우저를 스크롤 하거나 할 수 있어

코드
쓰레기 코드로 죄송합니다

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초 후에 게시할 수 있었습니다…
Reference
이 문제에 관하여(가속도를 바탕으로 동작하는 키보드를 만들었습니다!), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/maki_maki_art/items/1ca10d725d4e01a9b3b8텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)