Why does this not work?

Ok, so I have this code, which reads from a Nintendo DS touchpad plus some buttons, the basic idea is that in order to read data from the buttons, you have to be touching the touchpad, using the following code:

#include <MIDI.h>
/*
 *  THIS IS THE FINAL VERSION FOR FUNCTIONAL PROTOTYPE
 */
 
//These variables indicate which pins the touchpad is connected to
int xLow = A3;
int xHigh = A1;
int yLow = A0;
int yHigh = A2;

int irSensorPin = 4;

int buttonStateArray[9] = {LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW};
boolean previousButtonStateArray[9] = {false,false,false,false,false,false,false,false,false};
int buttonNoteValueArray[9] = {0,0,0,0,0,0,0,0,0};

int sensorArray[10];

int baseNoteValue = 60;

int prevNoteValue = 0;


void setup() {
  for(int i = 0; i <14;i++){
    pinMode(i,INPUT);
  }
  //Serial.begin(9600);
  MIDI.begin();
  MIDI.sendProgramChange(41,0);
}

/**
 *  This function loops indefinately until the instrument is stopped, this function
 *  contains all the code that produces the sound outputs.
 */
void loop() {
  if(touched()){
      for(int i = 0; i < 10; i++){
      int tempValue = int(((12343.85 * pow(analogRead(4),-1.15))+ 0.5));
      sensorArray[i] = tempValue;
      delay(5);
    }
    int sensorValue = quickSortMedian();
    int temp = constrain(sensorValue,12,57);
    int noteValue = map(temp,12,57,0,27);
  
    if(noteValue != prevNoteValue){
      //Turn off all the previous notes (prevent's 'note hanging')
      MIDI.sendControlChange(123,0,0);
      //Turn on all the new notes
      for(int i = 0; i < 10; i++){
        buttonStateArray[i] = digitalRead(i+2);  //Read the i+2 button's state (as digital pins 1&2 aren't used)
        if(buttonStateArray[i] == HIGH){
          int noteToPlay = baseNoteValue + noteValue + i;
          MIDI.sendNoteOn(noteToPlay,127,0);
          buttonNoteValueArray[i] = noteToPlay;
        }
       prevNoteValue = noteValue;
      }
    }
    else{
      //If note doesn't change, just re-do the buttons
      for(int i = 0; i < 10; i++){
        buttonStateArray[i] = digitalRead(i+2);  //Read the i+2 button's state (as digital pins 1&2 aren't used)
        //If button is pushed down, activate
        if(buttonStateArray[i] == HIGH){
          //However, only activate the note once for the entire time button is pushed down
          if(previousButtonStateArray[i] == false){
            int noteToPlay = baseNoteValue + noteValue + i;  //calculate the value of the note
            MIDI.sendNoteOn(noteToPlay,127,0);  //turn it on
            buttonNoteValueArray[i] = noteToPlay;  //store the value
            previousButtonStateArray[i] = true;  //button is held down, so set to true
          }
        }
        else{
          int noteToPlay = baseNoteValue + noteValue + i;  //calculate value of note
          MIDI.sendNoteOff(noteToPlay,127,0);  //turn it off
          buttonNoteValueArray[i] = noteToPlay; //store the value
          previousButtonStateArray[i] = false;  //button no longer held down, so set to false
        }
      }
    }
  }
  else{}
}

/**
 *  This function sorts the array of sensor values from the IR sensor, and then
 *  calculates the median of the array (provides a better representation than
 *  the average)
 */
int quickSortMedian(){
  for(int  j = 2 ; j < 10; j++){
    int key = sensorArray[j];
    int i = j-1;
    while(i > 0 && sensorArray[i] > key){
      sensorArray[i+1] = sensorArray[i];
      i = i - 1;
    }
    sensorArray[i + 1] = key;
  }
  
  //Get the median value
  int median = sensorArray[5] + sensorArray[6] / 2;
  return median;
}

/**
 *  This function reads the x-value from the touchpad
 */
int getTouchpadX(){
  pinMode(xLow,OUTPUT);
  pinMode(xHigh,OUTPUT);
  digitalWrite(xLow,LOW);
  digitalWrite(xHigh,HIGH);
 
  digitalWrite(yLow,LOW);
  digitalWrite(yHigh,LOW);
 
  pinMode(yLow,INPUT);
  pinMode(yHigh,INPUT);
  delay(10);
  
  int x = analogRead(yLow);
  return x;
}

/**
 *  This function reads the y-value from the touchpad
 */
int getTouchpadY(){
  pinMode(yLow,OUTPUT);
  pinMode(yHigh,OUTPUT);
  digitalWrite(yLow,LOW);
  digitalWrite(yHigh,HIGH);
 
  digitalWrite(xLow,LOW);
  digitalWrite(xHigh,LOW);
 
  pinMode(xLow,INPUT);
  pinMode(xHigh,INPUT);
  delay(10);
  
  int y = analogRead(xLow);
  return y;
}

/**
 *  This method simply checks if there is something touching the touchpad
 */
boolean touched(){
  if(getTouchpadX() > 0){    
    return true;
  }
  else{   
    return false;
  }
}

Which should in theory work, as it says:
‘If there is something touching the touchpad, then go, else do nothing’

However, it seems to completely ignore the whole ‘if touched()’ conditional and allows me to read data from buttons regardless of whether I am touching the touchpad or not.

From other tests I have performed I can confirm the touchpad is working correctly.

"touched()" will only return false if

int x = analogRead(yLow);

returns zero.

Is that correct?

That is correct, the reason for the yLow is the way the sensor seems to work The touchscreen reading code was copied from this example:

// Taken from http://kousaku-kousaku.blogspot.com/2008/08/arduino_24.html
/*
#define xLow  A0
#define xHigh A2
#define yLow  A3
#define yHigh A1
*/

//modified to match my sparkfun connector
#define xLow  A0
#define xHigh A2
#define yLow  A3
#define yHigh A1


void setup(){
  Serial.begin(9600);
}

void loop(){
  pinMode(xLow,OUTPUT);
  pinMode(xHigh,OUTPUT);
  digitalWrite(xLow,LOW);
  digitalWrite(xHigh,HIGH);

  digitalWrite(yLow,LOW);
  digitalWrite(yHigh,LOW);

  pinMode(yLow,INPUT);
  pinMode(yHigh,INPUT);
  delay(10);

  //xLow has analog port -14 !!
  int x=analogRead(yLow);

  pinMode(yLow,OUTPUT);
  pinMode(yHigh,OUTPUT);
  digitalWrite(yLow,LOW);
  digitalWrite(yHigh,HIGH);

  digitalWrite(xLow,LOW);
  digitalWrite(xHigh,LOW);

  pinMode(xLow,INPUT);
  pinMode(xHigh,INPUT);
  delay(10);

  //xLow has analog port -14 !!
  int y=analogRead(xLow);

    Serial.print(x,DEC);   
    Serial.print(",");     
    Serial.println(y,DEC); 

  delay(200);
}

The DS touchpad works thusly: http://www.sparkfun.com/datasheets/LCD/HOW%20DOES%20IT%20WORK.pdf

Have tried swapping xLow for yLow, and it doesn't change anything.

I have also using analogRead(3)/analogRead(0) instead of xLow/yLow, doesn't seem to change anything.

Printing out the value read by analogRead might provide some clues.

Have done that using the touchpad code from the second post, if nothing is touching it, I get (0,0) in serial monitor.

However, doing the same thing in my code (i’ve changed it this for simplicity:

#include <MIDI.h>
/*
 *  THIS IS THE FINAL VERSION FOR FUNCTIONAL PROTOTYPE
 */
 
//These variables indicate which pins the touchpad is connected to
int xLow = A3;
int xHigh = A1;
int yLow = A0;
int yHigh = A2;

int irSensorPin = 4;

int buttonStateArray[9] = {LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW};
boolean previousButtonStateArray[9] = {false,false,false,false,false,false,false,false,false};
int buttonNoteValueArray[9] = {0,0,0,0,0,0,0,0,0};

int sensorArray[10];

int baseNoteValue = 60;

int prevNoteValue = 0;


void setup() {
  for(int i = 0; i <14;i++){
    pinMode(i,INPUT);
  }
  Serial.begin(9600);
  //MIDI.begin();
  //MIDI.sendProgramChange(41,0);
}

/**
 *  This function loops indefinately until the instrument is stopped, this function
 *  contains all the code that produces the sound outputs.
 */
void loop() {
  boolean me = touched();
  if(me){
   Serial.println("w00r");   
  }
  else{}
}

/**
 *  This function sorts the array of sensor values from the IR sensor, and then
 *  calculates the median of the array (provides a better representation than
 *  the average)
 */
int quickSortMedian(){
  for(int  j = 2 ; j < 10; j++){
    int key = sensorArray[j];
    int i = j-1;
    while(i > 0 && sensorArray[i] > key){
      sensorArray[i+1] = sensorArray[i];
      i = i - 1;
    }
    sensorArray[i + 1] = key;
  }
  
  //Get the median value
  int median = sensorArray[5] + sensorArray[6] / 2;
  return median;
}

/**
 *  This function reads the x-value from the touchpad
 */
int getTouchpadX(){
  pinMode(xLow,OUTPUT);
  pinMode(xHigh,OUTPUT);
  digitalWrite(xLow,LOW);
  digitalWrite(xHigh,HIGH);
 
  digitalWrite(yLow,LOW);
  digitalWrite(yHigh,LOW);
 
  pinMode(yLow,INPUT);
  pinMode(yHigh,INPUT);
  delay(10);
  
  int x = analogRead(yLow);
  return x;
}

/**
 *  This function reads the y-value from the touchpad
 */
int getTouchpadY(){
  pinMode(yLow,OUTPUT);
  pinMode(yHigh,OUTPUT);
  digitalWrite(yLow,LOW);
  digitalWrite(yHigh,HIGH);
 
  digitalWrite(xLow,LOW);
  digitalWrite(xHigh,LOW);
 
  pinMode(xLow,INPUT);
  pinMode(xHigh,INPUT);
  delay(10);
  
  int y = analogRead(xLow);
  return y;
}

/**
 *  This method simply checks if there is something touching the touchpad
 */
boolean touched(){
  int sample = getTouchpadX();
  if(sample > 0){
    Serial.println(sample,DEC);    
    return true;
  }
  else{
    return false;
  }
}

Which simply prints the value if it returns true in touched(), and prints the word ‘w00r’ if a call to touched() returns true. This gives:

15
w00r
3
w00r
3
w00r
1
w00r
3
w00r
29
w00r
1
w00r
7
w00r
19
w00r
2
w00r
1
w00r
3
w00r
23
w00r
1
w00r
3
w00r
15
w00r
1
w00r

Now then, using the touchscreen code I get:

1,0
0,0
0,3
0,0
5,0
0,0
0,0
0,0
1,0
0,0
0,0
0,0
0,0
0,0
0,0
0,0
0,0
0,0
0,0
0,0
0,0
0,0
0,0
0,0
1,0
0,0
0,0
0,0
0,0
0,19

So in this case, what seems to be happening is that there is minor interference (despite the other software note containing much. Think I need some calibration in there. Will give it a shot and report my results, [smiley=dankk2.gif] for the help, will keep posting the results!

If the minimum of value, when the screen IS being touched is significant (i.e. above 27), then return true when the value read is greater than the significant minimum value, and false when it isn't. A discrete value like 0 is not a good idea.