2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

leJOS EV3で光センサーの値によるイベント処理

Last updated at Posted at 2016-06-19

光センサーの値が0~0.4の場合にイベントが発生する
lejos 0.9.1-beta の環境で開発

LightDetectorSample.java
import lejos.hardware.Button;
import lejos.hardware.port.SensorPort;
import lejos.hardware.sensor.EV3ColorSensor;
import lejos.hardware.sensor.SensorMode;
import lejos.robotics.RangeFinder;
import lejos.robotics.RangeFinderAdapter;
import lejos.robotics.objectdetection.Feature;
import lejos.robotics.objectdetection.FeatureDetector;
import lejos.robotics.objectdetection.FeatureListener;
import lejos.robotics.objectdetection.RangeFeatureDetector;

public class LightDetectorSample {

	public static void main(String[] args) {
		EV3ColorSensor lightSensor = new EV3ColorSensor(SensorPort.S2);
		SensorMode light = lightSensor.getRedMode();  // 赤色LEDの反射光を読み取るセンサーモードを取得
		RangeFinder rangeFinder = new RangeFinderAdapter(light); // 光センサーの値を読み取りやすくする
		
		// 光センサーの値が0~0.4の時にイベントを発生させる、読み取り周期は20ms
		RangeFeatureDetector lowerDetector = new RangeFeatureDetector(rangeFinder, 0.4f, 20);
		lowerDetector.addListener(new LowerLightProcess());  // イベントが発生した時に呼び出されるインスタンスを設定
		
		Button.waitForAnyPress();  // 何かボタンが押されるまで待つ
	}

}

class LowerLightProcess implements FeatureListener {

	// イベントが発生した時に呼び出されるメソッド
	public void featureDetected(Feature feature, FeatureDetector detector) {
		System.out.println("DETECT " + feature.getRangeReading().getRange());
	}
	
}
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?