单片机实现实体音游模拟器

项目介绍&总结

这个大一暑期和大二上学期做的一个合作项目。
我们希望做一个硬件来实现对音游的实体化,所采用了音乐楼梯的形式——检测人的位置来击打掉落的光快,从而发出音乐。
硬件上使用了XNUCLEO的stm32-f103rb单片机(主控),音频播放模块(播放音频),LED灯(阵列表示台阶),超声波传感器(障碍物位置输入)。
软件上用mbed online compiler和keil5测试程序,再使用st-link烧录进单片机。
我主要参与了开始的创意确立和方案制定,完成了代码中部分逻辑的实现和最后实物的焊接与组装。
虽然付出了很多努力,但最终效果不尽如人意。主要是因为硬件设计需要能力要求较高,我们当时的水平难以达到,走了很多弯路。而且使用的超声波传感器比较玄学,测量精度不佳。综上种种,要想快速实现idea,并且实现快速迭代,还是软件好用。或许有机会再去啃硬件的“深坑”。

效果图

代码部分

#include "mbed.h"

//define pins of LEDs(6*6 matrix)

BusOut dec0(PA_10,PB_3,PB_5,PB_4);//CN9
BusOut dec1(PB_10,PA_8,PA_9,PC_7);//CN5
BusOut dec2(PB_6,PA_7,PA_6,PA_5);//CN8
BusOut dec3(PB_9,PB_8,PC_9,PC_3);//CN7
BusOut dec4(PC_2,PA_0,PA_1,PC_13);//CN7
BusOut dec5(PB_7,PA_15,PA_14,PA_13);//CN9

//define pins of sensors
DigitalOut trig_L(PC_8);//
DigitalOut trig_R(PC_6);//
DigitalIn echo_L(PC_5);
DigitalIn echo_R(PA_12);//CN10
int position;

//define pins of audio module
BusOut aud(PC_12,PC_10,PC_11,PD_2,PC_4,PB_13);//CN10 0d000001~0d100100
//PwmOut buz(PB_13);

//define pins of song selector(switch)
DigitalIn song(PA_11);
int SongNum=0;

//define pins of start botton
DigitalIn sta(PB_12);

//define pins of response LEDs
DigitalOut red(PB_11);//?
DigitalOut green(PB_2);//?

//pc communication(for debug)
Serial pc(USBTX, USBRX);

//define buzzer frequencies of keys

/*float S_0=0;
float C_3=261.63/2,c_3=277.18/2,D_3=293.66/2,d_3=311.13/2,E_3=329.63/2,F_3=349.23/2,
f_3=369.99/2,G_3=392.00/2,g_3=415.30/2,A_3=440.00/2,a_3=466.16/2,B_3=493.88/2;
float C_4=261.63,c_4=277.18,D_4=293.66,d_4=311.13,E_4=329.63,F_4=349.23,
f_4=369.99,G_4=392.00,g_4=415.30,A_4=440.00,a_4=466.16,B_4=493.88;
float C_5=261.63*2,c_5=277.18*2,D_5=293.66*2,d_5=311.13*2,E_5=329.63*2,F_5=349.23*2,
f_5=369.99*2,G_5=392.00*2,g_5=415.30*2,A_5=440.00*2,a_5=466.16*2,B_5=493.88*2;
*/

int S_0=0;
int C_3=1,c_3=2,D_3=3,d_3=4,E_3=5,F_3=6,
    f_3=7,G_3=8,g_3=9,A_3=10,a_3=11,B_3=12;
int C_4=13,c_4=14,D_4=15,d_4=16,E_4=17,F_4=18,
    f_4=19,G_4=20,g_4=21,A_4=22,a_4=23,B_4=24;
int C_5=25,c_5=26,D_5=27,d_5=28,E_5=29,F_5=30,
    f_5=31,G_5=32,g_5=33,A_5=34,a_5=35,B_5=36;


//define combo counter
int com = 0;

//define songs
int TestSong[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36};
int   WeightOfTheWorld[]= {
    S_0,S_0,S_0,S_0,S_0,A_3,E_4,D_4,G_4,A_3,E_4,D_4,G_4,A_3,E_4,D_4,A_4,A_3,E_4,D_4,A_4,
    A_3,E_4,D_4,G_4,A_3,E_4,D_4,G_4,A_3,E_4,D_4,A_4,A_3,A_4,A_4,S_0,
    A_3,A_3,A_3,A_3,A_3,B_3,C_4,S_0,A_3,A_3,A_3,A_3,A_3,B_3,C_4,S_0,
    C_4,D_4,E_4,S_0,S_0,S_0,E_4,F_4,E_4,E_4,D_4,D_4,S_0,S_0,S_0,S_0,
    A_3,A_3,A_3,A_3,A_3,B_3,C_4,S_0,A_3,A_3,A_3,A_3,A_3,B_3,C_4,S_0,
    C_4,S_0,G_4,S_0,S_0,S_0,G_4,F_4,E_4,E_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,S_0,
    C_5,B_4,G_4,G_4,E_4,B_4,A_4,S_0,
    E_4,G_4,F_4,A_4,B_5,G_4,B_4,C_5,B_4,
    E_4,C_5,B_4,D_5,C_5,G_4,B_4,C_5,
    C_5,B_4,A_4,A_4,D_5,G_4,G_4,B_4,A_4,
    S_0,S_0,S_0,S_0,S_0,S_0,S_0,S_0,
};


void PlaySong(int i);

int pow(int x);
void TEST();
void SENSORTEST();
void AUDIOTEST();
//float toBuzzer(int x);

int main()
{
    //TEST();
	  //SENSORTEST();
	//AUDIOTEST();
	

    int a[556]={0};
		int b[556]={0};
    //float a[556]={0};
		//float b[556]={0};

    while(1) {

        //1.select songs
        if(song==1) {
            //printf("select song success");
            SongNum++;
            PlaySong(SongNum);
            //printf("play song success");
        }

        if(SongNum>=2) {
            SongNum=0;
        }

        //2.press botton to start
        SongNum=1;
        //3.countdown before start(displayed by 6*6 LED matrix)
        int m;
				//float m;
        int judge=0;
				
        /*
				if(1) {
            int j=0;
            for(j=0; j<550; j++) {
                if(TestSong[j]==0) {
                    a[j]=0;
									b[j]=0;
                } else {
                    m=TestSong[j]%4;
                    a[j]=m+1;//m=1~4
									b[j]=TestSong[j]; 
                }
            }
        }
				*/
        if( SongNum==1) {
            int j=0;
            for(j=0; j<550; j++) {
                if(WeightOfTheWorld[j]==0) {
                    a[j]=0;
                } else {
                    m=WeightOfTheWorld[j]%4;
                    a[j]=m+1;//m=1~4
                }
								
									b[j]=~WeightOfTheWorld[j];
            }
        }

        if( SongNum==2) {
            for(int j=0; j<550; j++) {
                if(HackingToTheGate[j]==0) {
                    a[j]=0;
                } else {
                    m=HackingToTheGate[j]%4;
                    a[j]=m+1;
                }
								
									b[j]=~HackingToTheGate[j];
            }
        }

        dec1=2+4+8+1;
        dec2=8;
        dec3=2+4+8+1;
        dec4=8;
        dec5=2+4+8+1;//3
        wait(1);
        dec1=2+4+8+1;
        dec2=1;
        dec3=2+4+8+1;
        dec4=8;
        dec5=2+4+8+1;//2
        wait(1);
        dec1=4;
        dec2=4;
        dec3=4;
        dec4=4;
        dec5=4;//1
        wait(1);

        int n;
        int y[6]= {0};

        //wait_us(10);

        /*if (lengthCount_R >= 53 && lengthCount_R <= 67) {
            position = 3;
        }
        if (lengthCount_R >= 29 && lengthCount_R <= 46) {
            position = 2;
        }
        if (lengthCount_R >= 8 && lengthCount_R <= 26) {
            position = 1;
        }*/

        //wait_us(10);

        for(int j=0; j<=550; j++) {
        //sensor_left
        int lengthCount_L = 0;
        wait_us(10);
        trig_L = 1;
        wait_us(10);
        trig_L = 0;
        for(int i = 0; i < 120; i++) {
            if (echo_L == 1) {
                lengthCount_L += 1;
            }
            wait_us(10);
        }

        //sensor right
        int lengthCount_R = 0;
        wait_us(10);
        trig_R = 1;
        wait_us(10);
        trig_R = 0;
        for(int i = 0; i < 80; i++) {
            if (echo_R == 1) {
                lengthCount_R += 1;
            }
            wait_us(10);
        }
        int position = 0;
        /*if (lengthCount_L >= 16 && lengthCount_L <= 30) {
            position = 6;
        }
        if (lengthCount_L >= 33 && lengthCount_L <= 52) {
            position = 5;
        }
        if (lengthCount_L >= 55 && lengthCount_L < 73) {
            position = 4;
        }*/
				if (lengthCount_L >= 10 && lengthCount_L <= 18) {
            position = 1;
					
        }
					else if(lengthCount_L >= 21 && lengthCount_L <= 29){
            position = 2;
					
					}
					else if(lengthCount_L >= 31 && lengthCount_L <= 41){
            position = 3;
					
					}
					else if(lengthCount_L >= 43 && lengthCount_L <= 53){
            position = 4;
					
					}
					else {
            position = 0;
					
					}
					pc.printf("position = %d\n",position);
            for(int i=0; i<=5; i++) {
                y[i]=a[j+i];//y[i]save a[i]and other five before
            }

            if(y[5]==0) {
                dec5=0;
            } else {
                n=pow(y[5]);
                dec5=n;
            }
            
            if(y[4]==0) {
                dec4=0;
            } else {
                n=pow(y[4]);
                dec4=n;
            }

            if(y[3]==0) {
                dec3=0;
            } else {
                n=pow(y[3]);
                dec3=n;
            }

            if(y[2]==0) {
                dec2=0;
            } else {
                n=pow(y[2]);
                dec2=n;
            }

            if(y[1]==0) {
                dec1=0;
            } else {
                n=pow(y[1]);
                dec1=n;
            }

            if(y[0]==0) {
                dec0=0;
            } else {
                n=pow(y[0]);
                judge=y[0];
                dec0=n;
            }
						aud=b[j];
            if(judge==position) {
                green=1;
                red=0;
            } else {
                red=1;
                green=0;
            }
            wait(0.65);
						aud=0;
						wait(0.05);
        }

        //4.play
        //4-1.lights drop down
        //4-2.sensor detect finger position
        //4-3.feedback:right positio —— play sound, combo counts; wrong position —— no sound, combo breaks

        //5.show maximum combo

        //6.press botton to end

    }

}

void PlaySong(int i)
{

    int j=0;

    if(i==1) {
        //printf("select first song success");

        while(WeightOfTheWorld[j]!=NULL) {
            j++;
            aud=(~WeightOfTheWorld[j]);
            wait(1);
            if(i==0) {
                return;
            }
        }
    } else if(i==2) {
        //printf("select second song success");

        while(HackingToTheGate[j]!=NULL) {
            j++;
            aud=(~HackingToTheGate[j]);
            wait(1);
            if(i==0) {
                return;
            }
        }
    }
}

int pow(int x)
{
    int k=1;
    for(int i=x; i>1; i--) {
        k=k*2;
    }
    return k;
}


void TEST(){
    /*int y = 1;
    for(int x=1;x<=36;x++){
        aud = ~x;
        pc.printf("x = %d; y = %d\n",x,y);
        pc.printf("%d\n",~x);
        wait(2);
    }*/

    /*int x = 1;
    while(1){

    aud = x;
        pc.printf("%d\n",x);
        x=x*2;
        if (x==64){
            x=1;
        }
    wait(5);

    }*/
    while(1) {
        dec5=1;
        wait(0.1);
        dec5=2;
        wait(0.1);
        dec5=4;
        wait(0.1);
        dec5=8;
        wait(0.1);
        dec5=0;

        dec4=1;
        wait(0.1);
        dec4=2;
        wait(0.1);
        dec4=4;
        wait(0.1);
        dec4=8;
        wait(0.1);
        dec4=0;

        dec3=1;
        wait(0.1);
        dec3=2;
        wait(0.1);
        dec3=4;
        wait(0.1);
        dec3=8;
        wait(0.1);
        dec3=0;

        dec2=1;
        wait(0.1);
        dec2=2;
        wait(0.1);
        dec2=4;
        wait(0.1);
        dec2=8;
        wait(0.1);
        dec2=0;

        dec1=1;
        wait(0.1);
        dec1=2;
        wait(0.1);
        dec1=4;
        wait(0.1);
        dec1=8;
        wait(0.1);
        dec1=0;

        dec0=1;
        wait(0.1);
        dec0=2;
        wait(0.1);
        dec0=4;
        wait(0.1);
        dec0=8;
        wait(0.1);
        dec0=0;
        
        trig_L=1;
        wait(0.1);
        trig_L=0;
        trig_R=1;
        wait(0.1);
        trig_R=0;
				
        aud=1;
        wait(0.1);
        aud=2;
        wait(0.1);
        aud=4;
        wait(0.1);
        aud=8;
        wait(0.1);
        aud=16;
        wait(0.1);
        aud=32;
        wait(0.1);
        aud=0;
        wait(0.1);
        
        red=1;
        wait(0.1);
        red=0;
        green=1;
        wait(0.1);
        green=0;
				
				pc.printf("output test finish\n");
				while(echo_L==0){
            wait(1);
            pc.printf("echo_L=0\n");
            }
        
        pc.printf("echo_L=1\n");
				wait(1.0);
            
        while(echo_R==0){
            wait(1);
            pc.printf("echo_R=0\n");
            }    

        pc.printf("echo_R=1\n");
				wait(1.0);
						
        while(song==0){
            wait(1);
            pc.printf("song=0\n");
            }    

        pc.printf("song=1\n");
				wait(1.0);
        
        while(sta==0){
            wait(1);
            pc.printf("sta=0\n");
            }    

        pc.printf("sta=1\n");
				wait(1.0);
    }
		
}

void SENSORTEST(){
        while(1){
					//sensor_left
        int lengthCount_L = 0;
        wait_us(10);
        trig_L = 1;
        wait_us(10);
        trig_L = 0;
        for(int i = 0; i < 120; i++) {
            if (echo_L == 1) {
                lengthCount_L += 1;
            }
            wait_us(10);
        }


        //sensor right
        int lengthCount_R = 0;
        wait_us(10);
        trig_R = 1;
        wait_us(10);
        trig_R = 0;
        for(int i = 0; i < 120; i++) {
            if (echo_R == 1) {
                lengthCount_R += 1;
            }
            wait_us(10);
        }
				pc.printf("L=%d,R=%d\n",lengthCount_L,lengthCount_R);
				wait(1);
				if (lengthCount_L >= 10 && lengthCount_L <= 18) {
            position = 1;
        }
					else if(lengthCount_L >= 21 && lengthCount_L <= 29){
            position = 2;
					
					}
					else if(lengthCount_L >= 31 && lengthCount_L <= 41){
            position = 3;
					
					}
					else if(lengthCount_L >= 43 && lengthCount_L <= 53){
            position = 4;
					
					}
					else {
            position = 0;
					
					}
				/*if((lengthCount_L+lengthCount_R<=65)&&(lengthCount_L+lengthCount_R>=55)){
          if (lengthCount_L >= 10 && lengthCount_L <= 18) {
            position = 1;
        }
					else if(lengthCount_L >= 21 && lengthCount_L <= 29){
            position = 2;
					
					}
					else if(lengthCount_L >= 31 && lengthCount_L <= 41){
            position = 3;
					
					}
					else if(lengthCount_L >= 43 && lengthCount_L <= 53){
            position = 4;
					
					}
					else {
            position = 0;
					
					}
					
				}*/
				/*
        int position = 0;
        if (lengthCount_L >= 16 && lengthCount_L <= 30) {
            position = 6;
        }
        if (lengthCount_L >= 33 && lengthCount_L <= 52) {
            position = 5;
        }
        if (lengthCount_L >= 55 && lengthCount_L < 73) {
            position = 4;
        }

        //wait_us(10);

        if (lengthCount_R >= 53 && lengthCount_R <= 67) {
            position = 3;
        }
        if (lengthCount_R >= 29 && lengthCount_R <= 46) {
            position = 2;
        }
        if (lengthCount_R >= 8 && lengthCount_R <= 26) {
            position = 1;
        }
				*/
				
				}
}

void AUDIOTEST(){
	while(1){
		int i;
		for(i=1;i<=36;i++){
			/*aud=~i; 
			wait(0.65);
			aud=~0;
			wait(0.05);*/
			aud=~WeightOfTheWorld[i];
			wait(0.65);
			aud=~0;
			wait(0.05);
//pc.printf("WeightOfTheWorld[%d]=%d\t",i,WeightOfTheWorld[i]);	
		}
	}
}

你可能感兴趣的:(学习总结,单片机,传感器,stm32)