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: Accelerometers

0 (0 Likes / 0 Dislikes)
[ADOBE DEVELOPER CONNECTION] [Kevin Hoyt, Platform Evangelist] Hi, I'm Kevin Hoyt, Platform Evangelist with Adobe. In this session, we're talking about integrating hardware with Flash, specifically Arduino hardware that you can find out more about at with Flash. [For more on Arduino visit] In this case, on top of the Arduino board, I have a prototyping that pulls the headers from the Arduino through for me to work with, and, also, it gives me this nice breadboard for me to work with to prototype things on. Now, in the other sessions I've actually gone ahead and connected the hardware for you specifically because I always felt that that was one of the hardest challenges, trying to learn how to do this stuff, is looking at these kind of random pictures or schematics and trying to figure out how to wire them. In this case, though, this is an accelerometer chip like you might find in your phone or your Wii game console. And there's six different places to connect it to. That's a little bit too long for me to sit here and just wire it all up, so those are already prewired for us and ready to go. It's relatively basic, though. There's a clock pin, a data pin, power in, power out, and that's pretty much all there is to it. The hardest part is just making sure you get it seated right so you can get all the different wires in the right places. There's actually one pin on here--if you get to go try this guy-- there's one pin on here that you don't use at all; it just sits open. So don't be worried if you don't have all the pins connected to something. So this handy accelerometer will let us detect changes in the tilt of the surface. So as we tilt the board one way or another, we'll actually get data from the accelerometer about how far we're tilted one way or another. This accelerometer is pretty cool. It actually also does drop detection, so if we wanted to detect a fall or someone dropping the board or whatever it maybe was contained in, we can go ahead and capture that data and represent that in our user interface. So let's take a look at the code that goes on the accelerometer. Now, there's a lot of variables and different settings in here. Again, because there's six different pins, there's also some math that has to happen to be able to deal with how far things are tilted. I won't deal with too many specifics other than just kind of setting up the baseline of the setup and the loop construct that go with any Arduino program. So in the setup here, we're going to go ahead and say 'Serial begin'. That essentially is allowing us to be able to listen to serial data to and from the Arduino. And then we have a 'pinmode' for the different pins. We're setting up all the different pins on the board. And then we're going to go ahead and synch them up and tell them to just chill out for a moment. Then we'll go ahead and, once they've kind of chilled out and settled, then we'll fire them back up and be ready to go. So we've a set of 'digitalwrites' low, delay for a second, for one microsecond, and then go ahead and fire those pins back up. So now that they're fired up and ready to go, we enter the loop construct. And there's functions already in this particular program that allow me to get the values from the accelerometer. The code for this session and all the other sessions will be available on my, where you can get this code. In this case, you can see it's pretty straightforward. It says 'GetValue', and it gets a value from a certain register, which is going to be telling us how far that register, essentially, is tilted. I'm going to use the 'Serial.print' to print that value. Then I'll 'Serial.print' a comma, and that comma will separate the next value, which is how far it's tilted on the X axis, how far it's tilted on the Y axis, how far it's tilted on the Z axis, or shifted, essentially. And we'll send those values and comma-separated list, and then we'll send the all-important '0, BYTE' there at the end. That '0 BYTE', or that null BYTE, actually is, again, going to register in our XML socket over in Flash, which is how we're going to listen to this data coming in, and it will actually raise--that's how its triggers raises the event for us to go ahead and get that data and work with that data. All right, so that's the basics of working with it. Let's go ahead and connect the Arduino up. I'll put that USB port in there, this USB port in here, and with them connected, I'll go ahead and go to my tool bar, my Arduino IDE, and upload this particular program. So it's going to go ahead and compile it and then send it over to the board. It will let me know when it's done. It takes just a moment. So it's done now, and so it's running. The program is running. Now we can go over into the Flash world and see about getting that data. Now I want to mention that there's a middle piece here, TinkerProxy, also called Serproxy, which is actually kind of taking the data from the USB port, which Flash doesn't have access to, and broadcasting it over a socket, or, essentially, making it available on a network for a socket to connect to. That's exactly what we're going to do from Flash, using the XML socket class. So here is my application. I have a picture of a cat. What we're going to do with this picture of a cat is tilt it based on how I'm tilting the board. So tilting the cat around, making it an interactive piece of Flash content based on the tilt of the board. That might be a game or any other types of different visualizations. The code for it looks something like this. The main parts and pieces here is this XML socket, so we're going to use the XML socket class, not the socket class. And down here in my 'init' function, I say 'XMLSocket' as we use a new XML socket. I'll listen for '', and that's, again, going to be fired off by the null BYTE, or the 0 BYTE, we're sending from our Arduino code. That actually raises the event, we get that comma-separated list of how far it's tilted and the different directions, and then we'll go ahead and tell the socket to connect out, in this case, to that TinkerProxy, also called Serproxy, to listen to the data coming across the USB port. Now, when we get the data, I'm actually going to split them off. In this case, I'm only interested in two different tilt aspects. You can obviously do more. I'm also not worried about fall detection or anything like that in this case, just simply how far it's tilted either on the X or Y axis. So we take the data that comes across to us from the wire; it's a string. We split it based on the commas that we put into it from the Arduino. And then that gives us three values--X,Y, and Z. And then we go ahead and translate those into amounts of tilt. Now I've gone ahead and set up values for the maximum range of the numbers that we'll get from the accelerometer. And I'm going to go ahead and divide the number we've currently got by the total value and multiply that by 90 for 90 degrees of a tilt. So now we can go ahead and tilt that image in our application based on the accelerometer. Now you also notice I'm using TweenLite here. In my experience, when you're doing real time kind of stuff like this, having a Tween engine helps kind of smooth out how things are displayed. So let's go ahead and let's take a look at that. Run my application here. So we have the cat sitting in the screen. It's actually already slightly tilted, but let's go ahead and tilt it up. You can see that the cat is shifting down. If I shift it the other way, the cat tilts in. I can shift it back down or rotate it over and over, so we have, actually, full real-time control over the 3D positioning of this particular picture based on input from our accelerometer. Set it back down. It settles out. I think it's interesting that the picture is just slightly tilted off, and that's because of the levelness. It could be the levelness of the table, it could be the levelness of the chip, it could be the breadboard thickness. There's lots of different things, but the point being is it's really sensitive, which really opens the door for some really interesting visualizations. So that's connecting an accelerometer on an Arduino into Flash and interacting with the content in the Flash world. That's it for this hardware session. Lots more to come. I'll catch you next time. [For more information,,] I'm Kevin Hoyt. [ADOBE DEVELOPER CONNECTION]

Video Details

Duration: 8 minutes and 40 seconds
Language: English
License: All rights reserved
Genre: None
Views: 142
Posted by: adobetv on Oct 21, 2010

Accelerometers detect the tilt of a surface and are found in phones and game controllers. Join Kevin Hoyt as he guides you through adding those functions to your Adobe Flash applications.

Caption and Translate

    Sign In/Register for Dotsub to translate this video.