Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
11 years, 2 months ago.
Conv RAd to Deg and moving pixel
hi im very new to micros and i am using an ADXL345 accelerometer which i am measuring the angle of tilt on my board but what i want to do is convert the output reading to deg instaed of radians and i want to have a small square in the centre of the nokia lcd which will move accordingly with the x and y output grateful for any help
1 Answer
11 years, 2 months ago.
For some info on converting radians to degrees: http://www.teacherschoice.com.au/maths_library/angles/angles.htm
I assume you want to make some kind of carpenters level like display on your LCD depending on the tilt/angle of your sensor? If you have your conversion working then you can set some maximum values for x and y angles and translate them to pixel positions. If you set horizontal level to 0 then you can for example tilt the sensor 90 degrees to the right and left (+90 to -90) and 90 degrees forward backward (also between +90 and -90) If your display has let's say 128 x 128 pixels then the center point would be about 63,63 you need to convert the derived degree values like this:
xposition=63+( angle_x / 1.4); yposition=63+( angle_y / 1.4);
The 1.4 comes from 1.4 = 2 * ( 90 / 128 ) see below
if angle_x and angle_y are zero (aka 'level') then xposition will be:
xposition=63+( 0 /1.4); // xposition will be 63 yposition=63+( 0 /1.4); // yposition will be 63
if angle_x and angle_y are at +90 degrees:
xposition=63+(90/1.4); // xposition will be 127 yposition=63+(90/1.4); // xposition will be 127
if angle_x and angle_y are at -90 degrees:
xposition=63+(-90/1.4); // xposition will be 0 yposition=63+(-90/1.4); // xposition will be 0
You can calculate the conversion factor based on the maximum angle and your display's resolution
xconversionfactor = 2 * (maximum_angle_x / display_x_resolution); yconversionfactor = 2 * (maximum_angle_y / display_y_resolution);
Of course calculating the x and y display position gives you a single dot position, if you want a larger square just subtract half the width of the square from the x position and half the height of the y position to calculate the upperleft corner and draw a square from those new coordinates. By playing with the conversion factor you can set your sensitivity. You might also need to limit the maximum and minimum coordinates so you do not draw 'outside' your display.
1 radian = 57.2957795 degrees. So if you multiply your radian value by 57.3 or so you'll have a value in degrees.
I'm not sure how you want to move the pixel.
posted by Stephen Paulger 05 Feb 2013