Created
January 3, 2014 06:03
-
-
Save eccyan/8233596 to your computer and use it in GitHub Desktop.
Play sound with the serial communication Arduino
This file contains hidden or 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
#define BUTTON_PIN 2 | |
#define SPEAKER_PIN 3 | |
#define CONTROLLER_PIN 9 | |
#define FREQUENCY 440 | |
#define DURATION 300 | |
#define COMMAND_LENGTH_MAX 10 | |
char current_command[COMMAND_LENGTH_MAX+1]; | |
int command_index = 0; | |
// コールバック型 | |
// TODO: シーン管理等で使う | |
typedef void(*callback_t)(void); | |
// ボタン操作関数 | |
//// | |
// ボタンが押されているか | |
boolean hasPressedButton() { | |
return digitalRead(BUTTON_PIN) == HIGH; | |
} | |
// ボタンが離されているか | |
boolean hasReleasedButton() { | |
return digitalRead(BUTTON_PIN) == LOW; | |
} | |
// ボタンが押されるのを待つ | |
void waitForPressButton() { | |
while (hasPressedButton()) { | |
; | |
} | |
} | |
// ボタンが離されるのを待つ | |
void waitForReleaseButton() { | |
while (hasReleasedButton()) { | |
; | |
} | |
} | |
// ボタンが押して離されるのを待つ | |
void waitForClickButton() { | |
waitForPressButton(); | |
waitForReleaseButton(); | |
} | |
// サウンド関数 | |
//// | |
// サウンドを鳴らす | |
void playSound(unsigned long int frequency, unsigned long int duration) { | |
tone(SPEAKER_PIN, frequency, duration); | |
delay(duration); | |
} | |
// コマンド関数 | |
//// | |
// コマンドを読み込む | |
void readCommand() { | |
if ( Serial.available() ) { | |
int ch = -1; | |
while (command_index < COMMAND_LENGTH_MAX ) { | |
ch = Serial.read(); | |
if (ch == -1) continue; | |
current_command[command_index] = ch; | |
if (ch == ';') { | |
current_command[command_index] = '\0'; | |
command_index = 0; | |
break; | |
} | |
++command_index; | |
} | |
} | |
} | |
// コマンドから周波数を取得する | |
// 読み込めない場合は 0 が返る | |
unsigned long int frequencyFromCommand(char const * const command) { | |
if (!command) return 0; | |
if (strlen(command) <= 0) return 0; | |
unsigned long int frequency = 0; | |
int index = 0; | |
char ch = 0; | |
char frequency_string[COMMAND_LENGTH_MAX+1]; | |
while (index < COMMAND_LENGTH_MAX ) { | |
ch = command[index]; | |
if (ch == ',') { | |
frequency_string[index] = '\0'; | |
frequency = atoi((char *)frequency_string); | |
break; | |
} | |
frequency_string[index] = ch; | |
++index; | |
} | |
return frequency; | |
} | |
// コマンドから長さを取得する | |
// 読み込めない場合は 0 が返る | |
unsigned long int durationFromCommand(char const * const command) { | |
if (!command) return 0; | |
if (strlen(command) <= 0) return 0; | |
unsigned long int duration = 0; | |
char duration_string[COMMAND_LENGTH_MAX+1]; | |
int index; | |
int offset; | |
char ch; | |
// カンマ後までインデックスを進める | |
offset = 0; | |
ch = 0; | |
while (offset < COMMAND_LENGTH_MAX) { | |
ch = command[offset]; | |
if (ch == ',') { | |
++offset; | |
break; | |
} | |
++offset; | |
} | |
index = 0; | |
while (index < COMMAND_LENGTH_MAX) { | |
ch = command[offset + index]; | |
if (ch == '\0') { | |
duration_string[index] = '\0'; | |
duration = atoi((char *)duration_string); | |
break; | |
} | |
duration_string[index] = ch; | |
++index; | |
} | |
return duration; | |
} | |
// シーン関数 | |
//// | |
// 初期化 | |
void setup() { | |
// スピーカー用PINの初期化 | |
pinMode(SPEAKER_PIN, OUTPUT); | |
// ボタン用PINの初期化 | |
pinMode(BUTTON_PIN, INPUT_PULLUP); | |
// シリアル通信を9600ポートで開始 | |
Serial.begin(9600); | |
} | |
// ループ | |
void loop() { | |
readCommand(); | |
if (strlen(current_command) > 0) { | |
Serial.print("Command = "); | |
Serial.println((char *)current_command); | |
unsigned long int frequency = frequencyFromCommand(current_command); | |
unsigned long int duration = durationFromCommand(current_command); | |
if (frequency && duration) { | |
playSound(frequency, duration); | |
} | |
memset(¤t_command, 0, COMMAND_LENGTH_MAX); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment