简体   繁体   English

如何制作 function 以在 Arduino 蜂鸣器上保存和重放音调?

[英]How to make function for save and replay tones on Arduino buzzer?

i have a question about my project in Arduino, i have this array of frequencies for notes:我对我在 Arduino 中的项目有疑问,我有这个频率数组作为注释:

int note[] = {261, 293, 329, 349, 392, 440, 494, 523};

and this function for play notes if one of pushbuttons is pressed:如果按下其中一个按钮,则此 function 用于播放音符:

void play(float U_ADC0){ 


        if(U_ADC0 >= 4.80) { // ADC conversion (Voltage value)  PB1
            BUZZ (0.1 , note[0]) ; _delay_ms (100) ;  //  buzz
            lcd_clear();
            lcd_write("C4");  // lcd display 

        }
        
        if(U_ADC0 < 4.80 && U_ADC0 >= 4.70){ //PB2
            BUZZ (0.1 , note[1]) ; _delay_ms (100) ;
            lcd_clear();
            lcd_write("D4");
        }
        
        if(U_ADC0 < 4.72 && U_ADC0 >= 4.65){ //PB3
            BUZZ (0.1 , note[2]) ; _delay_ms (100) ;
            lcd_clear();
            lcd_write("E4");
        }
        
        if(U_ADC0 < 4.60 && U_ADC0 >= 4.50){  //PB4
            BUZZ (0.1 , note[3]) ; _delay_ms (100) ;
            lcd_clear();
            lcd_write("F4");
        }
        
        if(U_ADC0 < 4.20 && U_ADC0 >= 4.05){ //PB5
            BUZZ (0.1 , note[4]) ; _delay_ms (100) ;
            lcd_clear();
            lcd_write("G4");
        }
        
        if(U_ADC0 < 3.80 && U_ADC0 >= 3.70){ //PB6
            BUZZ (0.1 , note[5]) ; _delay_ms (100) ;
            lcd_clear();
            lcd_write("A4");
        }
        
        if(U_ADC0 < 3.55 && U_ADC0 >= 3.30){ //PB7
            BUZZ (0.1 , note[6]) ; _delay_ms (100) ;
            lcd_clear();
            lcd_write("B4");
        }
        
        if(U_ADC0 < 2.55 && U_ADC0 >= 2.45){ //PB8
            BUZZ (0.1 , note[7]) ; _delay_ms (100) ;
            lcd_clear();
            lcd_write("C5");

        }

} }

so, how can i make new field of frequencies in order by pressed pushbuttons so i could save and replay my melody on buzzer?那么,我怎样才能通过按下按钮按顺序制作新的频率场,以便我可以保存并在蜂鸣器上重播我的旋律? I used all my ideas but doesn't work and i don't have new ones.我使用了我所有的想法,但没有奏效,我也没有新的想法。 So if somebody have idea, can you help me?所以如果有人有想法,你能帮帮我吗?

I would use one button (let's call it the record button ) to switch between play & record and only play .我会使用一个按钮(我们称之为录制按钮)在播放和录制之间切换,并且只播放 In this way, whenever you push buttons that get those buzzer frequencies will not save, but when you like the melody and want to save you can click the record button and start to save.这样,每当你按下按钮,得到那些蜂鸣器频率不会保存,但当你喜欢旋律并想保存时,你可以点击录制按钮开始保存。 For making this happen, follow the algorithm below:为了实现这一点,请遵循以下算法:

After your first function, create a function for record button .在您的第一个 function 之后,创建一个 function 用于录制按钮 In this function, you need call your first function (void play) you have already written and add one more snippet of code for assigning the value of pushed button into the int array you will create at the beginning of your code (let's call it int recorded[] ).在这个 function 中,您需要调用您已经编写的第一个 function(无效播放)并添加一个代码片段,用于将按下按钮的值分配到您将在代码开头创建的int 数组中(我们称之为int记录[] )。

One more step remains and that's to check (if the record button is pushed) the switch button, so it will toggle between record & play and play and call the corresponding function.还剩下一步,即检查(如果按下录制按钮)开关按钮,因此它将在录制和播放播放之间切换,并调用相应的 function。 Finally, you can add one more button to play the melody from your int recorded[] .最后,您可以再添加一个按钮来播放int Recorded[]中的旋律。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM