Finish the Slouch Alert and Make Noise!
by push_reset in Circuits > Wearables
3404 Views, 5 Favorites, 0 Comments
Finish the Slouch Alert and Make Noise!
Congratulations! The slouch alert is done! In this lesson, we will continue to test and play with the slouch alert circuit and learn how to trigger sound with it. That's the beauty of working with a microcontroller, you can use the same circuit and program it to have different behaviors.
After the Slouch Alert is tested and working you learn how to make your LilyPad USB into a keyboard. The LilyPad USB can produce keypresses that can trigger sound files when used with the right software. Let's get to it!
Test Slouch Alert Circuit
In the previous lesson, you finished the circuit and tested your connections along the way. It is now time to test the circuit again to make sure it's working before you try it on. The more you test a circuit throughout the creation process, the less amount of steps you need to backtrack once the circuit stops working.
Instead of using the battery let's test it with the USB cord plugged in. This way we can see the sensor's values and know if the sensor is working properly and whether the vibe board is turning on when it is supposed to.
Open Arduino, plug the in the USB cord and turn the board on. Open the slouchAlert sketch. This sketch was already uploaded during the Slouch Alert Circuit lesson, so no need to hit upload again. Remember that the sensor won't be read until the snap switch is closed so when you open the serial monitor there won't be any values printing in the monitor. Go ahead and close the switch and watch the values jump onto the screen. Press or bend the flex sensor to make it hit the threshold of 400. The word "slouch" will print in the monitor and the vibe board will go on.
Wear and Calibrate Slouch Alert
Now you know the circuit is working but you will need to test the sensor while it's on too. Right now the value for the flex sensor to hit that tells you whether you are slouching or not is 400. This is the default and will change based on what you read in the serial monitor once the sensor in on and you are you slouching.
Turn the LilyPad USB off. With the board still plugged into the computer put the t-shirt on. If this proves to be too difficult, unplug the cord from the computer, put the shirt on then replug the USB cord. Turn the LilyPad board back on.
Open the serial monitor while the slouchAlert sketch is open and observe the sensor's values. Slouch and straighten like you did in the Read Body Movement lesson to see if the current threshold works for you. The threshold right now is 400. You can see below where it states that in the code.
As a refresher let's go over where this is found in the sketch again. Below is the part of the code that says - if the sensor value is more than 400 turn the vibe on and print "slouch" in the serial monitor. Otherwise, turn the vibe board off.
If it's already above 400 and detecting a slouch while you are straight you need to raise the threshold. If it's difficult to detect to a slouch lower this number.
if (average > 400) { Serial.println("slouch"); digitalWrite(vibeBoard, HIGH); delay(1); } else { digitalWrite(vibeBoard, LOW); }
Make It Mobile
Now is the time for the project to become mobile! The circuit is done, tested, and the sensor is calibrated. Plug the battery in if it isn't already and unplug the USB cord. The circuit is now untethered and you can walk away from the computer! Test it out and make sure to take a picture to share at the end of this lesson while it's on!
Use LilyPad USB As a Keyboard
In the next few steps, I will go over how to make your LilyPad USB trigger sound files. You can think of this as bonus material... a little going away present to get inspired from for more projects to come. It uses the same circuit you just built for the Slouch Alert project and a simple sketch that I will explain later on. Ready to make some noise? Let's do it!
The thing that sets the LilyPad USB apart from other LilyPad boards is that it can be used as a keyboard or a mouse. This means that when a switch is closed or when a sensor hits a threshold the LilyPad USB can produce a keypress of a character, just like if you were to hit a key on your keyboard.
Before we go on, I must say that there is a warning that comes with turning your LilyPad USB (or any microcontroller) into a keyboard. You need to build an on/off switch into the circuit. This is because once you make it a keyboard it can get stuck printing a character and can take over your computer prohibiting you from uploading a new sketch to your board and many other things. This is partly why the snap switch is part of the circuit, so you can turn on and off the keyboard emulation while still being able to upload a new sketch to your LilyPad USB. This will all make more sense after you have uploaded the sketch and experienced it.
Download the attached musicTee file. In it are three sketches and sound files to get you started.
Downloads
Upload
To use the LilyPad USB as a keyboard you will need to tether it back to the computer. I know it can be a drag but wireless serial communication will need to wait for another class. :)
Plug the USB cord into the board and open up the musicTee sketch in Arduino. Check the board and port, then upload the sketch. Now open the serial monitor to see your sensor values. You will get the below error but you already selected your port. So, why does this pop-up?
It is because the LilyPad's port has changed to an HID (human interaction device) type port and will need to be selected again under Tools > Port. This means that your board has begun to emulate a keyboard because a keyboard is an HID. You will need to select this port every time you upload a sketch using the keyboard feature.
Now open the serial port and remember to close your snap switch in order to see the sensor values. Press or bend the sensor to see the character "n" print in the text field at the top of the serial monitor and "play" print in the window. Woohoo! You are ready to trigger some sound - well, almost. First, let's quickly go over the new parts of this sketch that makes this all possible. Keep the t-shirt plugged into the computer but turn the LilyPad board off.
The Code
So how does your board become a keyboard? There is some magic going on under the hood in the software. Arduino software can do many things and some of these features are available through the use of libraries. Arduino libraries come bundled with the Arduino software or you can download and install libraries to add more functionality. You import libraries into your sketches so you can use the library's features throughout the sketch. Want to know more? Check out Arduino's website page on libraries here.
Let's take a look at where the library is used in the musicTee sketch.
To import a library into a sketch go to Sketch > Include Library > choose a library to import. It will add a line at the top of your sketch that starts with
#include
The musicTee already has this line at the very top which imports the Keyboard library.
#include "Keyboard.h"
Start emulating a keyboard by calling Keyboard.begin() in setup().
If the sensor value held in the variable "average" goes over 250 print "play" in the serial window and print the character "n" by emulating a keyboard press using Keyboard.press(). Emulate pressing the key for 100 milliseconds using delay() and then release the key with Keyboard.releaseAll().
if (average > 250) { Serial.println("play"); Keyboard.press('n'); delay(100); Keyboard.releaseAll(); digitalWrite(vibeBoard, HIGH); delay(1); }
Download Soundplant and Load Files
To trigger sounds with your LilyPad keyboard you are going to use a free piece of software called Soundplant created by Marcel Blum. Head over to the website to download and install Soundplant based on your operating system (sorry Linux!).
Open up Soundplant from your Applications folder and a virtual keyboard will pop up on your screen. This software is really fun and super easy to use. I love it because you can quickly change sounds whenever you like. Open the folder that contains the sound files you downloaded earlier in this lesson. Choose one and drag and drop it onto the "n" key. Check to see if your LilyPad is still off (it should be). Press the "n" on your computer's keyboard and the sound file will play.
Try dragging and dropping other sound files. Soundplant's website has a great list of where to get more free sounds on the download page.
Trigger Sound With LilyPad USB
At this point, you have gotten "n" to print and you've gotten sound files playing with Soundplant. Now let's put these two things together. Turn on your LilyPad USB and the musicTee sketch that you have already uploaded to the board will start running. While in Soundplant, bend the flex sensor and the sound file on the character "n" will play. You have just created a gestural musical controller!
Hooray!
You now know how to make your own sensor, how to sense a gesture, and how to trigger events such as haptic feedback and sounds. The sound triggering can get really fun when you add more sensors.
:)
Washing Wearable Electronics
The first thing to know about washing a wearable: take out the battery! The other things you need to consider before throwing your threads into water are:
+ Do my electronic components have any open spaces for water go and get trapped in or exposed delicate parts? For example, buzzers, speakers, and mics.
The LilyPad series of electronic components that you have been using in this class are made to be washable. However, there is an exception. Head over to the LilyPad buzzer hookup guide. When you scroll down you see that it says "This particular LilyPad component cannot be washed." Why? If you look at the component it has a small hole in it where the magnet and coil is. If water gets in there, it can lead to corrosion and damage the delicate mechanism that needs to stay intact for it to make sounds. You will find other wearable and sewable components for sale that are also made to be washable.
What about other components? If a component consists of a sealed package and is not receiving power it can likely be washed, dried and survive. Checking them for any holes or delicate parts can be a good enough indicator for whether they can make it through a washing or not.
+ What kind of fabric are my components sewn to?
The garment or accessory your circuit is on has its own washing needs. It's washing needs are based on what kind of fiber it is made from. To find out look at the tag, take note in the fabric store or do a burn test. For example, if you put a wool sweater in the washer on hot and dry it on hot, it will shrink and felt up. This applies to the 100% wool felt you use in this class. Once you know the fiber, look up that fiber's washing needs and wash it accordingly. Make sure to think about any added pieces! For example in this class, you added 100% wool to a t-shirt. The t-shirt will have different washing needs than the wool, use the directions for wool which is the most delicate.
+ Hand or machine wash? What about dry cleaning?
Dry cleaning uses chemicals, so avoid when possible. You can wash most items that need dry cleaning by hand in cold water and air dry them. Some silks will get discolored if it touches water, so if a garment is silk and says "dry clean", then be mindful.
If the project is built on material that is machine washable, check for delicate areas and machine wash according to the fiber content. If machine washed, you may want to put it on a delicate setting to prevent parts from wearing down faster.
If a project seems delicate or is small and handmade I like to hand wash it. Delicate parts could be embroidery or connections that may not be that strong. Soldered joints can be delicate and some solder contains silver and copper which corrodes when wet and exposed to air.
Always dry your electronics completely as quickly as possible to prevent corrosion.
I recommend the same washing method for all the projects you make in this class because they are all made from 100% wool, a material that needs careful handling or it will shrink significantly. Follow along with the instructions I've illustrated using the Hi-5 Collector for washing all the handmade parts of your class projects.
Remove the battery. The LilyPad USB can stay on the project, but I recommend removing it to make cleaning and drying easier and faster. I recommend hand washing the Hi-5 Collector in cold water and air drying it. If there is a visible stain, I recommend spot treating the piece to preserve the shape of the project. This is because it's made of 100% wool that will shrink when heat and agitation are applied. It is also a non-woven material that can stretch out of shape while wet and if it's not kept an eye on. I do not recommend machine washing because since it's 100% wool. If you find that your project has shrunken a little, which I would expect from 100% felted wool, rewet the wool and stretch it out! That is the great thing about wool, it's very malleable.
Remove the battery.
Even though the t-shirt's care tag tells me that I can machine wash it in cold and tumble dry on low I will hand wash it because of the 100% wool sensor and switch additions.
Be careful when washing the pin/patch. If you used a marker for any drawn details that was not a fabric marker the details may bleed when they come in contact with water.