android
  #1  
Old 05-12-2010, 12:20 PM
Tiwiz Tiwiz is offline
Junior Member
 
Join Date: May 2010
Posts: 5
Default Can't manage the accelerometer

Hello everyone I was trying to create an app for my x-fi2, but I can't make it working properly, it just doesn't turn... here I post the code, maybe you know where I'm wrong

Code:
h = screen.height()
w = screen.width()
black = color.new(0,0,0)
red = color.new(255,0,0)
screen.fillrect(0,0,w,h,black)
txtsize = text.size(30)
txtcolor = text.color(red)
string = "Hello, World!"
text.draw(0,80,string,"center",w)
screen.update()
text.size(txtsize)
text.color(txtcolor)
screen.update()

--Accendo l'accelerometro
accInit = accelerometer.open();
print(accInit); --Stampo il valore, se  0 l'accelerometro  acceso
oldDir = accelerometer.getdata("orientation");

while 1 do
	--controllo la posizione dell'accelerometro
	dir = accelerometer.getdata("orientation");
	if dir ~= oldDir then
		if dir == 1 then
			screen.orientation(0);
		elseif dir == 2 then
			screen.orientation(1);
		elseif dir == 3 then
			screen.orientation(0);
		elseif dir == 4 then
			screen.orientation(1);
		end
		
		oldDir = dir;
		green = color.new(124,252,0)
		txtsize = text.size(30)
		txtcolor = text.color(green)
		text.draw(0,80,string,"center",w)
		screen.update()
		text.size(txtsize)
		text.color(txtcolor)
		screen.update()
	end
	if control.read()==1 then
		if control.isButton()==1 then
			if button.click()==1 then
				if accInit==0 then
					accelerometer.close();
				end
				break
			end
		elseif control.isTouch()==1 then
			if accInit==0 then
				accelerometer.close();
			end
			break
		end
	else
		os.sleep(10)
	end
	
end
it's almost all copied from the tutorial, but it doesn't turn...either it doesn't become green...where I'm doing it wrong?
Reply With Quote

Advertisement [Remove Advertisement]

  #2  
Old 05-12-2010, 01:25 PM
Brett_val's Avatar
Brett_val Brett_val is offline
Senior Member
 
Join Date: Jan 2010
Location: The Netherlands
Posts: 636
Default

You forgot to set the datatype for the accelerometer output after you've opened it:
Code:
accInit = accelerometer.open();
os.sleep(10);
accelerometer.set_datatype("orientation")
It's still not working as it should I think, but at least the text turns green and the screen changes orientation in some way... I believe the accelerometer is known to output wrong orientation numbers.. maybe you should just use the x,y,z for orientation.

By the way have you seen Tetrajak's accelerometer output app? Helped me a lot to figure out how the accelometer functions.
Reply With Quote

  #3  
Old 05-12-2010, 01:31 PM
Tiwiz Tiwiz is offline
Junior Member
 
Join Date: May 2010
Posts: 5
Default

oh, thank you very much, I didn't find anything! I'll look at it and correct mine, thank you!
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 04:35 AM.