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

stuck trying to implement Hitechnic Touch Sensor Multiplexer

Postby timread » Sun Jan 18, 2009 5:47 pm

Hi there - can anyone tell me what I'm doing wrong? -
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
      }   
   }
timread
New User
 
Posts: 11
Joined: Thu Dec 25, 2008 11:48 am
Location: London

Postby gloomyandy » Sun Jan 18, 2009 6:44 pm

Hi,
Is it possible to see the original c++ code? It may make it easier to figure out what is going wrong...

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

Postby lawrie » Mon Jan 19, 2009 9:46 pm

I don't have the TouchSensor multiplexer, but I tried you program with a normal touch sensor on port 4. It gave me a value of 841, which is said was switch 4. Have you tried it with a standard touch sensor? Since the multiplexer should be the same as a touch sensor apart from giving a different raw value that indicates which swiches are pressed, it looks like a hardware problem rather than a problem with your program. Have you tried the multiplexer with NXT-G on any other language?
lawrie
leJOS Team Member
 
Posts: 677
Joined: Mon Feb 05, 2007 1:27 pm

Postby timread » Tue Jan 20, 2009 3:35 pm

gloomyandy wrote:Hi,
Is it possible to see the original c++ code? It may make it easier to figure out what is going wrong...

Andy


Yes, its
Code: Select all
value=1023-SensorRaw(INPUT_PORT);
switches=339*value;
switches/=1023-value;
switches+=5;
switches/=10;
if(switches&8) switch4=1; else switch4=0;
if(switches&4) switch3=1; else switch3=0;
if(switches&2) switch2=1; else switch2=0;
if(switches&1) switch1=1; else switch1=0;
timread
New User
 
Posts: 11
Joined: Thu Dec 25, 2008 11:48 am
Location: London

Postby timread » Tue Jan 20, 2009 3:45 pm

lawrie wrote:I don't have the TouchSensor multiplexer, but I tried you program with a normal touch sensor on port 4. It gave me a value of 841, which is said was switch 4. Have you tried it with a standard touch sensor? Since the multiplexer should be the same as a touch sensor apart from giving a different raw value that indicates which swiches are pressed, it looks like a hardware problem rather than a problem with your program. Have you tried the multiplexer with NXT-G on any other language?

Hi,
I tried it with a normal sensor and got 840 and switch4 like you.
I'll have a go with reinstalling the NXT-G firmware this evening and using the block that Hitechnic made. (my wife thinks im crazy playing with lego - any solution to that?!! )
timread
New User
 
Posts: 11
Joined: Thu Dec 25, 2008 11:48 am
Location: London

Postby timread » Tue Jan 20, 2009 8:50 pm

timread wrote:
lawrie wrote:I don't have the TouchSensor multiplexer, but I tried you program with a normal touch sensor on port 4. It gave me a value of 841, which is said was switch 4. Have you tried it with a standard touch sensor? Since the multiplexer should be the same as a touch sensor apart from giving a different raw value that indicates which swiches are pressed, it looks like a hardware problem rather than a problem with your program. Have you tried the multiplexer with NXT-G on any other language?

Hi,
I tried it with a normal sensor and got 840 and switch4 like you.
I'll have a go with reinstalling the NXT-G firmware this evening and using the block that Hitechnic made. (my wife thinks im crazy playing with lego - any solution to that?!! )

Hi again - yes, I reinstalled the NXT-G firmware and used the NXT-G block from Hitechnic and - it all works. SO my code must be wrong somewhere....?
timread
New User
 
Posts: 11
Joined: Thu Dec 25, 2008 11:48 am
Location: London

Postby timread » Tue Jan 20, 2009 8:58 pm

I just thought - do I need some sort of port listener - a trigger that once triggered by the change in state of the port stay triggered, which gives the code time to do something. If so - how do I do that?
timread
New User
 
Posts: 11
Joined: Thu Dec 25, 2008 11:48 am
Location: London

Postby gloomyandy » Tue Jan 20, 2009 10:49 pm

Hi,
You should not need a port listener or anything as complex as that... Time to try and debug this. Can you add some code to your program to simply display the raw readings from the sensor and then press each touch sensor in turn and report the results...

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

Postby lawrie » Wed Jan 21, 2009 12:07 am

(my wife thinks im crazy playing with lego - any solution to that?!! )

I think you will either have to grow up and do something serious or get a divorce.
lawrie
leJOS Team Member
 
Posts: 677
Joined: Mon Feb 05, 2007 1:27 pm

Postby lawrie » Wed Jan 21, 2009 12:48 am

Have you tried it on one of the other ports?
lawrie
leJOS Team Member
 
Posts: 677
Joined: Mon Feb 05, 2007 1:27 pm

Postby Shawn » Wed Jan 21, 2009 8:42 am

timread wrote: (my wife thinks im crazy playing with lego - any solution to that?!! )


Please reminder her that:

1) it is better to start too early than too late
2) you want to be around and healthy in your old age
3) memory loss is a very sad, very real, very difficulty and unfortunately all too common to aging
4) your regular playing with legos may help both you and her in the future

Then ask her to google for "memory loss exercises" to confirm what you are saying is true.

An example is:
http://www.articlesphere.com/Article/Aging-Memory-Loss---How-to-use-Brain-Aerobics-to-Build-a-Better-Memory/155862 wrote:We hear and think a lot about physical fitness, but frequently overlook mental exercise. For your optimal brain power, it’s important to exercise your mental muscles as well as your physical ones. A healthy mind and body relationship is the key to unlocking, activating, and sustaining brainpower during your entire lifespan.

Brain aerobics are vital to building and growing your mental muscles. But what exactly constitutes a brain aerobic exercise? To qualify as a brain aerobic exercise, it:

-- Needs to engage your attention
-- Must involve two or more of your senses
-- Must break a routine activity in an unexpected, nontrivial way

Here are some actual examples of simple brain aerobics you can use:

-- Volunteering
-- Singing songs
-- Memorizing lists
-- Learning a new language
-- Becoming computer literate
-- Discussing headlines and current events
-- Engaging in music, art, and other hobbies
-- Doing jigsaw puzzles and crossword puzzles

According to Dr. Dharma and the ARPF, everyone should spend at least 20 minutes — three times a week — doing mental exercises. Studies show that when people engage in moderate, pleasant forms of mental exercise, their knowledge, as well as the efficiency and the power of their brains, increases.


I can't really say for certain if this hobby will ultimately help you but ... best luck, I personally feel it's worth it.

Shawn
User avatar
Shawn
Advanced Member
 
Posts: 723
Joined: Wed Sep 12, 2007 4:59 am
Location: Tokyo

Postby timread » Wed Jan 21, 2009 8:56 pm

Well, I think I solved it...
I put in a new line to read the raw output just after the beggining of the method WhichTouch:
Code: Select all
int value = touch.port.readRawValue(); //new line to read raw output

and although it compiled, when I ran it I got the error message:
java exception:
class: 10
method: 17
pc: 2264

so I commented out the new line, and ran the old one:
Code: Select all
int value = 1023 - (touch.port.readRawValue());

and this time the code ran ok, but the readouts were as before raw0, switch0

So I changed the port to port 2, and ran the code again.
This time it worked!
I got

raw29 sw1
raw56 sw2
raw109 sw3
raw197 sw4
interestingly when i pressed 1+2 I got raw82 sw2
and when I pressed 3+4 I got raw270 sw4

so I went back to port 4, and it stopped working.
After pulling the plugs in and out a bit, port 4 started working, so I can only think that it must have been the leads, or the connectors weren't in properly.

The only question i have now is why does
Code: Select all
int value = touch.port.readRawValue();

create an exception
whereas
Code: Select all
int value = 1023 - (touch.port.readRawValue());

doesn't?
timread
New User
 
Posts: 11
Joined: Thu Dec 25, 2008 11:48 am
Location: London

Postby gloomyandy » Wed Jan 21, 2009 9:17 pm

Hi,
Glad you have got it working... If you post all of the code for the method that throws the exception we may be able to work out what is going on...

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

Postby timread » Wed Jan 21, 2009 10:20 pm

Hi Andy
The code is all at the top in my first post. The method is WhichTouch.
timread
New User
 
Posts: 11
Joined: Thu Dec 25, 2008 11:48 am
Location: London

Postby gloomyandy » Wed Jan 21, 2009 10:58 pm

Hi,
Well assuming that you replaced the line...
Code: Select all
int value = 1023 - (touch.port.readRawValue());

with
Code: Select all
int value = touch.port.readRawValue();


then if the port returns a value of 1023, then this statement....
Code: Select all
    switches = switches / (1023 - value);


Will result in a division by zero which will result in an arithmeticException which is what exception class 10 is....

Andy

PS Also your code is not the same as that in the C++ version, that code allows for multiple switches to be pressed at the same time and will return these switches as pressed, your code will only return the highest numbered switch pressed...
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3004
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Next

Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 3 guests

more stuff