{"id":490,"date":"2018-10-10T13:36:12","date_gmt":"2018-10-10T17:36:12","guid":{"rendered":"http:\/\/www.thesmarthomehookup.com\/test_install\/?p=490"},"modified":"2023-04-24T15:44:31","modified_gmt":"2023-04-24T19:44:31","slug":"add-audio-to-your-projects-and-learn-to-quickly-read-and-use-other-peoples-code","status":"publish","type":"post","link":"http:\/\/www.thesmarthomehookup.com\/test_install\/add-audio-to-your-projects-and-learn-to-quickly-read-and-use-other-peoples-code\/","title":{"rendered":"Add Audio To Your Projects, and Learn To Quickly Read and Use Other People&#8217;s Code"},"content":{"rendered":"<p><iframe width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/CXLVrqekgiA\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen=\"\"><\/iframe><\/p>\n<p>Today on the hookup I\u2019m going to show you an Arduino compatible MP3 player board that you can use to add sound effects to your projects, but more importantly, I\u2019m going to show you how to decipher other people\u2019s Arduino code to use in your own projects.<\/p>\n<p>As part of my Halloween decorations I had some pretty simple goals, I wanted the randomly generated lightning effect on my house LEDs to also play a thunder sound effect, and I wanted a crazy witch to cackle every time someone rang the doorbell.&nbsp;&nbsp; To do this, I bought this little Arduino compatible serial MP3 player from amazon.&nbsp; For 8 bucks you get an MP3 decoder that can read a microSD card, adjust volume and play specific tracks, but you get basically zero documentation on how to use it.<\/p>\n<p>Normally when you get one of these boards it has an accompanying Arduino library to import and work with, but this one doesn\u2019t.&nbsp; In fact, the only documentation can be found in the questions section of the amazon listing where ther is a link to some example code you can use to make it work.<\/p>\n<p>Now, at this point in my electronics journey I feel pretty confident reading through someone else\u2019s code to figure out what in the world is going on, but I remember a time not so long ago that the code may as well been written in hieroglyphics.&nbsp; This video aims to help you decipher the important parts of other people\u2019s code, so you can make it your own.<\/p>\n<p>I\u2019m going to use the code for this MP3 player board because I think it\u2019s more useful if I use code that I didn\u2019t write as an example, this code will also be ideal because it doesn\u2019t use any additional libraries so all of the functions are included in the main sketch.&nbsp; If you\u2019d like to follow along, I included the link for this code in the video description below.<\/p>\n<p>Before we even begin we need to separate code from not code.&nbsp; Most sketches will include sections called comments that are made by using double front slash, or front slash asterisk, these sections do not contain code and are just there to help you understand what each part of the sketch does and they never get uploaded to your microcontroller.&nbsp; The Arduino IDE turns them gray so it\u2019s a bit easier to distinguish them from the coding regions.&nbsp; On to the actual code!<\/p>\n<p>I remember when I first started reading and writing code my biggest problem was figuring out what commands a microcontroller could understand and what it couldn\u2019t.&nbsp; The quick answer is that there are very few commands that an Arduino board can -natively- understand.&nbsp; These commands are called \u201cbuilt in functions\u201d and there\u2019s a list of them on the Arduino website.&nbsp; But why are there so many commands used in other people\u2019s code that aren\u2019t listed as built in functions? For instance you can see that in the code for this MP3 player it says sendCommand(CMD_PLAY, 0), which seems like a nice human readable command, but the microcontroller will have no idea what that code means unless we tell it.<\/p>\n<p>Since this code doesn\u2019t use any special libraries we should be able to figure out how the microcontroller knows what these extra commands mean.&nbsp; The first part says sendCommand, and if we look in the code we can see that there is a part that says void sendCommand(), this part of the code is called a function and is basically a series of actions that performs a specific task.<\/p>\n<p>This function sends commands to the MP3 player that it can understand over a serial connection.&nbsp; You can see that it expects two inputs, the first one is a command, and the second one is data of some kind.&nbsp; Now our function makes a bit more sense, sendCommand(CMD_PLAY, 0) means it will use this function to send the command CMD_PLAY to the mp3 player, and it doesn\u2019t need any additional data for that, so the data is equal to zero.<\/p>\n<p>But wait, CMD_PLAY isn\u2019t a built in function either, so what does that mean?<\/p>\n<p>Another place where we teach the Arduino code new words is usually located at the top of the code.&nbsp; This area has a whole bunch of entries that say #define, and each define entry essentially works like the \u201cfind and replace\u201d function in Microsoft word.&nbsp; Once you press the compile button it will replace every instance of the value CMD_PLAY with the value 0X0D, which is a hexadecimal number that the MP3 player will understand as a command, using #define statements allows us to make our code more human readable and also allows us to change every instance of a command quickly since editing this one line will change what each value is replaced with.<\/p>\n<p>The final way we teach our Arduino new words is with variables.&nbsp; The difference between a define and a variable is bit obvious, but also a bit more complex.&nbsp; At the most basic level a define gets defined as a single value and never changes, where as a variable is, well, variable meaning you can assign it different values at different points in the code.<\/p>\n<p>The more complex difference is that when you hit the compile button define values get replaced before they get to the microcontroller, but variables are assigned a location in memory on the chip.<\/p>\n<p>Armed with this knowledge, we can try to figure out what this code actually does.&nbsp; Generally speaking there are two places to look.<\/p>\n<p>The void setup() section contains parts of the code that need to run one time in order to make the hardware work.&nbsp; This is where you normally connect to wifi, initialize sensors, and set up MQTT or HTTP connections.&nbsp; Generally speaking, stuff that\u2019s in the void setup of someone else\u2019s code will need to be copied into the void setup of your code to make it work.<\/p>\n<p>The second, and more important place to look to figure out what a program does is the void loop.&nbsp; The void loop runs hundreds, or even thousands of times a second.<\/p>\n<p>You can see in this void loop it checks to see if there is any information has been sent via the serial connection and then sends that information to the MP3 player using the sendMP3Command function.&nbsp; So to figure out what that function does we will scroll through the code to find void sendMP3Command, and immediately we can see if I type question mark or the letter h, it will output this list of commands for me.<\/p>\n<p>At this point, I know enough about the program to start testing it out.&nbsp; I\u2019ll hook up my little MP3 player board to my ESP8266 node MCU using the pins that are listed in the define section of the sketch.&nbsp;&nbsp; The pins were initially listed as pins 5 and 6, but I chose to modify them based on the chart I made for my earlier video about selecting the correct pins on the NodeMCU.&nbsp; I decided that pin 4 and 5 would make better choices because they won\u2019t interfere with the boot process.<\/p>\n<p>I also need to grab a microSD card and format the names of my mp3 files using naming convention specified in the example code.&nbsp; Which is folders with two digit numbers and these names for the mp3 files.<\/p>\n<p>Next I\u2019ll plug my NodeMCU into my computer with a USB cable and select the correct COM port for that USB port on my computer.&nbsp; These are the standard settings that I use to upload programs to a NodeMCU.&nbsp; Once that\u2019s all done just hit upload and the file will automatically save, compile, and upload to the ESP8266.<\/p>\n<p>After it\u2019s uploaded I should be able to go into the Serial monitor and press h to get a list of commands for my mp3 player.&nbsp; Pressing 1 should play my first sound effect, 2 should play the second, and 3 should play the third.&nbsp; Perfect.&nbsp; But this doesn\u2019t really do me any good yet.&nbsp; I\u2019m not going to be triggering these audio files via the serial monitor, I want them to be triggered by MQTT events.<\/p>\n<p>At this point, I want to take an old sketch that I\u2019ve already written that has WiFi and MQTT, and combine it with this mp3 player sketch to enable my mp3 player to work via MQTT.&nbsp;&nbsp; As I mentioned before, I know I\u2019ll need to combine the void setup of my sketch with the void setup of the MP3 sketch, I\u2019ll also need to make sure I include the functions, variables, and definitions for any commands I want to use.&nbsp; Specifically I\u2019m going to be using the command CMD_PLAY_WITH_VOLUME, which allows me to specify both a file and a volume to play which will allow me to vary the volume randomly to create more realistic thunder.<\/p>\n<p>To keep my code more readable I\u2019ll group together the functions from the MP3 player and put a comment on that section so I know what those functions do.&nbsp; Since I borrowed this MQTT code from another project, I also need to change the ClientID and the check in topic so I don\u2019t create conflicts on my MQTT server.<\/p>\n<p>Finally I\u2019ll add in some MQTT responses to play each track via MQTT then hit compile to see if I get any errors.&nbsp; If I do get an error, it\u2019s not the end of the world.&nbsp; Sometimes I forget to add a semicolon or curly bracket, but usually I just forgot to copy over a needed function or variable.&nbsp; In this case I purposely left out the s_byte_to_hex function to show what happens when you forget something.&nbsp; No problem, go to the original code, copy it over and try compiling again.&nbsp; If you can\u2019t find that function in the original code it\u2019s very likely that you are missing a library, check the includes statements at the top of the code to make sure you aren\u2019t missing any.<\/p>\n<p>My program successfully compiled, so I can send it over to my NodeMCU using the same settings as before.&nbsp; Now when I send the message 1 to the topic Audio it plays the thunder1 file, 2 sends thunder2 and 3 sends the witch cackle.&nbsp; I plan on adding more later to play them at different volumes, but this will do for now.<\/p>\n<p>A simple node-red automation triggers the cackle when my MQTT doorbell rings, and a small edit to my lightning LED code sends either a 1 or a 2 to trigger one of the thunder sound effects.&nbsp; Mission accomplished\u2026 now I just need to find some bigger speakers to use for trick or treat night.<\/p>\n<p>Thanks to all my patrons over at patreon for your continued support of my channel, if you look forward to these videos each week and you\u2019re interested in supporting my channel, check out the links in the description.&nbsp; If you aren\u2019t subscribed yet, and you enjoyed this video, please consider subscribing, and as always, thanks for watching the hookup.<\/p>\n<h3>Arduino MP3 Player: <a href=\"https:\/\/amzn.to\/2QEQlxz\">https:\/\/amzn.to\/2QEQlxz<\/a><\/h3>\n<h3>MP3 Player Code: <a href=\"https:\/\/github.com\/cefaloide\/ArduinoSerialMP3Player\">https:\/\/github.com\/cefaloide\/ArduinoSerialMP3Player<\/a><\/h3>\n<h3>MQTT MP3 Player Code: <a href=\"https:\/\/github.com\/thehookup\/Misc\/blob\/master\/ArduinoSerialMP3Player_CONFIGURE.ino\">https:\/\/github.com\/thehookup\/Misc\/blob\/master\/ArduinoSerialMP3Player_CONFIGURE.ino<\/a><\/h3>\n<h3><\/h3>\n<h3>Support my channel:<\/h3>\n<h3>Patreon: <a href=\"https:\/\/www.patreon.com\/thehookup\">https:\/\/www.patreon.com\/thehookup<\/a><\/h3>\n<h3><\/h3>\n<h3><\/h3>\n<h3>Visit my website: http:\/\/www.thesmarthomehookup.com\/test_install<\/h3>\n<h3>Follow me on Twitter: @TheHookUp1<\/h3>\n<h3><\/h3>\n<h3>Music by www.BenSound.com<\/h3>\n","protected":false},"excerpt":{"rendered":"<p>Today on the hookup I\u2019m going to show you an Arduino compatible MP3 player board that you can use to add sound effects to your projects, but more importantly, I\u2019m going to show you how to decipher other people\u2019s Arduino code to use in your own projects. As part of my Halloween decorations I had [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2586,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4,5],"tags":[],"class_list":["post-490","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-projects","category-tutorials"],"acf":[],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/490","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/comments?post=490"}],"version-history":[{"count":4,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/490\/revisions"}],"predecessor-version":[{"id":2341,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/490\/revisions\/2341"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/media\/2586"}],"wp:attachment":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/media?parent=490"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/categories?post=490"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/tags?post=490"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}