Music shield triggered by PIR sensor

I recently got a Seeeduino music shield, and I spent last night and a bit today trying to get it to be triggered by a PIR sensor, so that the music would turn on when there’s movement and turn off when there’s not been movement for 5 seconds.

It took a bit of tinkering and studying. Initially, I could just get it to turn on with movement and then run through a full song. But today I was finally able to get it to work.

The music shield library has rather ugly code with a lot of extraneous commented-out lines and little documentation. But it is rather easy to follow and to get working. Most importantly, it was quite easy to do the modifications to handle the PIR sensor information. I did need to make a change to the actual library, which is less than ideal, but it was only a couple of lines.

The file player.cpp contains a function AvailableProcessorTime() that gets called repeatedly in order to handle user interface events. Then there’s a global variable playStop that indicates whether the player should be playing (playStop == 1) or stopped (playStop == 0). So I just needed to add a line to check the sensor and change playStop accordingly.

What I did was create a generic user-defined function,

void userInterruptFunction(void) { /* ... */ }

that gets called within AvailableProcessorTime(). The user would need to be sure to define that function, but it could be left empty, or it could be used (as with my code) to check a sensor and then change a setting.

The code for my project is at my github page.

Advertisements

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s