stuck trying to implement Hitechnic Touch Sensor Multiplexer

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

Moderators: roger, 99jonathan, imaqine

Postby timread » Thu Jan 22, 2009 8:49 am

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.
timread
New User
 
Posts: 11
Joined: Thu Dec 25, 2008 11:48 am
Location: London

Postby gloomyandy » Thu Jan 22, 2009 11:50 am

Oh my goodness - division by zero - how did I miss that!


Well it doesn't help that Java calls it an arithmeticException!

All the best

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3004
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby lawrie » Thu Jan 22, 2009 12:28 pm

When you have your code working reliably, if you separate the MultiTouch class from the test program that uses it, we could add MultiTouch to the next release of leJOS.
lawrie
leJOS Team Member
 
Posts: 677
Joined: Mon Feb 05, 2007 1:27 pm

Postby timread » Thu Jan 22, 2009 8:07 pm

hi Lawrie,
Works good.
The output is now a boolean array of the state of the switches 1-4:
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 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
      }   
   }
timread
New User
 
Posts: 11
Joined: Thu Dec 25, 2008 11:48 am
Location: London

Re: stuck trying to implement Hitechnic Touch Sensor Multipl

Postby mescalinum » Fri Sep 14, 2012 10:14 pm

I like having it in a class which I put in my class library:

Code: Select all
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};
   }
}
mescalinum
New User
 
Posts: 14
Joined: Sat Jul 30, 2011 12:38 pm

Previous

Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 0 guests

more stuff