一、项目说明

这里我们需要使用到RGB LED蜂鸣器按键开关这个元件。为什么要叫RGB呢?是因为这个LED是由红(Red)绿(Green)蓝(Blue)三种颜色组成。我们可以通过调整三个LED中每个灯的亮度从而产生不同的颜色。这里我们就可以通过代码让一个RGB小灯随机产生不同的炫彩颜色。另外,蜂鸣器播放音乐,RGB LED随着音乐播放节奏闪烁不同颜色。这里设置了一个按键开关,控制蜂鸣器及RGB灯的工作。

二、所需元件及连接

三、程序代码

#define NOTE_B0 31

#define NOTE_C1 33

#define NOTE_CS1 35

#define NOTE_D1 37

#define NOTE_DS1 39

#define NOTE_E1 41

#define NOTE_F1 44

#define NOTE_FS1 46

#define NOTE_G1 49

#define NOTE_GS1 52

#define NOTE_A1 55

#define NOTE_AS1 58

#define NOTE_B1 62

#define NOTE_C2 65

#define NOTE_CS2 69

#define NOTE_D2 73

#define NOTE_DS2 78

#define NOTE_E2 82

#define NOTE_F2 87

#define NOTE_FS2 93

#define NOTE_G2 98

#define NOTE_GS2 104

#define NOTE_A2 110

#define NOTE_AS2 117

#define NOTE_B2 123

#define NOTE_C3 131

#define NOTE_CS3 139

#define NOTE_D3 147

#define NOTE_DS3 156

#define NOTE_E3 165

#define NOTE_F3 175

#define NOTE_FS3 185

#define NOTE_G3 196

#define NOTE_GS3 208

#define NOTE_A3 220

#define NOTE_AS3 233

#define NOTE_B3 247

#define NOTE_C4 262

#define NOTE_CS4 277

#define NOTE_D4 294

#define NOTE_DS4 311

#define NOTE_E4 330

#define NOTE_F4 349

#define NOTE_FS4 370

#define NOTE_G4 392

#define NOTE_GS4 415

#define NOTE_A4 440

#define NOTE_AS4 466

#define NOTE_B4 494

#define NOTE_C5 523

#define NOTE_CS5 554

#define NOTE_D5 587

#define NOTE_DS5 622

#define NOTE_E5 659

#define NOTE_F5 698

#define NOTE_FS5 740

#define NOTE_G5 784

#define NOTE_GS5 831

#define NOTE_A5 880

#define NOTE_AS5 932

#define NOTE_B5 988

#define NOTE_C6 1047

#define NOTE_CS6 1109

#define NOTE_D6 1175

#define NOTE_DS6 1245

#define NOTE_E6 1319

#define NOTE_F6 1397

#define NOTE_FS6 1480

#define NOTE_G6 1568

#define NOTE_GS6 1661

#define NOTE_A6 1760

#define NOTE_AS6 1865

#define NOTE_B6 1976

#define NOTE_C7 2093

#define NOTE_CS7 2217

#define NOTE_D7 2349

#define NOTE_DS7 2489

#define NOTE_E7 2637

#define NOTE_F7 2794

#define NOTE_FS7 2960

#define NOTE_G7 3136

#define NOTE_GS7 3322

#define NOTE_A7 3520

#define NOTE_AS7 3729

#define NOTE_B7 3951

#define NOTE_C8 4186

#define NOTE_CS8 4435

#define NOTE_D8 4699

#define NOTE_DS8 4978

//选取音乐《卡农》

int tune[]=

{

NOTE_G5,NOTE_E5,NOTE_F5,NOTE_G5,NOTE_E5,NOTE_F5,

NOTE_G5,NOTE_G4,NOTE_A4,NOTE_B4,NOTE_C5,NOTE_D5,NOTE_E5,NOTE_F5,

NOTE_E5,NOTE_C5,NOTE_D5,NOTE_E5,NOTE_E4,NOTE_F4,

NOTE_G4,NOTE_A4,NOTE_G4,NOTE_F4,NOTE_G4,NOTE_E4,NOTE_F4,NOTE_G4,

NOTE_F4,NOTE_A4,NOTE_G4,NOTE_F4,NOTE_E4,NOTE_D4,

NOTE_E4,NOTE_D4,NOTE_C4,NOTE_D4,NOTE_E4,NOTE_F4,NOTE_G4,NOTE_A4,

NOTE_F4,NOTE_A4,NOTE_G4,NOTE_A4,NOTE_B4,NOTE_C5,

NOTE_G4,NOTE_A4,NOTE_B4,NOTE_C5,NOTE_D5,NOTE_E5,NOTE_F5,NOTE_G5

};

float durt[]=

{

1,0.5,0.5,1,0.5,0.5,

0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,

1,0.5,0.5,1,0.5,0.5,

0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,

1,0.5,0.5,1,0.5,0.5,

0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,

1,0.5,0.5,1,0.5,0.5,

0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5

};

int length;//音节长度

int tonepin=3;//蜂鸣器IO口,3脚

int a = 200;//延迟变量,控制时间

int Red = 9;//红色灯引脚

int Gre = 10;//绿色等引脚

int Ble = 11;//蓝色灯引脚

int i = 0;//变量i

int p;//变量p

int buttonPin=2;//按键

boolean ledState = false;//布尔值读取ledState的状态

void setup()//主程序

{

pinMode(tonepin,OUTPUT);

pinMode(Red,OUTPUT);

pinMode(Gre,OUTPUT);

pinMode(Ble,OUTPUT);

length=sizeof(tune)/sizeof(tune[0]);//音调长度

pinMode(buttonPin,INPUT_PULLUP);

}

void loop()//循环程序

{

for(int x=0;x<length;x++)

{

if(digitalRead(buttonPin) == LOW)//如果读取到按键被按压改变ledState

{

ledState = !ledState;

delay(500);//这个延迟是你按压按键的大概时间

p = x;//记录当前播放的音乐的位置

}

if (ledState == true) //如果ledState是这个状态就播放

{

tone(tonepin,tune[x]);//音调

RGB_14(i);//彩灯子程序跟随音乐的节奏变色闪烁

delay(400*durt[x]);//节拍进行延时

delay(100*durt[x]);//这里用来根据节拍调节延时,500这个指数可以自己调整,在该音乐中,我发现用500比较合适。

noTone(tonepin);

}else

{

x = p;//如果是暂停状态记录此时音乐的位置

}

}

delay(a);//延时

}

void RGB_7(int i)//彩灯程序,7种颜色

{

i = random(7);//这会随机选择一个介于0和6之间的数字

switch(i)

{

case 0://如果i等于0,则led会变成红色

analogWrite(Red,255);

analogWrite(Gre,0);

analogWrite(Ble,0);

break;

case 1://如果i等于1,则led将变为黄色

analogWrite(Red,255);

analogWrite(Gre,255);

analogWrite(Ble,0);

break;

case 2://如果i等于2,则led将变为蓝色

analogWrite(Red,131);

analogWrite(Gre,111);

analogWrite(Ble,255);

break;

case 3://如果i等于3,则led将变为橙色

analogWrite(Red,255);

analogWrite(Gre,161);

analogWrite(Ble,0);

break;

case 4://如果i等于4,则led将变成青色

analogWrite(Red,0);

analogWrite(Gre,255);

analogWrite(Ble,127);

break;

case 5://如果i等于5,则led将变为紫色

analogWrite(Red,255);

analogWrite(Gre,0);

analogWrite(Ble,255);

break;

case 6://如果i等于6,则led会变成淡紫色

analogWrite(Red,160);

analogWrite(Gre,32);

analogWrite(Ble,240);

break;

}

}

void RGB_14(int i)//彩灯子程序

{

if(i<=6)

RGB_7(i);

else

RGB_7(i-7);

}