Search code examples
c++carduinoavrmicrochip

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:

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

and this function for play notes if one of pushbuttons is pressed:

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?


Solution

  • 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. 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[]).

    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. Finally, you can add one more button to play the melody from your int recorded[].

    I have finished the code already. You can check my comments throughout the code. It might not be the shortest way to work, simulate and test the results, but I believe it is going to solve your problem. Let me know if it helped you.

    Link: https://onlinegdb.com/PV7Mu_51q

    Embedded:

    <script src="//onlinegdb.com/embed/js/PV7Mu_51q?theme=dark"></script>