Oh my goodness - division by zero - how did I miss that!
OK - so I've just got to sort out my code to fit the c++ version... Thanks ever so much for your time everyone.
Moderators: roger, 99jonathan, imaqine
Oh my goodness - division by zero - how did I miss that!
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 an array WhichTouch with the states of the 4 switches
* (1-4) that have been pressed. Implementation of SensorConstants
* can be found at http://lejos.sourceforge.net/nxt/nxj/tutorial/AdvancedTopics/Advanced_Hardware.htm#13
* A touch sensor raw outputs 1023 when it is not pressed. Any value less than 600 is interpreted as pressed.
* timread v1.0 22/01/09
*/
public class MultiTouch implements SensorConstants {
ADSensorPort port; //all this is on the sourceforge lejos tutorial
public MultiTouch(ADSensorPort port){ //create switch sensorport with raw output
this.port = port;
port.setTypeAndMode(TYPE_SWITCH, MODE_RAW);
}
public static MultiTouch touch = new MultiTouch(SensorPort.S4); // create new MultiTouch sensor on port S4.
public static void main(String[] args) { //the main method
while (!Button.ENTER.isPressed()){ //making a loop once the orange button is pressed
boolean [] whichswitch = new boolean[4]; //create 4 element array called whichswitch which holds the state of the switches
WhichTouch(); //this calls the method that finds out which switch is pressed
}
}
public static boolean[] WhichTouch(){
int value = 1023 - (touch.port.readRawValue()); // this reads the multiplexer raw output
//the raw data goes from 0-1023
int switches = 339*value; //next 4 lines setup switches to be evaluated with bitwise operator
switches/= (1023 - value);
switches+= 5;
switches/= 10;
boolean [] whichswitch = {false,false,false,false}; //create 4 element array all initialised false
if((switches & 8)==8){ // this is a bitwise operator &
// - if the 8th bit of switches is a 1 then switch 4 is true (pressed)
whichswitch[3] = true;
}
if((switches & 4)==4){ //test for switch 3
whichswitch[2] = true;
}
if((switches & 2)==2){ //test for switch 2
whichswitch[1] = true;
}
if((switches & 1)==1){ //test for switch 1
whichswitch[0] = true;
}
return whichswitch; //return array of switch states
}
}
import lejos.nxt.ADSensorPort;
import lejos.nxt.SensorConstants;
public class TouchMuxHT {
private ADSensorPort port;
public TouchMuxHT(ADSensorPort port) {
this.port = port;
port.setTypeAndMode(SensorConstants.TYPE_SWITCH, SensorConstants.MODE_RAW);
}
public boolean[] readSensor() {
int val = 1023 - port.readRawValue();
int sw = (339 * val / (1023 - val) + 5) / 10;
return new boolean[]{(sw & 1) > 0, (sw & 2) > 0, (sw & 4) > 0, (sw & 8) > 0};
}
}Users browsing this forum: NadirHajiyev and 1 guest