constant midi message

Hello,
I have a problem, the code below lets a 4-wire resistive touch screen send midi cc. The problem is it constantly sends a message out when it isn't being touched.
Anyone have a fix to get it to idle while not in use?

Thanks!

// DS TouchScreen
/*
#define xLow  14
#define xHigh 15
#define yLow  16
#define yHigh 17
*/

#define xLow  17
#define xHigh 15
#define yLow  16
#define yHigh 14
#define midiChannel 1

 int touchX, touchY;
 
void setup(){
 //  Setup serial / MIDI
    Serial.begin(31250);       // MIDI speed
 //  Serial.begin(9600);    // Debug speed
}
 
void loop(){
 touchScan();
 touchOut(); 
   delay(200);
}

void touchOut(){
   sendCC(20, char(touchX));  // send CC message 10
   sendCC(21, char(touchY));  // send CC message 11
}

//  sends a CC message
void sendCC(char control, char data) {
  int cmd;
  cmd = 0xB0 | midiChannel;  // merge channel number
  Serial.print(cmd, BYTE);
  Serial.print((control & 0x7F), BYTE);
  Serial.print((data & 0x7F), BYTE);  // use the 0x7F to restrict the data to a maximum of 127
}

void touchScan(){
   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 !!
  touchX=analogRead(yLow -14);
 
  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 !!
  touchY=analogRead(xLow - 14); 
}

What are the values of touchX and touchY when the screen is not being touched?

i forget what they were when the screen was directly connected to the analog in 1-4.
i placed 10k resistors between the screen and the inputs to drop the x and y values to 0.

so currently it just fires out 0, 0 a few times a second when it is not in use.

With the resistors in place, do you get meaningful values when the screen IS touched?

If so, simply tell touchout not to send anything if touchX is 0 and touchy is 0.

void touchOut()
{
   if(touchX > 0 && touchY > 0)
   {
      sendCC(20, char(touchX));  // send CC message 10
      sendCC(21, char(touchY));  // send CC message 11
   }
}

By the way, the char function only returns meaningful information for values below 256. The touchX and touchY variables can take on much larger values, can't they?

the values i get are semi meaningful, the devices only want to see 0-127 but when controlling, lets say a knob, with the screen i am able to turn the knob through its cycle about 4 times. i need to work that out next.

thank you very much for the help Paul

that line worked to make it not send out 0, 0 messages :slight_smile:
but when i hold one spot it will still send the same message over and over again. i have attached some of the output i get from holding a spot on the screen for a couple seconds.
(it is so sensitive the value drifts a little so that makes sense that it would show up but near the end there a a few 74, 86 values in a row, shouldn't they just be one entry until i moved to a new value?)

thanks!

oops here is the output data i forgot to attach to my last post

Controller, Channel: 1, Number: 20 (), Value: 91
Controller, Channel: 1, Number: 21 (), Value: 64
Controller, Channel: 1, Number: 20 (), Value: 84
Controller, Channel: 1, Number: 21 (), Value: 67
Controller, Channel: 1, Number: 20 (), Value: 84
Controller, Channel: 1, Number: 21 (), Value: 68
Controller, Channel: 1, Number: 20 (), Value: 85
Controller, Channel: 1, Number: 21 (), Value: 70
Controller, Channel: 1, Number: 20 (), Value: 85
Controller, Channel: 1, Number: 21 (), Value: 72
Controller, Channel: 1, Number: 20 (), Value: 86
Controller, Channel: 1, Number: 21 (), Value: 73
Controller, Channel: 1, Number: 20 (), Value: 86
Controller, Channel: 1, Number: 21 (), Value: 73
Controller, Channel: 1, Number: 20 (), Value: 85
Controller, Channel: 1, Number: 21 (), Value: 74
Controller, Channel: 1, Number: 20 (), Value: 86
Controller, Channel: 1, Number: 21 (), Value: 74
Controller, Channel: 1, Number: 20 (), Value: 86
Controller, Channel: 1, Number: 21 (), Value: 74
Controller, Channel: 1, Number: 20 (), Value: 86
Controller, Channel: 1, Number: 21 (), Value: 74
Controller, Channel: 1, Number: 20 (), Value: 85
Controller, Channel: 1, Number: 21 (), Value: 71

If you want, you can make it send a value only if the value is not the same as the previous value, or not within some distance of the previous value. That requires storing the value sent, and deciding on what constitutes a significant change.

thanks Paul!
(i sent you a pm)

Got the PM, but I prefer to reply here.

Create two new variables, lastTouchX and lastTouchY, of the same type as touchX and touchY. Initialize them to 0.

The touchOut function would then look like this:

void touchOut()
{
   if(touchX > 0 && touchY > 0)
   {
        if(touchX > lastTouchX + 5 || touchX < lastTouchX - 5)
       {
         sendCC(20, char(touchX));  // send CC message 10
           lastTouchY = touchY;
       }

        if(touchY > lastTouchY + 5 || touchY < lastTouchY - 5)
        {
            sendCC(21, char(touchY));  // send CC message 11
           lastTouchY = touchY;
        }
   }
}

Change the 5 in all 4 places to whatever you consider a significant change.

awesome! i will give that a try right now.

sadly that didn't seem to do the trick, i think i inserted it correct.

// DS TouchScreen
/*
#define xLow  14
#define xHigh 15
#define yLow  16
#define yHigh 17
*/
//modified to match my sparkfun connector
#define xLow  17
#define xHigh 15
#define yLow  16
#define yHigh 14
#define midiChannel 1

 int touchX, touchY;
 int lastTouchX, lastTouchY;
 
void setup(){
 //  Setup serial / MIDI
    Serial.begin(31250);       // MIDI speed
 //  Serial.begin(9600);    // Debug speed
}
 
void loop(){
 touchScan();
 touchOut(); 
   delay(200);
   
   
   
   

{
  int val = analogRead(0);
  val = map(val, 0, 1023, 0, 127);
  analogWrite(9, val);
}
{
  int val = analogRead(1);
  val = map(val, 0, 1023, 0, 127);
  analogWrite(9, val);
}
{
  int val = analogRead(2);
  val = map(val, 0, 1023, 0, 127);
  analogWrite(9, val);
}
{
  int val = analogRead(3);
  val = map(val, 0, 1023, 0, 127);
  analogWrite(9, val);
}

   
   
}

void touchOut()
{
   if(touchX > 0 && touchY > 0)
   {
        if(touchX > lastTouchX + 5 || touchX < lastTouchX - 5)
       {
         sendCC(20, char(touchX));  // send CC message 10
           lastTouchY = touchY;
       }

        if(touchY > lastTouchY + 5 || touchY < lastTouchY - 5)
        {
            sendCC(21, char(touchY));  // send CC message 11
           lastTouchY = touchY;
        }
   }
}



//  sends a CC message
void sendCC(char control, char data) {
  int cmd;
  cmd = 0xB0 | midiChannel;  // merge channel number
  Serial.print(cmd, BYTE);
  Serial.print((control & 0x7F), BYTE);
  Serial.print((data & 0x7F), BYTE);  // use the 0x7F to restrict the data to a maximum of 127
}

void touchScan(){
   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 !!
  touchX=analogRead(yLow -14);
 
  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 !!
  touchY=analogRead(xLow - 14); 
}

nevermind i got it to work! looking back at the code under the "lastTouchX" part it was then referred to as lastTouchY, i changed it to X in the appropriate places and sure enough its stopping the stream of messages.

thank you!!

now to get the constrain or map function in order and i'll be in business!!