gada888 发表于 2019-11-22 16:48:59

4x4键盘边播放音乐边开关12v电磁锁

吃灰很久的4x4键盘,终于有了用途了。就从标题所示的功能开始吧。先看下视频。https://v.youku.com/v_show/id_XNDQ0NDY1MTYyOA==.html?spm=a2hzp.8244740.0.0

按A键4下开锁并播放音乐,B键4下关锁,并播另一个曲子

项目用的硬件有:

arduino uno

4x4键盘

mp3模块

12v电磁锁

5v继电器

4x4键盘内部构造

连线图
//you may copy or modify the code below,but please refer gada888 as the original source
#include <Keypad.h>
char myNum;
int i;
unsigned char order = {0xAA,0x06,0x00,0xB0};
#define RELAY 10

const byte ROWS = 4;
const byte COLS = 4;
char keys = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins = {9,8,7,6};
byte colPins = {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=='A')
{
   play(0x01);
   //delay(500);
   digitalWrite(RELAY, HIGH);
   }
if(myNum=='B')
{
    play(0x02);
    //delay(500);
    digitalWrite(RELAY, LOW);
    i=0;
}
if(myNum=='*')
{
    play(0x03);
    delay(500);
    i=0;
}
if(myNum=='#')
{
    play(0x04);
    delay(500);
    i=0;
}
if(myNum=='D')
{
    play(0x05);
    delay(500);
    i=0;
}
}

void play(unsigned char Track)
{
unsigned char play = {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 = {0xAA,0x13,0x01,vol,vol+0xBE};//0xBE=0xAA+0x13+0x01,閸楄櫕娓堕崥搴濈娴e秳璐熼弽锟犵崣閸?
Serial.write(volume,5);
}




hnyzcj 发表于 2020-7-5 12:16:06

8错的帖子

hnyzcj 发表于 2020-7-5 12:17:14

为啥要按4下捏
页: [1]
查看完整版本: 4x4键盘边播放音乐边开关12v电磁锁