-2

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?

Yunnosch
  • 26,130
  • 9
  • 42
  • 54

1 Answers1

0

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>
Hardwarics
  • 18
  • 4
  • Hi, thanks for your help but im not sure about this, i need to store my frequencies in new array after im done with my playing melody in order which i pressed buttons, and after this i want to replay that on buzzer after i press one extra pushbutton on my board (PB9) – Domagoj 954 Jan 18 '22 at 17:00
  • https://onlinegdb.com/J8NrZWe9j -> here is my full code with combined with yours (C++) – Domagoj 954 Jan 18 '22 at 18:45
  • Hi. I checked your code and I think it is time to add another function called **play_the_recorded_melody**. Please check the following code: `void play_the_recorded_melody() { for(i=0; i < 8; i++) { BUZZ (0.1 , recorded[i]) ; _delay_ms (100) ; } }` Then call `play_the_recorded_melody();` to play the melody you have recorded. If I could help you, that is my pleasure! – Hardwarics Jan 18 '22 at 20:37
  • Thanks for answer, so i just must to add this function, everything others is ok, right? And what is with recordAndPlay() function now, do I have to change anything? Sorry but I didn't have much time to study your recommendations in detail yesterday and today – Domagoj 954 Jan 18 '22 at 22:44
  • And yes, sorry if i bother you – Domagoj 954 Jan 19 '22 at 00:06
  • Hey Hardwarics i did it, i made code which is combination of yours and mine and it works good, thanks for help. – Domagoj 954 Jan 22 '22 at 22:56
  • At the end of hard work and determination there comes success! It is my pleasure! Happy to hear that you made it work. I don't know if it's the right place to give advice, but always try to ask less, be patient, and think more on your own. This will help you to improve yourself more than just getting ready-to-use answers from others. – Hardwarics Jan 23 '22 at 10:52
  • I agree with you :), thanks for help once again – Domagoj 954 Jan 23 '22 at 16:45