MOCKSTACKS
EN
Questions And Answers

More Tutorials









Raspberry Two Tigers


Next, let's try to control the buzzer to play the complete music of Two Tigers by defining a function to simplify the program.

Write a Program


When we write a simple piece of music, we can write the program line by line. However, complex programs written this way are too long and repetitive. If we want to reproduce whole music, whether it is from the perspective of improving code reuse or program readability, we need to use custom functions to simplify the program. Following Project 1, we can define every note as a function that can be called when we want to play that note.

Taking DO as an example, the program to define the function is as follows:


Define seven notes and a stop-sound program, N(), as functions in the same way:


In the later program, we only need to call these functions to complete the music. The program of playing Two Tigers is then as follows:


In the fifth line of the program, we defined a variable named “vol" to represent the duty cycle value of the PWM signal in later programs. Now, whenever we want to modify the volume of the buzzer, we only need to modify the initial value of the variable “vol".


If you look at our program carefully, you will find it's much simpler than writing the program line by line. Now, the only part that is always repeated in this program is the “sleep()" function. In complete music, many notes sound at different times – thus we cannot simply add the “sleep()" function to the function body. Instead, we need to create a custom function with parameters. Similarly, taking the note DO as an example, the program to DO(0.5)the note DO with duration is as follows:


When we use this function, we need to type the parameter of the function body in the parentheses. The parameter is called by the “sleep() function" in the function body so that we can conveniently control the sound time of the note. For example, when the note DO needs to be sounded for 0.5 seconds, we can write the program like this:


How about it? Is it more concise? By defining functions this way, we can make the program both more efficient and easier to read. The final program is as follows:


Use a USB cable to connect Pico to the computer, and click the “run" button to save the program to any location. Now, you can hear the buzzer play Two Tigers.



Conclusion

In this page (written and validated by ) you learned about Raspberry Two Tigers . What's Next? If you are interested in completing Raspberry tutorial, your next topic will be learning about: Raspberry Journey of Data Visualization the Use of LCD.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.