Using accel. as a trigger to move an object.

I have an object and I want the accelerometer to just act as a trigger to move that object on the X axis instead of using the value of “xGravity”.

local move = "" movePlayer = function ( dt ) &nbsp; &nbsp; if (move == "still") then &nbsp; &nbsp; &nbsp; &nbsp; player.x = player.x -- notice here &nbsp; &nbsp; elseif (move == "right") then &nbsp; &nbsp; &nbsp; &nbsp; player.x = player.x + 5 \* dt &nbsp; &nbsp; elseif (move == "left") then &nbsp; &nbsp; &nbsp; &nbsp; player.x = player.x - 5 \* dt &nbsp; &nbsp; end end onAccel = function ( event ) &nbsp; &nbsp; local x = event.xGravity &nbsp; &nbsp; if (x == 0) then -- notice here &nbsp; &nbsp; &nbsp; &nbsp; move = "still" &nbsp; &nbsp; elseif (x \> 0.1) then &nbsp; &nbsp; &nbsp; &nbsp; move = "right" &nbsp; &nbsp; elseif (x \< -0.1) then &nbsp; &nbsp; &nbsp; &nbsp; move = "left" &nbsp; &nbsp; end end enterFrame = function ( &nbsp;) &nbsp; &nbsp; local dt = getDT() -- A function to calculate delta time, not relevant here. &nbsp; &nbsp; movePlayer(dt) end Runtime:addEventListener( "enterFrame", enterFrame ) Runtime:addEventListener("accelerometer", onAccel)

It works as expected except for one thing, even though I created a condition for when the “xGravity = 0” (the device is exactly 90 degrees) the object still keeps moving in its original direction.

To be clearer, when I tilt the device to the right, the object moves to the right, but when I tilt it back, it keeps moving to the right until I tilt it more to the left and then the object starts moving to the left. It never stands still if it was already moving.

You should print out the xGravity values onscreen so that you can confirm the values that are being received by the device.

In my experience, it’s extremely difficult to get the device to register a valid pure zero reading, without the assistance of a carpenter level.

I agree with Alex. The device is so sensitive that it’s really hard to get a 0 value. You usually have to ignore small amounts of movement. You experience this frequently with the analog inputs on game controllers. They don’t like to settle back out at 0, and you’re always getting events from them.

Rob

Thanks, guys, but I managed to fix this problem by using a bool to check if the device is moving or not. It now works perfectly. 

You should print out the xGravity values onscreen so that you can confirm the values that are being received by the device.

In my experience, it’s extremely difficult to get the device to register a valid pure zero reading, without the assistance of a carpenter level.

I agree with Alex. The device is so sensitive that it’s really hard to get a 0 value. You usually have to ignore small amounts of movement. You experience this frequently with the analog inputs on game controllers. They don’t like to settle back out at 0, and you’re always getting events from them.

Rob

Thanks, guys, but I managed to fix this problem by using a bool to check if the device is moving or not. It now works perfectly.