矩阵键盘从左到右分别连接gpio13,gpio12,gpio14,gpio27,gpio26,gpio25,gpio33,gpio32,无源蜂鸣器接gpio15,键盘从左到右从上到下分别为do,升do,re,升re,mi,fa,升fa,sol,升sol,la,升la,ti,升高一个八度,降低一个八度,回到中声(第四八度),调整板载led状态。每个音持续250毫秒,总共1-7 7个八度
#include
const int row1=13,row2=12,row3=14,row4=27,col1=26,col2=25,col3=33,col4=32,led=2,bp=15,channel=0;
const byte rows=4,cols=4;
bool p=1;
uint8_t y=4;
uint16_t x=250;
char hk[rows][cols]=
{
{'d','D','r','R'},
{'m','f','F','s'},
{'S','a','A','t'},
{'+','-','4','l'}
};
byte rowpines[rows]={row1,row2,row3,row4};
byte colpines[cols]={col1,col2,col3,col4};
Keypad ck=Keypad(makeKeymap(hk),rowpines,colpines,rows,cols);
void setup()
{
// put your setup code here, to run once:
for(int i=0;i1)
{
y--;
}
break;
}
case '4' :
{
y=4;
break;
}
}
}
}
#include
const int row1=13,row2=12,row3=14,row4=27,col1=26,col2=25,col3=33,col4=32,led=2,bp=15,channel=0;
const byte rows=4,cols=4;
bool p=1;
uint8_t y=4;
uint16_t x=250;
char hk[rows][cols]=
{
{'d','D','r','R'},
{'m','f','F','s'},
{'S','a','A','t'},
{'+','-','4','l'}
};
byte rowpines[rows]={row1,row2,row3,row4};
byte colpines[cols]={col1,col2,col3,col4};
Keypad ck=Keypad(makeKeymap(hk),rowpines,colpines,rows,cols);
void setup()
{
// put your setup code here, to run once:
for(int i=0;i { pinMode(rowpines[i],OUTPUT); pinMode(colpines[i],OUTPUT); } pinMode(led,OUTPUT); ledcSetup(channel,20000,10); ledcAttachPin(bp,channel); Serial.begin(921600); } void loop() { // put your main code here, to run repeatedly: char cK=ck.getKey(); if(cK) { Serial.println(cK); switch(cK) { case 'l' : { digitalWrite(led,int(p)); p=!p; break; } case 'd' : { ledcWriteNote(0,NOTE_C,y); delay(x); ledcWriteTone(channel,0); break; } case 'D' : { ledcWriteNote(0,NOTE_Cs,y); delay(x); ledcWriteTone(channel,0); break; } case 'r' : { ledcWriteNote(0,NOTE_D,y); delay(x); ledcWriteTone(channel,0); break; } case 'R' : { ledcWriteNote(0,NOTE_Eb,y); delay(x); ledcWriteTone(channel,0); break; } case 'm' : { ledcWriteNote(0,NOTE_E,y); delay(x); ledcWriteTone(channel,0); break; } case 'f' : { ledcWriteNote(0,NOTE_F,y); delay(x); ledcWriteTone(channel,0); break; } case 'F' : { ledcWriteNote(0,NOTE_Fs,y); delay(x); ledcWriteTone(channel,0); break; } case 's' : { ledcWriteNote(0,NOTE_G,y); delay(x); ledcWriteTone(channel,0); break; } case 'S' : { ledcWriteNote(0,NOTE_Gs,y); delay(x); ledcWriteTone(channel,0); break; } case 'a' : { ledcWriteNote(0,NOTE_A,y); delay(x); ledcWriteTone(channel,0); break; } case 'A' : { ledcWriteNote(0,NOTE_Bb,y); delay(x); ledcWriteTone(channel,0); break; } case 't' : { ledcWriteNote(0,NOTE_B,y); delay(x); ledcWriteTone(channel,0); break; } case '+' : { if(y<7) { y++; } break; } case '-' : { if(y>1) { y--; } break; } case '4' : { y=4; break; } } } }