Help with orientation sensor

Hello I am currently trying to understand orientation sensors and i have developed a “game” which has an arrow. What I am trying to achieve is to have the arrow move and point towards the direction i am tilting the phone (for example if i tilt the phone left the arrow will point left as it moves towards the left).

**I know ANGLE would be the easiest way to do this, but im just trying to make it move either left or right and up or down (i dont want it to move diagonally or all over the place)

Ive attempted programming this a number of ways but its just not doing what i need it to do. The issue im having currently is that the sprite (arrow) only moves up or down. When the arrow goes up it points left (instead of up) and when the arrow moves down it points right (instead of down). I would very appreciate some help or tips on how to fix this issue. Thank you so much for the help in advance.

Here is the code I have:

Arrow

Here are some possibilities:

  • at the moment your global Direction is not initialized properly… this should probably be a blue Math 0; not OrientationEnabled = true. You do not show how you use Direction.

  • You probably already read and done this tutorial showing how to use the Canvas … Chapter 17. Creating Animated Apps ; it contains this
    image that shows how the Heading works. Knowing that 0 degrees not at the top of your device is important.

heading

  • how to program Chapter 18. Programming Your App to Make Decisions shows how to ‘trap’ multiple conditions like
    Pitch and Roll (both properties described in OrientationSensor )

  • this is supposed to work on your device, you have to debug it by experimentation. What you do depends on your arrow image and the direction it is pointed as DESIGNED. See the image above. How you present your arrow image might be part of the issues. Another part depends on how you structure your conditional statements. At present you are not using the Logic and block and any code specifying ranges of pitch and roll. That might be what you need to do.

Do some experimenting by deleting most of you code and adding one action at a time. You might need to use the azimuth property ( you want your arrow head on your arrow sprite to point in the direction of movement too?)

Hope something here helps.

Regards,
Steve

Hi,
A while ago I have experimented with this quite a lot, because I wanted to control a robot car (a MaQueen with a micro:bit, from DFRobot) by tilting the phone. I used an Accelerometer, that works much better than an OrientationSensor. I do not know why.
Also, I used a Clock.Timer event instead of the Accelerometer.Changed event, because the Changed event of the Accelerometer changes much too fast.
Here is a picture of my screen:


I also upload the .aia here: maqueen8.aia (349.0 KB)
Do not bother about not having a robot car. You can run the .aia without it, just press the start button.
You will see all kinds of debug values appear on the screen, this could help you to make the right calculation.
You see also a blue ball, I wanted to have it run along the border of the compass, but I did not quite get there so far. Maybe I will pick this up some day again.

I read the chapters, looked, and tried different ways and i just cant get it. I was however able to make the sprite move up, left, right, but now it does not want to move down and i keep changing the code but it just doesnt want to work so far this is the closest ive gotten maybe someone sees the issue? (everything is correct until i try to move it down. when i tilt down the sprite moves to the right instead of down) Again, i really appreciate the help and thank you in advance.

***UPDATE this piece was working like i stated but now it is not? not sure why it worked for that brief moment. Im extremely new in this and extremely confused but i figured id share that in case some disagree with what i previously stated and the code i posted along with that.

new

Here is a Project that is a demonstration of the OrientationSensor working in an Android 4.2.2 tablet that provides simultaneous pitch and roll information. The example may help you design your app Choppa. It highlights buttons depending on the attitude of the device (it shows the pitch and roll values)


OrientationSensorExplore.aia (13.1 KB)
The app is not optimized. It does show how to use the Logic and to limit response to a range of pitch values and a range of roll values .

I can’t debug your app. I expect you need to use the Logic and block to make your sprite do what you want comparing the Roll and Pitch.

I hope this example is helpful.
Regards,
Steve

Hi,
I am trying this again. If your goal is to determine left, right, up and down on your phone, then I would suggest to use an AcceleroMeter instead of an OrientationSensor. Believe me, I tried and struggled with the OrientationSensor, but it is awkward. But I will take a look at your app (Did you upload an .aia???).

I remodeled my app a bit. It just has a compass now and a sprite on it. All the robot code is gone.

It is also best to use a clock instead of a changed event, because the change events go much to fast.
Here is the Clock block:
blocks (53)
The values used to choose where to go are determined by trial and error, more or less. Just by displaying debug values you get more information on how things work.
Try it. But if Steve’s example works for you, take that one!!!
compass_1.aia (345.5 KB)
Cheers, Ghica.

Thank you all. Ghica I truly appreciate the info, but my goal was to use the orientation sensor only because i’m practicing with one thing at a time, just to get a feel for it. I will definitely take your advice on the AcceleroMeter at a later time when start trying it out. Steve idea actually help me achieve what i intended, but again i really appreciate your input and help. I was able to successfully create the simple app i wanted just using the orientation sensor.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.