Payday Loan Payday Loan

Archive for the ‘Sensors’ Category

Hi,
I want to show you how you can get in real time x,y,z values of the Accelerometer sensor so that you can make any calculations for any reason you want.

This data’s will be displayed on the screen in textViews but you can use them however you want
First i will show you main.xml code:

 
<?xml version="1.0" encoding="UTF-8"?>
 
<LinearLayout android:layout_height="fill_parent" android:layout_width="fill_parent" 
android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> 
<TextView android:layout_height="wrap_content" android:layout_width="fill_parent"
 android:text="@string/hello"/>
 <TextView android:layout_height="wrap_content" android:layout_width="fill_parent"
 android:text="Accelerometer"/> 
 <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" 
android:text="X Value" android:id="@+id/xvalue"/> 
 <TextView android:layout_height="wrap_content" android:layout_width="fill_parent"
 android:text="Y Value" android:id="@+id/yvalue"/> 
 <TextView android:layout_height="wrap_content" android:layout_width="fill_parent"
 android:text="Z Value" android:id="@+id/zvalue"/> 
 <TextView android:layout_height="wrap_content" android:layout_width="fill_parent"
 android:text="Orientation"/> 
 <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" 
android:text="X Value" android:id="@+id/xvalues"/>
  <TextView android:layout_height="wrap_content" android:layout_width="fill_parent"
 android:text="Y Value" android:id="@+id/yvalues"/> 
  <TextView android:layout_height="wrap_content" android:layout_width="fill_parent"
 android:text="Z Value" android:id="@+id/zvalues"/>
  <TextView android:text="TextView" android:id="@+id/textView1" 
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> 
  </LinearLayout>

And this is the Main.java class:

package com.neuralnets;
 
 
import android.app.Activity;
import android.graphics.Color;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
 
 
public class Main extends Activity implements SensorListener {
 
	SensorManager sm = null;
 
	TextView xacc= null;
	TextView yacc = null;
	TextView zacc = null;
	TextView xorient = null;
	TextView yorient = null;
	TextView zorient = null;
	TextView text = null;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        setContentView(R.layout.main);
 
 
        xacc = (TextView) findViewById(R.id.xvalue);
        yacc = (TextView) findViewById(R.id.yvalue);
        zacc = (TextView) findViewById(R.id.zvalue);
        xorient = (TextView) findViewById(R.id.xvalues);
        yorient = (TextView) findViewById(R.id.yvalues);
        zorient = (TextView) findViewById(R.id.zvalues);
 
 
    }
 
 
 
    public void onSensorChanged(int sensor, float[] values) {
        synchronized (this) {
 
            if (sensor == SensorManager.SENSOR_ORIENTATION) {
	            xorient.setText("Orientation X: " + values[0]);
	            yorient.setText("Orientation Y: " + values[1]);
	            zorient.setText("Orientation Z: " + values[2]);
            }
            if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
	            xacc.setText("Accel X: " + values[0]);
	            yacc.setText("Accel Y: " + values[1]);
	            zacc.setText("Accel Z: " + values[2]);
            }            
        }
    }
 
    public void onAccuracyChanged(int sensor, int accuracy) {
    	Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);
 
    }
 
 
    @Override
    protected void onResume() {
        super.onResume();
        sm.registerListener(this, 
                SensorManager.SENSOR_ORIENTATION |
        		SensorManager.SENSOR_ACCELEROMETER,
                SensorManager.SENSOR_DELAY_NORMAL);
    }
 
    @Override
    protected void onStop() {
        sm.unregisterListener(this);
        super.onStop();
    }    
 
 
}

If you run this code you will understand very well how actually works accelerometer sensor.

||||| 3 I Like It! |||||

Hi,
Today i want to show you a Override function.
This function you will put it before the last “}”, where you implement the methods of the class.
This function will get any user interface touches and will make an action according to it.
So, here it is the code:

@Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) { //if we touched the screen of device
          View v = new View(this);
        	Intent x=new Intent(v.getContext(), Main.class); //In this case we go to a new activity...but you 
 
//can implement anything here.
            startActivityForResult(x,0);finish();
        }
        return true;
    }

So, here is how you can do an action when user touch the screen.

||||| 0 I Like It! |||||

How to detect shake motion on Android

I want to show you how you can detect a simple shake motion on Android, using accelerometer sensor.
You will need to use the following code:
You will use the Sensor Manager and sensor_accelerometer for getting some datas from them.
You will make a formula with these datas and that’s is…you’ve got an shake detector.

 
import android.app.Activity;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
 
public class ShakeActivity extends Activity implements SensorListener {
    // For shake motion detection.
    private SensorManager sensorMgr;
    private long lastUpdate = -1;
    private float x, y, z;
    private float last_x, last_y, last_z;
    private static final int SHAKE_THRESHOLD = 800;
//800 is the value for detecting the shake...if you raise the value than you 
	//will need to shake harder for detection if you lower the value you
	//will need to shake softer for detection.
 
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
 
	setContentView(R.layout.main);
	// start motion detection
	sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
	boolean accelSupported = sensorMgr.registerListener(this,
		SensorManager.SENSOR_ACCELEROMETER,
		SensorManager.SENSOR_DELAY_GAME);
 
	if (!accelSupported) {
	    // on accelerometer on this device
	    sensorMgr.unregisterListener(this,
                SensorManager.SENSOR_ACCELEROMETER);
	}
    }
 
    protected void onPause() {
	if (sensorMgr != null) {
	    sensorMgr.unregisterListener(this,
                SensorManager.SENSOR_ACCELEROMETER);
	    sensorMgr = null;
        }
	super.onPause();
    }
 
    public void onAccuracyChanged(int arg0, int arg1) {
	// TODO Auto-generated method stub
    }
 
    public void onSensorChanged(int sensor, float[] values) {
    	Log.d("sensor", "onSensorChanged: " + sensor);
	if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
	    long curTime = System.currentTimeMillis();
	    // only allow one update every 100ms.
	    if ((curTime - lastUpdate) > 100) {
		long diffTime = (curTime - lastUpdate);
		lastUpdate = curTime;
 
		x = values[SensorManager.DATA_X];
		y = values[SensorManager.DATA_Y];
		z = values[SensorManager.DATA_Z];
 
		float speed = Math.abs(x+y+z - last_x - last_y - last_z) / diffTime * 10000;
 
		// Log.d("sensor", "diff: " + diffTime + " - speed: " + speed);
		if (speed > SHAKE_THRESHOLD) {
			Log.d("sensor", "shake detected w/ speed: " + speed);
		    Toast.makeText(this, "shake detected w/ speed: " + speed, Toast.LENGTH_SHORT).show();
		}
		last_x = x;
		last_y = y;
		last_z = z;
	    }
	}
    }
}
||||| 0 I Like It! |||||

How to use pressure sensor on Android!

I want to tell you that some Android Devices(and not small amount) have a pressure sensor.
Now you will gonna ask me where is located this sensor.
Is very simple. This sensor is located on the display of the Device. For example my HTC Hero have this sensor, and i have used it in some creative games. You can overpass your imagination using the pressure sensor.
This sensor tells you how hard the user press the display.
To find this out is very easy. You just need the following code:

package com.pressure;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.TextView;
 
public class Main extends Activity {float x; TextView y;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
   y    =(TextView)findViewById(R.id.TextView01);
 
    }
 
	@Override
	public boolean onTouchEvent(MotionEvent event) {
	 x=event.getPressure();
	 y.setText(String.valueOf(x));
		return super.onTouchEvent(event);
	}
 
}

In my example i have displayed the value returned by sensor on a TextView from the layout.
You just touch the screen and the value must increase in a value between 0.00 and 1.00.
Now you can make some levels and be creative in creating your game:d.
For other neclarities please comment.

||||| 0 I Like It! |||||

Search

Popular

Sponsors