android
  #1  
Old 01-21-2011, 12:55 PM
Saponificio Saponificio is offline
Junior Member
 
Join Date: Jan 2011
Posts: 2
Default Accelerometer app

Hi! I'm trying to understand how accelerometer works so I made an easy app to display accel. x y z and orientation but it doesn't work... It's my first app so I'm not sure if everything is correct. Help!

Accelerometer test:

Code:
height=screen.height()
width=screen.width()

black=color.new(0,0,0)

strx="acc. x value: "
stry="acc. y value: "
strz="acc. z value: "
stro="acc. orientation: "
err="ERROR OPENING ACC."
all="all"

accok=accelerometer.open()
os.sleep(10)

if accok==0 then
	accelerometer.set_datatype(all)
	while 1 do
		screen.fillrect(0,0,width,height,black)
		x,y,z,o=accelerometer.getdata(all)
		text.draw(5,20,strx..x,"center",width)
		text.draw(5,60,stry..y,"center",width)
		text.draw(5,100,strz..z,"center",width)
		text.draw(5,140,stro..o,"center",width)
		screen.update()
		if control.read()==1 then
			if control.IsButton()==1 then
				if button.home()==1 then
					accelerometer.close()
					break
				end
			end
		end
	end
else
	screen.fillrect(0,0,width,height,black)
	text.draw(5,20,"center",width)
	screen.update()
end
Reply With Quote

Advertisement [Remove Advertisement]

  #2  
Old 01-21-2011, 01:02 PM
jbob182's Avatar
jbob182 jbob182 is offline
X-Fi2 Development Moderator
 
Join Date: Feb 2010
Location: The U.S.A.
Posts: 554
Default

well one thing is that your while loop is in an if statement. THe bigger problem is their is no os.sleep(1)

also you need

Code:
if control.read()==1 then
          if control.isSensor()==1 then
                x_dir,y_dir,z_dir = accelerometer.getdata("xyz")
          end
end
in your while loop to read from the accel.
ie: this is a simple while loop

Code:
while true do
    if control.read()==1 then
        if control.isSensor()==1 then
            x_dir,y_dir,z_dir = accelerometer.getdata("xyz")
        end
    else
        os.sleep(1)
    end
    if quit then
        accelerometer.close()
        break
    end
end
also when i initialize the accel. i use
Quote:
accelerometer.open();os.sleep(10)
if (accelerometer.set_datatype) then
accelerometer.set_datatype("all")
else
accelerometer.set_senddatatype("all")
end

Last edited by jbob182; 01-21-2011 at 01:17 PM.
Reply With Quote

  #3  
Old 01-21-2011, 01:32 PM
Saponificio Saponificio is offline
Junior Member
 
Join Date: Jan 2011
Posts: 2
Default

Thanks a lot for your quick reply. Can't while be in an if statement? What's os.sleep(1) for?
I'll modify it
Reply With Quote

  #4  
Old 01-21-2011, 02:34 PM
Brett_val's Avatar
Brett_val Brett_val is offline
Senior Member
 
Join Date: Jan 2010
Location: The Netherlands
Posts: 636
Default

accelerometer.open() should return 0 so I see no problem with the if statement.

I do believe that (all) should be between quotes: accelerometer.set_datatype("all") and accelerometer.getdata("all").

As for setting the datatypes, not sure if those are needed but I use it anyway to be sure.

Tetrajak left us with an app to do a similar task, check it out to get some ideas on how to work with the accelerometer:
http://www.anythingbutipod.com/forum...ad.php?t=52712
__________________
this is a link to my personal homepage which I haven't
Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 12:37 AM.