Last active
August 24, 2019 11:33
-
-
Save tokyoff/e5c62493d41fe7bc4962fad9a89fad41 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
//------------------------------------ | |
// おみくじキーボード | |
// ※日本語入力ONの状態でキーを押すこと | |
// https://www.one-button-key.com/ | |
//------------------------------------ | |
#include "Keyboard.h" | |
#define PIN_KEYSW (9) | |
int prevKeyState; | |
int currKeyState; | |
int randNumber; | |
void setup() { | |
pinMode(PIN_KEYSW, INPUT_PULLUP); | |
prevKeyState = HIGH; | |
currKeyState = HIGH; | |
Keyboard.begin(); | |
// 乱数のseed設定 | |
randomSeed(analogRead(0)); | |
} | |
void loop() { | |
currKeyState = digitalRead(PIN_KEYSW); | |
// キースイッチが押された | |
if ((prevKeyState == HIGH) && (currKeyState == LOW)) { | |
// 乱数(0-11)の値に応じて運勢を出力 | |
// 0: 大吉 | |
// 1,2: 中吉 | |
// 3,4,5: 小吉 | |
// 6,7,8: 吉 | |
// 9,10: 凶 | |
// 11: 大凶 | |
randNumber = random(12); | |
if (0 == randNumber) { | |
Keyboard.print("daikichi"); | |
} | |
else if (2 >= randNumber) { | |
Keyboard.print("chuukichi"); | |
} | |
else if (5 >= randNumber) { | |
Keyboard.print("shoukichi"); | |
} | |
else if (8 >= randNumber) { | |
Keyboard.print("kichi"); | |
} | |
else if (10 >= randNumber) { | |
Keyboard.print("kyou"); | |
} | |
else { | |
Keyboard.print("daikyou"); | |
} | |
delay(10); // 10ms待つ | |
Keyboard.press(0x20); // 変換 | |
delay(10); // 10ms待つ | |
Keyboard.releaseAll(); // すべて放す | |
Keyboard.press(KEY_RETURN); // リターン | |
delay(10); // 10ms待つ | |
Keyboard.releaseAll(); // すべて放す | |
Keyboard.press(KEY_RETURN); // リターン | |
delay(10); // 10ms待つ | |
Keyboard.releaseAll(); // すべて放す | |
} | |
prevKeyState = currKeyState; | |
delay(10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment