○ライブラリのインストール
Grove – Serial MP3 Player V2.0のライブラリをダウンロードして、インストールします。
※このライブラリは基本的な機能のみサポートしています。
○回路・配線図
○プログラム(スケッチ)
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
#include <SoftwareSerial.h> | |
#include <MP3Player_KT403A.h> | |
SoftwareSerial mp3(2, 3); | |
uint8_t mode = 0; | |
bool start = 0; | |
bool pause = 0; | |
bool rep = 0; | |
int adc_key_val[5] ={100, 350, 500, 610, 900 }; | |
int NUM_KEYS = 5; | |
int adc_key_in; | |
int key=-1; | |
int oldkey=-1; | |
void setup() { | |
mp3.begin(9600); | |
Serial.begin(9600); | |
delay(100); | |
SelectPlayerDevice(0x02); // Select SD card as the player device. | |
SetVolume(0x0E); // Set the volume, the range is 0x00 to 0x1 | |
} | |
void loop() { | |
adc_key_in = analogRead(0); // read the value from the sensor | |
digitalWrite(13,LOW); | |
key = get_key(adc_key_in); // convert into key press | |
//Serial.println(adc_key_in); | |
if (key != oldkey) // if keypress is detected | |
{ | |
delay(50); // wait for debounce time | |
adc_key_in = analogRead(0); // read the value from the sensor | |
//Serial.println(adc_key_in); | |
key = get_key(adc_key_in); // convert into key press | |
if (key != oldkey) { | |
oldkey = key; | |
if (key >=0) { | |
digitalWrite(13,HIGH); | |
switch(key) | |
{ | |
case 0:Serial.println("S1 OK"); | |
PlayPrevious(); | |
break; | |
case 1:Serial.println("S2 OK"); | |
PlayNext(); | |
break; | |
case 2:Serial.println("S3 OK"); | |
DecreaseVolume(); | |
break; | |
case 3:Serial.println("S4 OK"); | |
IncreaseVolume(); | |
break; | |
case 4:Serial.println("S5 OK"); | |
if ( start == 0) { | |
SpecifyMusicPlay(1); | |
start = 1; | |
} | |
if ( start == 1) { | |
if (pause == 0){ | |
PlayPause(); | |
pause = 1; | |
} else { | |
PlayResume(); | |
pause = 0; | |
} | |
} | |
break; | |
} | |
} | |
} | |
} | |
delay(100); | |
} | |
// Convert ADC value to key number | |
int get_key(unsigned int input) { | |
int k; | |
for (k = 0; k < NUM_KEYS; k++) { | |
if (input < adc_key_val[k]) { | |
return k; | |
} | |
} | |
if (k >= NUM_KEYS)k = -1; // No valid key pressed | |
return k; | |
} |
0 件のコメント:
コメントを投稿