[SOUND]. Hello. In this lesson I'm going to teach you how to access accelerometer data from your mobile device. most mobile devices have an accelerometer in which essentially it allows you to figure out what angle the device is being held at. And of course, we're going to use this for creative purposes, to control sound and visualization. The accelerometer will give you three values which relate to the various angles from the phone, so it can be twisted like this or like this, or like this. So lets have a look at those. So, here's some code which will, will create an accelerometer and print out some data from it. So, let's run this on the phone, and see what comes out. First of all, I'm going to type it in, so we need at the top, we create a global variable. And the type of the variable is accelerometer. And this is a built-in class for those of you who've done a bit more programming. Which comes with our audio and graphics API. Let's call, I'm going to call it XL. So in set-up. As usually we create teh variable XY equals new acceleromter. Okay so that's. What we're going to do and now I can quere that accel to now get some information. So lets just for example print out the X value. Okay so I has an X Y and Z value. Well we put them all out actually and we'll do X is Y is, and Z is. Okay. And I just need to edit this. So that is getX, getY, and getZ. So I've got 3 lines in my draw method there. So every time draw gets called it's going to ask the accelerometer, what is your current X position? What's your current Y position? What's your current Z position? Okay, so let's run that on the device, and we're going to just look at the console to see the numbers that are coming out. Right, there are the numbers coming out of the device. That's telling me the current position of the device. Now if I pick it up. And start moving it around. You can see depending on which angle I'm changing at different numbers are produced. And basically the range is from minus ten through to ten. So when you're at the most extreme angle the numbers will go up to ten. And, when you're at the opposite extreme they'll get a minus ten. So the values that come out from the accelerator are in the range minus ten to ten depending on the different angles you're holding the phone at. So how can we use this creatively? Well, first of all let's just see if we can use it to set the color of the background. So we're going to do this. So we'll create some variables, which are going to be our red, green and blue. And we can set the red value using the accelerometer data. And then the green, and blue. Right. But remember, these values are in the range minus 10 to 10. And the range, the range that we need for, colors is 0 to 255. So well done if you've spotted that already now what can I do here so to get into the range 0 to 255 so I add 10. Okay that puts it in the range 0 to 20. And then, I can just. Do an approximation here and just times it by 10. So it goes up to 0 to 200. Okay. That's near enough. You can calculate that accurately by dividing 10 by 255 by 10. sorry, 255 by 20. so we could do that. We could do 255 by 20 to do it really accurately. [SOUND]. Okay. So let's do that with the other ones as well. [SOUND]. Right. And then, now this, remember that these that come out are things like 0.15 or -9.3 which are floating point numbers. But I've declared my RGB's as integers. So what I need to do is then cast it, convert it to an integer before I can use it. Finally, I'm going to set my background color using these values R, G, B. Okay so, we've used the accelerometer data to set up some red green and blue value and then we're going to use that to set up the background color so lets hit play. And see what happens to the background color of the screen as we move the device around. [SOUND]. Okay. So we pick up the device. And as I move it around, you can see the background color changes. Now it, it seems to be changing kind of slowly, so it kind of jumps from one color to the next. That's because the accelerometer data updates quite slowly. So remember, for example, the audio, what we did with audio data, that updates very fast. so you know, that's very smooth and very rapid, but it's, other types of data are slower. So, for example, the frame rate in processing, which is the number of times a second it cause a draw function is, is, the default is 60. Whereas the number of samples a second is 44,100 when you're dealing with audio. So there's all these different rates of, of data flow. So what we're dealing with is a flow of data from a sensor in the phone which is coming in at certain rate. that's not very high rate in this case. Okay so that, that's dealing with accelerometer data and sending it to the background color. In the next section, I'm going to talk about how we can map that accelerometer data to a playback control. So there's a line here which if you're quick you're going to. Pause the video and quickly go and type it in, see what it does. So lets just lets just put the extra code we need to be able to do this line here. OK. So back to here. So now I need to create an audio player and I want to control the speed with the accelorometer. So I can twist the phone around and it will slow down and speed up the, the playback of the audio. So we do the usual thing, create the maxim variable, create an Audio Player variable, and then initialize it, maxim equals new, maxim. This should be familiar by now. Oops. And audio so player equals maxim.loadFile, and the name of the file. So, I'm going to use the, the beat we've been using. Okay, so thats the player. So in the draw method, we just make sure the player is playing. In fact we can. So we do player play. If the player is already playing we would just ignore that. Just make sure its playing. then, what we're going to do is say, player, and set the speed. Now, let's, let's do a calculation here. We're going to do this as a float, because remember speed so if we pass in a speed of two, then it will playback the audio twice as fast, so its going to skip through all those samples twice as quickly. If we, if we set the speed to one it will play back at the normal speed so it will sound as it was originally recorded. now, so, the kind of, an interesting range for speed, as we've seen with the DJVJ. It might be zero through to two. So we need to get that accelerometer data, and scale it to the range, zero to two. Just an organizational note, that I've just placed these variables from the middle of the code up to the top of the method. Now, everyone has their own way of doing things when it comes to programming. There's many ways to, to program and to organize your code. For me, I quite like to put my variables at the top of the method so that it's really clear. All of the variables I'm going to use in that method. its a, its a neater way to do it in my mind. So, so I've just placed those at the top there. Now, the speed. Remember, I said an interesting range for the speed was, say, zero to two. So what we're going to do is take that accelerometer data like, this, okay? And we're going to scale it to the range, zero to two. So we're going to do add ten. So that puts it in the range zero to 20 again. And then we can just divide it by 10 and that will put it in the range 0 to 2. So then we use that set the speed. So lets see how that goes. I'm using X so that's me twisting the phone like this. So hopefully what this is going to do is is if you like replace what we did last week which is the having to touch the screen to change the speed now we can actually just move the phone to change the speed. So it becomes maybe a more embodied experience. More like a real instrument where you move around and it changes the sound. So lets lets run that. [MUSIC] Okay so I pick the phone up and you can hear the speed's already changing now if I tip it that way, [SOUND] you can see it's. >> [MUSIC] It's now going really fast. And I'll twist it back around. I can take it to a stop. So let's just try and play with that a bit. [SOUND] So, you can see it becomes a really fun thing to do and, and once you, if you got a few sounds going, you can start really jerking the phone around and getting quite creative with the way you perform with that thing. So it becomes like a rather than it being, if you like. An interface that you tap on becomes an instrument that you can, you can really try to perform with. I would recommend that you try some different sounds in there. Try just record tones, try recording anything really and sticking in there and see if you can map different properties of the sound to the tellurometer data there. [MUSIC]