Arduino小项目设计(蜂鸣器与呼吸流水灯结合,以及led点阵)

这还是在禹州那边弄的,应该是在10月8日的时候,怀念一下当初的设计。

我们的蜂鸣器弄的音乐是自己在乐谱上扣的,如果想要学习请点击以下,

学习怎样Arduino蜂鸣器演奏音乐

目录

1.led8*8点阵

2.这是我们的蜂鸣器音乐(当然不可能只是简单的音乐,这其中的led灯会跟随着音乐的频率进行流水以及呼吸)

3.这是我们的呼吸三位数码管(数字跑的时间会加快,以及在666和888会进行变速的呼吸)

4.最后放几张图怀念一下 

1.led8*8点阵

学习点阵,学习8*8点阵(引脚的连接和这个是一样的)

其实先在8*8开发板上设计好相应的图案然后转换每一列装换成对应的16进制,这个可以用字模提取器进行提取

代码看我写的注释,理解一下

/*国庆项目*/
int leds[8] = {6, 11, 5, 9, 14, 4, 15, 2}; //正引
int gnds[8] = {10, 16, 17, 7, 3, 8, 12, 13}; //负引
//字库
char test[] = {
0x7,0x38,0x49,0x4a,0xdc,0xdc,0x4a,0x41,//庆
0x00,0x3c,0x24,0x24,0xff,0x24,0x24,0x3c,//中
0x00,0xff,0x81,0xd3,0xff,0xd3,0x85,0xff, 0x00, 0x00, 0x00,//国 
0x00, 0x80, 0x8E, 0x90, 0xA0, 0xC0, 0x00, 0x00, // -7-
0x00, 0x7C, 0x8A, 0x92, 0xA2, 0x7C, 0x00, 0x00,//-0-  
0x00,0x7f,0x88,0xaf,0xfd,0xaf,0x88,0xff,0x00,0x00,//周  
0x14,0x24,0xcc,0x54,0x7f,0x54,0x54,0x04,0x00,0x00,//年
};
char tanke[] = {
 0x07,0x3d,0x3f,0x3d,0x17,0x15,0x07,0x10,//坦克  
 0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x00,//子弹
 };
//倒计时
char testnumber[] = {  0x00,0x00,0x00,0x74,0x5C,0x00,0x00,0x00,//小5 
0x00,0x00,0x74,0x54,0x54,0x5C,0x00,0x00,//中5  
0x00,0xE4,0xA2,0xA2,0xA2,0x9C,0x00,0x00,//大5 
0x00,0x00,0x08,0x18,0x3C,0x00,0x00,0x00,//小4  
0x00,0x00,0x18,0x28,0x7C,0x00,0x00,0x00,//中4  
0x00,0x38,0x48,0x88,0xFE,0x08,0x00,0x00,//大4  
0x00,0x00,0x54,0x54,0x6C,0x00,0x00,0x00,//小3 
0x00,0x00,0x54,0x54,0x54,0x7C,0x00,0x00,//中3
0x00,0x84,0x82,0x92,0xB2,0xCC,0x00,0x00,//大3  
0x00,0x00,0x4C,0x54,0x64,0x00,0x00,0x00,//小2  
0x00,0x00,0x5C,0x54,0x54,0x74,0x00,0x00,//中2
0x00,0x00,0x46,0x8A,0x92,0x82,0x62,0x00,//大2  
0x00,0x00,0x00,0x00,0x38,0x00,0x00,0x00,//小1  
0x00,0x00,0x00,0x24,0x7C,0x04,0x00,0x00,//中1  
0x00,0x00,0x00,0x42,0xFE,0x02,0x00,0x00,//大1
};
//升旗
char shenqi[] = {
0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0xFF,0x02,0x02,0x04,0x04,0x02,0x03,
0x00,0xFF,0x04,0x04,0x02,0x02,0x04,0x07,
0x00,0xFF,0x08,0x08,0x04,0x04,0x08,0x0F,
0x00,0xFF,0x11,0x11,0x22,0x22,0x11,0x1F,
0x00,0xFF,0x44,0x44,0x88,0x88,0x44,0x7C,
0x00,0xFF,0x88,0x88,0x44,0x44,0x88,0xF8,
};
char guoqi[] = {
0x00,0xFF,0x44,0x44,0x88,0x88,0x44,0x7C,  
0x00,0xFF,0x88,0x88,0x44,0x44,0x88,0xF8,
};
//爆炸
char yanhua[] = {    
0x00,0x00,0x00,0x10,0x28,0x10,0x00,0x00,//爆炸特效   
0x00,0x00,0x10,0x28,0x44,0x28,0x10,0x00,   
0x00,0x10,0x28,0x44,0x82,0x44,0x28,0x10,    
0x00,0x10,0x44,0x00,0x82,0x00,0x44,0x10,    
0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,//升起    
0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,//升起
0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,//升起    
0x00,0x00,0x00,0x10,0x28,0x10,0x00,0x00,    
0x00,0x00,0x10,0x28,0x44,0x28,0x10,0x00,    
0x00,0x10,0x28,0x44,0x82,0x44,0x28,0x10,    
0x00,0x10,0x44,0x00,0x82,0x00,0x44,0x10,
};
void ledclean()
{for (int i = 0; i < 8; i++)//将点阵屏正极ladies,负极拉高,关断显示
  {   digitalWrite(leds[i], LOW);
     digitalWrite(gnds[i], HIGH);}}
//字符显示
void ledShow(char num, char dat)//每一列的16进制传到这里都会被读入然后亮灯
{  digitalWrite(gnds[num], LOW);
   for (int i = 0; i < 8; i++){
   digitalWrite(leds[i], dat & 0x80); // 1 0 0 0 0 0 0 0
    dat <<= 1;
  }
  delayMicroseconds(100);
  digitalWrite(gnds[num], HIGH);
  ledclean();
}
void xs(char ha[],int b,int time,int tt)//这里进行了一个小封装
{ 
  for (int a = 0; a <=b; a+=tt) {//控制流动
    for (int i = 0 ; i < time ; i++)    {//控制显示的时间
     for (int c = 0; c < 8; c++) {//列循环扫描,扫描8列,显示整个点阵
        ledShow(c, ha[c + a]);
      }
    }
  }
}
void setup() {
  for (int i = 0; i < 8; i++){
    pinMode(leds[i], OUTPUT);
    pinMode(gnds[i], OUTPUT);
    digitalWrite(gnds[i], HIGH); //负极引脚拉高,熄灭所有LED
  }
 }
void loop() {
  xs(testnumber,112,300,8);//倒计时
  xs(shenqi,48,500,8);
  int t;t=4;
  while(t--){                                
   xs(guoqi,8,300,8);//国旗飘动
 }
  xs(yanhua,80,300,8);//烟花
  xs(tanke,8,100,1);//坦克发射
  t=5;
  while(t--){
  xs(test,55,100,1);//流动字幕
  xs(yanhua,80,300,8);//烟花 
  }
}

2.这是我们的蜂鸣器音乐(当然不可能只是简单的音乐,这其中的led灯会跟随着音乐的频率进行流水以及呼吸)

想学自己做乐谱的点击学习怎样Arduino蜂鸣器演奏音乐

//音乐歌唱祖国

#define NTF0 -1
#define NTF1 350
#define NTF2 393
#define NTF3 441
#define NTF4 495
#define NTF5 556
#define NTF6 624
#define NTF7 661

#define NTFL1 175
#define NTFL2 196
#define NTFL3 221
#define NTFL4 234
#define NTFL5 262
#define NTFL6 294
#define NTFL7 330

#define NTFH1 700
#define NTFH2 786
#define NTFH3 882
#define NTFH4 935
#define NTFH5 1049
#define NTFH6 1178
#define NTFH7 1322

//列出全部D调的频率
#define WHOLE 1
#define HALF 0.5
#define QUARTER 0.25
#define EIGHTH 0.125
#define SIXTEENTH 0.0625
//列出所有节拍
int tune1[] = 
{
  NTF1,NTF1,NTF1,NTFL5,
  NTF3,NTF1,NTF5,NTF6,
  NTF5,NTF5,NTF5,NTF6,NTF6,
  NTF6,NTF5,NTF4,NTF6,NTF5,
  NTF5,NTF5,NTF5,NTF6,NTF6,
  NTF2,NTF2,NTF2,NTF5,NTF4,
  NTF3,NTFL5,NTFL5,NTF5,NTF5,NTF6,
  NTF5,NTF4,NTF3,NTF2,NTF1,
  NTF1,NTF5,NTF5,NTFH1,NTFH1,
  NTF6,NTF6,NTF5,NTF4,NTF5,
  NTF6,NTF2,NTF2,NTF5,NTF5,NTF6,
  NTF5,NTF4,NTF3,NTF2,NTF1,
  NTF1,NTF0,
  NTF1,NTFL5,NTF3,NTF3,NTF0,
  NTF3,NTF1,NTF6,NTF6,NTF0,
  NTFL6,NTFL6,NTF2,NTF2,NTF3,
  NTF2,NTF1,NTFL7,NTFL6,NTFL5,
  NTF1,NTFL5,NTFL6,NTFL6,NTFL5,
  NTF1,NTF2,NTF3,NTF0,
  NTF2,NTF6,NTF6,NTF5,NTF5,NTF3,
  NTF2,NTF6,NTF5,NTF0,NTFH1,
  NTFH1,NTFH1,NTFH1,NTF5,NTF6,NTF1,
  NTF6,NTF5,NTF4,NTF6,NTF5,NTF0,
  NTFH1,NTFH1,NTFH1,NTFH1,NTF5,NTF5,NTF6,
  NTF5,NTF4,NTF3,NTF2,NTF1,NTFL5,NTFL5,
};
float durt1[]=
{
  0.5+0.25,0.25,1,1,
  1,1,1+0.5,0.5,
  1,0.5+0.25,0.25,1,1,
  0.5+0.25,0.25,0.5,0.5,1+1,
  1,0.5+0.25,0.25,1,1,
  1,0.5+0.25,0.25,1+0.5,0.5,
  1,0.5+0.25,0.25,1,0.5,0.5,
  0.5,0.5,0.5,0.5,1+1,
  1,0.5+0.25,0.25,1,1,
  1,0.5+0.25,0.25,1+0.5,0.5,
  1,0.5+0.25,0.25,1,0.5,0.5,
  0.5,0.5,0.5,0.5,1+1,
  1+0.5,0.5,
  0.5+0.25,0.25,1,1+0.5,0.5,
  0.5+0.25,0.25,1,1+0.5,0.5,
  1+0.5,0.5,1,0.5+0.25,0.25,
  0.5,0.5,0.5,0.5,1+1,
  1,1,0.5,1,0.5,
  1,1,1,1,
  1,0.5,0.5,0.5,1,0.5,
  1,1,1,0.5,0.5,
  0.5+0.25,0.25,0.5,0.5,1+0.5,0.5,
  0.5+0.25,0.25,0.5,0.5,1,1,
  0.5+0.25,0.25,0.5,0.5,1,0.5,0.5,
  0.5,0.5,0.5,0.5,1,0.5+0.25,0.25,
};
int length;
int tonepin=1;   //得用6号接口
int led[] = {2,4,7,8,10,11,12,13};
int huxi[] ={5,6,9};
void setup()
{
  pinMode(tonepin,OUTPUT);
  for(int i=0;i<8;i++){
    pinMode(led[i],OUTPUT); 
  }
  for(int i=0;i<3;i++){
    pinMode(huxi[i],OUTPUT); 
  }
  length=sizeof(tune1)/sizeof(tune1[0]);   //计算长度
}
void loop()
{
  for(int i=0;i<3;i++){
  digitalWrite(huxi[i],1);
  delay(100);
  }//小三角逐个亮起
  for(int i=0;i<8;i++){
  digitalWrite(led[i],1);
  delay(100);
  }//箭头型led逐个亮起
  delay(1000);
  for(int i=0;i<8;i++){
  digitalWrite(led[i],0);
  delay(500);
  }//箭头型灯逐个灭掉
  for(int i=0;i<3;i++){
  digitalWrite(huxi[i],0);
  delay(500);
  }//小三角灯逐个灭掉
  int bright = 0,f = -15;
  int k=0;
  for(int x=0;x

3.这是我们的呼吸三位数码管(数字跑的时间会加快,以及在666和888会进行变速的呼吸)

#define dw digitalWrite
#define uint unsigned int
uint data[][10]={
{1,2,3,7,4,5,0},//0
{2,3,0},//1
{1,2,4,5,6,0},//2
{1,2,3,4,6,0},//3
{2,3,7,6,0},//4
{1,7,6,3,4,0},//5
{1,7,6,3,4,5,0},//6
{1,2,3,0},//7
{1,2,3,4,5,6,7,0},//8
{1,7,6,2,3,4,0}//9
};
bool flag = true;
uint p = 0;
//点亮一位数字
void ledstart(uint n,uint k)
{
  if(k%3==0){
    dw(9,1),dw(10,0),dw(11,0);
  }
  else if(k%3==1){
    dw(9,0),dw(10,1),dw(11,0);
  }
  else{
    dw(9,0),dw(10,0),dw(11,1);
  }
  for(uint i = 0;i<9;i++) //初始化数码管
    dw(i,1);
  for(uint i = 0;data[n][i]!=0;i++)
    dw(data[n][i],0);
}
void setup()
{
  for(uint i = 0;i<12;i++){
    pinMode(i,OUTPUT);
    dw(i,1);
  }       
}
void loop()
{
  dw(11,1);
  for(uint p = 0;p<9;p++){
    for(uint i = 0;i<9;i++){
      for(uint j = 0;j<9;j++)
      {
        if(p==6 && i==6 && j==6)//当数码管显示666时进行呼吸
        {
          ledstart(p,11);
          ledstart(i,9);
          ledstart(j,10); 
          //dw(11,0);
          int kk = 0;
          for(int b=0;b<50;b++){
            for (int a=0;a <= 255; a = a+kk){
              kk +=1;//呼吸加快,由弱变强
              analogWrite(11,a);
              analogWrite(10,a);
              analogWrite(9,a);
              delay(50);
            }
            //由强变弱
            for(int a = 255;a >= 0;a = a-kk){
              analogWrite(11,a);
              analogWrite(10,a);
              analogWrite(9,a);
              delay(50);
            }
          }
       }
      if(p==8&&i==8&&j==8){ //当数码管显示888时进行闪烁
      for(int d=0;d<30;d++)
                {
                  ledstart(p,11);
                  ledstart(i,9);
                  ledstart(j,10); 
                  dw(11,HIGH);
                  dw(10,HIGH);
                  dw(9,HIGH);
                  delay(100);
                  dw(11,LOW);
                  dw(10,LOW);
                  dw(9,LOW);
                  delay(100); 
                }}
      int q = 14;//变化的时间
      if((p>=6&&i>=6&&j>6)||p>=7){
        q = 3;//666之后加快变化
      }
      for(uint k = 0;k

4.最后放几张图怀念一下 

Arduino小项目设计(蜂鸣器与呼吸流水灯结合,以及led点阵)_第1张图片Arduino小项目设计(蜂鸣器与呼吸流水灯结合,以及led点阵)_第2张图片Arduino小项目设计(蜂鸣器与呼吸流水灯结合,以及led点阵)_第3张图片

你可能感兴趣的:(Arduino)