Watch videos with subtitles in your language, upload your videos, create your own subtitles! Click here to learn more on "how to Dotsub"

Physical Computing with Arduino: Ultrasonic Range Finder

0 (0 Likes / 0 Dislikes)
[Adobe Developer Connection] [Kevin Hoyt - Platform Evangelist] Hi, I'm Kevin Hoyt, Platform Evangelist with Adobe. And in this session, we're going to be talking more about integrating Arduino and hardware sensors with Flash. So I have my handy dandy Arduino board here. And I have on top of it a prototyping shield, already plugged into the Arduino board and exposes all of the Arduino parts and pieces and then an area for me to prototype with. For this particular session, we're going to be talking about this guy. This is a PING ultrasonic range finder. So essentially what happens is that this guy makes a little ultrasonic chirp. The sound goes out. Whatever is in front of, bounces back and it detects that, so then it can know how long it took for the sound to go from whatever is in front of it and back. The speed of sound being constant, we do the math and then we can figure out exactly how far away that object that's in front of the sensor is. So this is a way to measure distance for our applications. It expects three little wires, one for ground, one for 5 Volts, so our voltage in and our voltage out, and one for the data. So I like to take this guy and plug it in right on the edge of the board here. And then we go ahead and let's connect our power, which I put right in here, and that is going to go to our power pin on the protoshield board. So I've got power. I'm going to do ground. I just like to do power and ground first. So I'll go ahead and do ground. I'll go ahead and put ground into the ground pin on the Arduino board. So now we have a way to power it. Now, we need a way to get data off of it. I like to use a little yellow wire for data, and so we'll go ahead and put our little wire into the hole here, and in this case, it's actually going to be coming in off of pin 2. So let's go ahead and put that in here. It's kind of a tight squeeze with board being there. And you want to make sure that the wires aren't blocking the sensor at all, otherwise you'll detect the wire and get a false reading. Now, they make lots of different types of ultrasonic range finders you can find out there. It's really going from Maxbotix that you can even get with a noise canceling built into it, so if you're trying to detect distances ultrasonically in loud places, so some pretty cool stuff. So there's the board configured, and then we're going to go ahead and plug that in. And then plug it into our USB port. So now we can detect the distance. Now, in our Arduino program that we're going to put onto the board-- let's take a quick look at what that consists of. There is a setup function here, and that is going to go ahead and just get ready to do serial data exchange, to and from the board. In this case just from. We'll be getting the data as it comes off of the board for how far away something was. And then we get into our loop construct. Now, there's a little bit more code that goes on in working with the ultrasonic sensor, so I'm going to kind of skip down here a little bit to this guy, which is duration = pulseIn for the pingPin, which is the number 2 pin that we plugged our data wire into on high. And so what it will essentially do it pull off how long it took to go from the sensor to the object and back. Now, if you divide that by 2 for one trip out, then, again, divide by the speed of sound, you can figure out how far away that is. I've got that put off in another function down here. that does the math for me, but essentially the net result of that is how many inches away I am. So at that point I want to take the inches value, and then go ahead and send that off to the serial port, so we send that off into the computer. I'll send the inches measurement as a decimal, and there's the all-important zero or null byte. And that's going to be the key to the XML socket to be able to capture the data and do something with it. I'll go ahead and send this on to the board. We'll click our upload button on the Arduino ID. It's going to compile that, put that onto the chip,and it will let us know when it's done. And now it's done, so now we're ready. Now, in this case, I already have tinker proxy or sir proxy, a serial to socket proxy, running. And so it's essentially taking the data in from the USB port and broadcasting it on the network, so that I can connect to it via socket. And so let's go ahead and jump into our Flash program here. So in this case I have a picture of a cat on the screen, and you may be familiar with the fact that we added 3D support to Flash Player in Player 10. So what I am going to do is actually leverage that 3D capability and control kind of the depth of the image, based on how close or far I am away from the distance sensor. To do that, we'll take a look at the code here, a few kind of key parts and pieces. The first part is down here in this init method. I'm going to go ahead and create a new instance of XMLSocket. Now keep in mind, this is XMLSocket, not Socket. Then we go ahead and listen for the data event. And, again, that data that is triggered by the null byte, so it's very important. And then we go ahead and connect, and in this case, it's local host with a port of 5331, which is where I have that tinker proxy or sir proxy running and listening to the USB port and broadcasting that over the network. Now, when data comes in, I'm going to go ahead and take that. It's going to come across as a string, so I convert that to a number, and then that will give me how many inches it is, and then I just do some basic math. In this case, the distance sensor has a range of about 10 feet. And this one is a pretty broad sweep of how it looks at the world. You can get other ones that are more narrow or have different distances ranges and things like that, but in this case, what I do is I take the distance and just run some basic math and division to figure out how far into the distance that picture should be. Now, let's go ahead and run the application. And so right now, if I actually take the sensor and move it closer to the screen, the cat gets closer. If I move it further away from the screen, the cat gets further away. And so I can control the z order of that cat, based on the distance detected by the sensor. So, I put it really close, really far, right. So now we have distance capabilities in our Flash program. That was a pretty simple one. And the great thing about the 3D capabilities in Flash Player is that if we look at the code, It's literally just a little more than just pushing the z property. So you have x and y properties for positioning things in x and y. You also have now a z property for pushing things into the z index. So that's a PING ultrasonic range finder on the Arduino talking to Flash. That's what we did for this session. There's lots more hardware and sensors to go around, so keep in tune for the series, and until I see you again, I'm Kevin Hoyt. [Adobe Developer Connection]

Video Details

Duration: 7 minutes and 23 seconds
Language: English
License: All rights reserved
Genre: None
Views: 178
Posted by: adobetv on Oct 21, 2010

Sound travels at a constant rate of speed. Join Evangelist Kevin Hoyt to learn how to harness that trait to determine an objects distance with Arduino and Adobe Flash.

Caption and Translate

    Sign In/Register for Dotsub to translate this video.