Monday, January 4, 2016

How To Do A Science with Grant

One day we were sitting around drinking and watching the netflix and we remarked on how fun it would be if every time one of us came in the house a sitcom style studio audience reaction took place. Like a swell of applause and laughing, a twang of bass guitar, a ladder smacking a window and a guitar riff followed by "Hey Sam", that kind of thing! We were so enamored with the idea (and the drink) that we decided that we would provide our own studio audience catcalls whenever someone entered. Usually the ever popular "WooooOOo" :


So after a while we grew tired of that and put away such thoughts of studio adulation. Until an idea sprang up on the webs of someone doing something very similar with the help of a robot! Over at Geekologie (aka the website that refused to publish a link of our Brief History of Beer clip which was awesome and quickly garnered more hits than stuff we worked on for weeks because they are jerks. Or maybe that was TheAwesomer. I can't remember. So many tough guy news/video aggregate sites that almost make original content, its hard to keep them all straight.) I first noticed noticed that with the help of an Arduino my dreams were possible! So after a little bit of research and a decision to make Morgan's Xmas awesome via science I set out to recreate this enchanting micro-robot. I couldn't find step by step instructions anywhere on the internet so I made things up as I went along!


Knock off Arduino : $9.99
1.) LANMU® UNO R3 Atmega328p Atmega16u2 2014 Version Board Arduino Mklg.  Now I dont know much about Arduino stuff but this one is definitely some kind of knock off (sorry nice folks that make arduino) that is the same thing only it costs about 10 bucks. As far as you know.  These sweet rigs are available on Amazon for cheap, and I imagine as long as you get a similar board (important that it is an Arduino Uno R3!) and you get it shipped direct from the heart of the Mongolian Empire, or wherever such things are made you could probably get it for even cheaper.

MP3 Shield : $9.00
2.) Music MP3 Shield by LinkSprite.  The important thing about this shield is that it is compatible with an Arduino Uno and plays MP3s, other than that you figure it out.  This sweet rig is very cheap and is going to be adding MP3 playback capability to your Arduino project, including a Line Out to pipe that sweet sound out.  There are many different options available, some include an onboard AMP so you can wire in speakers directly.  That model is like 3 bucks more expensive so I opted to not use it.  If you are feeling ambitious please feel free to do whatever you want.  I got mine on Amazon.

Magnetic Reed Switch : $6.00

3.) Magnetic Switch (aka a Reed Switch) Possibly the cheapest thing you'll be buying, or rip one off of the door at your local 7-11. They can be had by the dozen for a low low price.  You can buy either kind (Normally Open or Normally Closed) or just buy one you can change by attaching wire to the different connectors.  This thing is powered by magnets, or things that befuddle juggalos to no end.  They are much cheaper than an IR switch and in my mind much more reliable, so that your mom's cats walking by won't trigger the sensor.

22 AWG Wire : IDK LOL
4.) The rest is up to you, depending on how fancy you wanna get.  The only other thing that you for sure need is wire to hook the switch into the arduino.  I always have 22 AWG solid copper wire laying around the house because I do lots of science with re wiring all my arcade projects and I find it easy enough to work with.  I got super fancy and crimped them on to forked wire terminals that I then attached to Breadboard Jumper Wires and then attached to the ports I wanted on the arduino, but I had that stuff laying around, you can just poke wires in, solder them, use a breadboard, whatever you wanna do.

Very very fancy.
5.)  The last thing you need is something to play the music and some way to power the arduino.  As it comes with a USB cord to connect it to your computer you can just grab a spare wall brick (like the ones that came with your moms Iphone charger that she let me use last night.) or go with a wall power adaptor.  As for the sound you can go as ham as you want.  I hooked mine up to my brother's expensive guitar amp.  I'm probably going to get a decent soundbar speaker and hook it into that and make the whole package a bit prettier. We'll see.


1.)  Connect the MP3 Shield to the Arduino.  This is about as simple as it sounds.  Most of these things come with the male and female pins already attached so you build it like some kind of non trade marked interlocking brick toy.  That will look like this:

Double check your documentation or google to make sure you're not going to make it explode by turning it on.
2.) Prepare your micro SD card. Due to some complicated coding reasons I wanted to make sure I had mp3 file names that all had the same length of characters.  So all my file names are "XX.mp3" (10.mp3 being the first one that way there were no file names with fewer characters.).  Many of the mp3 shields you may be working with had file allocation table limitations and size limitations, I chose a 2GB micro SD card formatted to FAT16.  (Ask your mamma about my FAT16. Heh).  I loaded on all the sound effects and then inserted that into my mp3 sheild.  There is only one way to do this so if you can't figure it out just quit now.

Here is where I got the mp3s:

And some nice peaceful reddit internet warrior named splam ripped them all and cut them into individual mp3s. So click his dropbox full of virus beetles here:

3.)  Wire Up The Magnetic Switch.  Now, if you have fancy breadboard jumper wires scattered about your workshop this is a good time for them.  You can stuff 22 AWG wire right into a female end and make everything work without any soldering or ugly butt connectors.  So consider getting a few because they make everything much easier.  And you can get a bushel of them for like 8 bucks on amazon. I used one Male>Female and one Female>Female jumper wire in my application.

So here's a bit of science for ya, in order for a switch to work its got a signal wire and a ground wire.  When the switch is in crucuit then the wire is basically making contact with the ground and that is telling the tiny robot that holy shit something is happening.  In your case the thing that is happening is that the magnetic switch is open (because you opened the door!) so then we can make the robot do something about it.  Like play a sound.  So how do we connect all this stuff together? Especially if you have no idea how to do any kind of elite hacker engineering.  Check it out:

The wire on the left is connected to a port on the arduino, the right is connected to a ground.
The idea is that you connect one wire to the one that goes to COM. on the magnetic switch, (or your common ground. May just say Ground on your switch) and the other wire to Normally Open or NO. Because that's what we're testing for. So if your arduino reads a HIGH voltage then your switch is not making contact, so in your fancy code you'll be writing you'll get a value of 1, because its Open.  When its closed, or making contact you'll be getting a value of 0. The NO wire you'll be connecting to the 28th pin on your arduino, or the one closest to the reset button right by the USB jack.  For whatever reason that is referred to as the 19th Pin when you're writing code, yea I dunno why and i couldnt be bothered to learn. But trust me its the 19th pin and its the one closest to the reset button on the bank of pins on the right.  The Ground wire you connect to the only available ground pin which is found in a group of 6 pins just to the left of the pin you hooked up the NO wire to.  Its in a cluster with a 5volt pin so make sure you don't accidentally connect it to that one, since that may cause 5v of sweet electricity to go some place you don't want.  There are also some other pins in that area that are labeled with scary letters and I have no idea what they do, so use caution.

Here is a fancy diagram that indicates with giant pink and yellow arrows and pink dots the pins we're talking about:

4.) Now you start writing the code. So make sure you keep the other end of the magnetic switch closed by (like by taping it together, we don't want infinite Seinfeld music playing as you go to test it all out) and let's plug the arduino into the computer with a sweet USB cable that either came with it or you have from buying that shitty printer from target.  This is already getting a bit too long so I'm not going to explain how to download the arduino development and whatnot.  Follow documentation for how you get the libraries to work for the mp3 shield.  If I decide it is too difficult (took me a few tries while drunk coding) then I'll add that to the tutorial in an appendix.

Here, dig the code I wrote (while drinking):

Now in case you are wondering about the dozen or so lines devoted to generating a random number and then concatenating it with a file suffix, and then turning that string into a character array its simply because for whatever reason (I am not a clever man, ask Dan) the function that plays music doesn't accept strings.  Or maybe it does and I'm just not understanding the syntax.  But this works and plays a random mp3 file and doesn't require you to traverse the entire directory, or do anything other than follow my naming convention.  Radical, now upload your sketch to your arduino and brace for moderate excitement.

5.) Testing and Installation! Keep everything plugged into your computer and plug in some headphones or your speakers to the Line Out and marvel as when you hold the sensor together it does nothing but when you move it apart it plays a random Seinfeld riff!  Excellent.  Now go attach it to your door, and do whatever you have to do to get it powered .  I used blue painters tape to temporarily secure it for practical testing (as you can see in the video near the top).

I think we learned a lot here today.  If you have any questions or comments pipe in below, or if you want me to build you one bring the components and some beer to my house.  Big things are happening fellows, TK is very busy doing stuff with Aaron (which should be a thing soon), and my average blog output is <1 a year! Huzzah.