loading
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.


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.
:)

CLASS PROJECT

Share a photo of your finished project with the class!

Nice work! You've completed the class project