All I get on the output is switch0 and raw0, even though I'm pressing the switches which are all connected to the multiplexer, which in turn is plugged in to port 4. I'm fairly new to Java, and I'm getting tied up in knots trying to find out whats wrong!
- Code: Select all
import lejos.nxt.*;
/* This programme interprets
* the output from the Hitechnic Touchsensor Multiplexer.
* The Multiplexer here is connected to Sensorport 4.
* The output returned is the number of the switch
* (1-4) that has been pressed), or 0 for no press. Implementation of SensorConstants
* can be found at http://lejos.sourceforge.net/nxt/nxj/tutorial/AdvancedTopics/Advanced_Hardware.htm#13
* A touch sensor outputs 1023 when it is not pressed. Any value less than 600 is interpreted as pressed.
*/
public class MultiTouch implements SensorConstants { //MultiTouch is the constructor
//for the multiplexer touch switch
ADSensorPort port; //all this is on the sourceforge lejos tutorial
public MultiTouch(ADSensorPort port){
this.port = port;
port.setTypeAndMode(TYPE_SWITCH, MODE_RAW);
}
// create new MultiTouch sensor.
public static MultiTouch touch = new MultiTouch(SensorPort.S4); //touch is the multiplexer
public static void main(String[] args) { //the main method
while (!Button.ENTER.isPressed()){ //making a loop once the orange button is pressed
int whichswitch;
whichswitch = WhichTouch(); //this calls the method that finds out which switch is pressed
System.out.println("switch" + whichswitch); //checking the value of whichswitch
}
}
public static int WhichTouch(){
int value = 1023 - (touch.port.readRawValue()); // this should read the multiplexer
//the raw data goes from 0-1023
System.out.println("raw" + value); //checking the value of value
int switches = 339*value; //the next few lines are on the sheet that comes
//with the multiplexer (but in c++ - I've attempted to translate it here)
switches = switches / (1023 - value);
switches = switches + 5;
switches = switches /10;
if((switches & 8)==8){ // this is a bitwise operator &
// - if the 8th bit of switches is a 1 then switch 4 is pressed
return 4; //switch 4 pressed
}
if((switches & 4)==4){
return 3;
}
if((switches & 2)==2){
return 2;
}
if((switches & 1)==1){
return 1;
}
else return 0; //this for no press
}
}

