2693| 2
|
[项目] 4x4键盘边播放音乐边开关12v电磁锁 |
吃灰很久的4x4键盘,终于有了用途了。就从标题所示的功能开始吧。先看下视频。 按A键4下开锁并播放音乐,B键4下关锁,并播另一个曲子 项目用的硬件有: Arduino uno 4x4键盘 mp3模块 12v电磁锁 5v继电器 4x4键盘内部构造 连线图 [mw_shl_code=applescript,true]//you may copy or modify the code below,but please refer gada888 as the original source #include <Keypad.h> char myNum[4]; int i; unsigned char order[4] = {0xAA,0x06,0x00,0xB0}; #define RELAY 10 const byte ROWS = 4; const byte COLS = 4; char keys[ROWS][COLS] = { {'1','2','3','A'}, {'4','5','6','B'}, {'7','8','9','C'}, {'*','0','#','D'} }; byte rowPins[ROWS] = {9,8,7,6}; byte colPins[COLS] = {5,4,3,2}; Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); void setup() { Serial.begin(9600); volume(0x1E); pinMode(RELAY, OUTPUT); digitalWrite(RELAY, HIGH); } void loop() { Serial.println("Enter a num -"); for ( i = 0; i < 4; ++i) { while((myNum = keypad.getKey())==NO_KEY) { delay(1); // Just wait for a key } while(keypad.getKey() != NO_KEY) { delay(1); } Serial.print(myNum); } if(myNum[3]=='A') { play(0x01); //delay(500); digitalWrite(RELAY, HIGH); } if(myNum[3]=='B') { play(0x02); //delay(500); digitalWrite(RELAY, LOW); i=0; } if(myNum[3]=='*') { play(0x03); delay(500); i=0; } if(myNum[3]=='#') { play(0x04); delay(500); i=0; } if(myNum[3]=='D') { play(0x05); delay(500); i=0; } } void play(unsigned char Track) { unsigned char play[6] = {0xAA,0x07,0x02,0x00,Track,Track+0xB3};//0xB3=0xAA+0x07+0x02+0x00,閸楄櫕娓堕崥搴濈娴e秳璐熼弽锟犵崣閸? Serial.write(play,6); } void volume( unsigned char vol) { unsigned char volume[5] = {0xAA,0x13,0x01,vol,vol+0xBE};//0xBE=0xAA+0x13+0x01,閸楄櫕娓堕崥搴濈娴e秳璐熼弽锟犵崣閸? Serial.write(volume,5); } [/mw_shl_code] |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed