What does the Imperial March, Windows Phone 8, Netduino, Bluetooth and Sonar have in common? This!
- Posted: Feb 22, 2013 at 6:00AM
- 6,543 views
In today's Hardware Friday Project takes us back, Windows Phone 8, Netduino and Bluetooth..., to the very cool work that Matt Cavanagh is doing. Chris Walker of Netduino fame was impressed enough to reach out to us to point out the cool stuff Matt's doing now.
I mean how can you not dig a Windows Phone 8, Netduino, Sonar and the Star Wars Imperial March all in one project!?
Following my previous post on Bluetooth with the Netduino, I thought I would extend it a bit.
The goal for this project is the following: Have a Netduino sending it’s orientation data (XYZ) over Bluetooth to WP8 which displays a 3D representation of it (at the right angle of course), and for bonus points attach a potentiometer to control the zoom.
In the video above, as with the last one, the Netduino is only plugged in because I don’t have a 9v battery lying around. And you might notice a few MS of lag – that’s due to the smoothing filters. It’s possible to get the movement to just about real-time (Bluetooth is impressively fast) but then you also get all the fluctuations of the accelerometer.
For the phone app I’ve copied pretty much the entire code from the last post. Some is commented out because it referenced UI stuff that isn’t in this project. Because the previous post covered all this, I’m not going to go into any of that code.
So how do we show a 3D model? Well, in my opinion the easiest way is XNA. However, Microsoft killed XNA on WP8 (and W8) leaving native code as the only way. Enter: MonoGame. MonoGame is a framework that enables XNA developers to write one code base and have it run on many different platforms. So even though XNA isn’t officially supported on WP8, we can still use MonoGame – which is pretty much identical (at least for anything we’re going to be doing). Here is their explanation of it:
Okay, okay.. I know, "Star Wars!...", here you go...
OK, maybe not with your mind, but at the very least, your hand.
The objective here was to add a sonar (ultrasonic) sensor to the Netduino, work out how close my hand was, and then send that info over Bluetooth to Windows Phone 8 and display it. Bonus points for doing something useful with the data (yes, controlling the volume of the Imperial March is totally useful!).
What you need for this is:
- Netduino or variant
- Sonic/sonar sensor
- Bluetooth Module
- Windows Phone 8
few months ago I bought this Sonar sensor: HC-SR04 (http://lightake.com/detail.do/sku.UltrasonicSensorHCSR04DistanceMeasuringModuleBlueSilver-58594). I got it from a random little online Chinese shop for $4. I literally just bought it to see if they would actually deliver (free worldwide) because they have quite a big range of Arduino/Netduino sensors and it’s hard to get things locally. Surprisingly it actually came.
So last night I opened it, tweeted, and plugged it into the Netduino:
VCC - 5v
Trig - D6
Echo - D7
GND - GND
This site has a great explanation about how these work: http://arduino-info.wikispaces.com/UltraSonicDistance
They have two transducers, basically a speaker and a microphone.
Ultrasound is a high frequency sound (typically 40 KHz is used). A short burst of sound waves (often only 8 cycles) is sent out the "Transmit" transducer (left, above). Then the "Receive" transducer listens for an echo. Thus, the principle of ultrasonic distance measurement is the same as with Radio-based radar.
After understanding it I started coding a little helper to do the math for me, and realized that someone would have done this before – and proceeded to find this perfect little class by J Wilson on the Netduino forums: http://forums.netduino.com/index.php?/topic/3256-library-for-hc-sr04-ultrasonic-rangefinder/
Download that class and add it to your Netduino project.
Create a new WP8 project just like before and add the Bluetooth stuff like in the first post.
Then I have a very simple UI. It’s got a ProgressBar to visualize the volume, a MediaElement to play the song, and some TextBlocks to show the current value.
This would have probably been a good time to show a very basic use of MVVM, but I will leave that for another day.
Besides the Bluetooth code, there is really only one method that does all the work here, and it’s really simple. This method is called when a message is received over BT. The message in this case will be the CM distance value from the sonar sensor:
And yes, of course the source is available for both projects...