how to read the TouchSensor value

This is where you talk about the EV3 software itself, installation issues, and programming talk.

Moderators: roger, gloomyandy, skoehler

r3d0xX
New User
Posts: 9
Joined: Sun Dec 29, 2013 3:23 pm

how to read the TouchSensor value

Postby r3d0xX » Mon Dec 30, 2013 7:27 pm

hiho !
i searched the forum but i found nothing about reading the value of the ev3 touch sensor or any other sensor.

in some threads they are talking about a lejos.nxt.Sensor package wich contains classes with .isPressed():bool methods.
but in the ev3classes there is no such package or in the EV3Sensors classes no such methods witch an boolean as return value.

i'd like to create somethis like this:

Code: Select all

while(true){
  if(new Sensor(SensorPort.S1).isPressed()){
    LCD.drawString("Im pressed!", 0, 0);
  }
}


how is this possible or is it possible at all :?:
thx for help :)

Aswin
leJOS Team Member
Posts: 227
Joined: Tue Apr 26, 2011 9:18 pm
Location: Netherlands
Contact:

Re: how to read the TouchSensor value

Postby Aswin » Tue Dec 31, 2013 1:05 am

Hi,

Assuming you are running alpha 0.5. The touch sensor is lejos.hardware.sensor.EV3TouchSensor.
It does not have the isPressed or isDown methods. It does implement the SampleProvider interface. This means you can query the sensor using the fetchSample method.

Code: Select all

sampleProvider touch = new EV3TouchSensor(port);
float[] sample = new float[touch.sampleSize()];
while(true) {
  touch.fetchSample(sample, 0);
  if (sample(0) == 1) {
  // do something
  }
}


This might seem a bit complicated. True, it is more complicated than it was before. However, all sensors now implement the SampleProvider interface. This means that you can easily make this code work with a different sensor, like a sound sensor for example.

Aswin
My NXT blog: http://nxttime.wordpress.com/

r3d0xX
New User
Posts: 9
Joined: Sun Dec 29, 2013 3:23 pm

Re: how to read the TouchSensor value

Postby r3d0xX » Tue Dec 31, 2013 12:52 pm

it works, great, thx ! :)

EDIT:
I recogized its only possible with analog-sensors but in this way not with "UART Sensors" such as the EV3UntrasonicSensor..
ideas? thx

r3d0xX
New User
Posts: 9
Joined: Sun Dec 29, 2013 3:23 pm

Re: how to read the TouchSensor value

Postby r3d0xX » Wed Jan 01, 2014 5:14 pm

i was dumb sry solved it.


Return to “EV3 Software”

Who is online

Users browsing this forum: No registered users and 1 guest