Map() Function - am i missing something?

hey guys, i have tried to implement the map function to this code to restrict the values sent from the touch screen hooked to analog 0-3 to MIDI CC values 0-127, but it isn't working as planned. did i not do 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;

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)
  {
   sendCC(20, char(touchX));  // send CC message 20
   sendCC(21, char(touchY));  // send CC message 21
}
}

//  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); 
}

Your use of map() seems fine, but why are you writing four different values all to pin D9 in succession? I didn't read much of your code past that point.

The map function may not be what you want. Look at the constrain function, too.

ah ha! yeah, i honestly don't know why it is going to D9. after looking at it again i am thinking that must be the problem but im not sure of the fix.

i am after sending the just the x and y values out as midi cc messages. should i change that D9 pin to D1 (the midi out pin?)

this map stuff is confusing me. thanks!

thanks Paul i will look in to that as well :)

try this:

// 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 pin9 = 9;

 int touchX, touchY;

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(pin9, val);
}
{
  int val = analogRead(1);
  val = map(val, 0, 1023, 0, 127);
  analogWrite(pin9, val);
}
{
  int val = analogRead(2);
  val = map(val, 0, 1023, 0, 127);
  analogWrite(pin9, val);
}
{
  int val = analogRead(3);
  val = map(val, 0, 1023, 0, 127);
  analogWrite(pin9, val);
}



}

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

//  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);
}

thank you Chris, i tried that code and it had no effect on the output :(

you're analogwriting to an unsigned pin i think... maybe some one else will jump in to help

Edit: How is this wired up?

void setup(){
 //  Setup serial / MIDI
    Serial.begin(31250);       // MIDI speed
 //  Serial.begin(9600);    // Debug speed
pin9 = OUTPUT; 
}

add that to that part and what you get?

yeah i think i am blowing it on the Digital 9 pin, i'm pretty sure it shouldn't be outputting there. the only output i am using is the Digital 1 pin for the midi out. do you think changing the D9 to D1 would help?

do you think changing the D9 to D1 would help?

give it a try see what happens

i tried switching the pin to D1 and still no luck there.

here is what i get for the Y value from bottom to top, the message starts in the high middle range rather than 0, and cycles 6 times then ends on a wrong message, rather than 127.

Controller, Channel: 1, Number: 21 (), Value: 81
Controller, Channel: 1, Number: 21 (), Value: 75
Controller, Channel: 1, Number: 21 (), Value: 96
Controller, Channel: 1, Number: 21 (), Value: 112
Controller, Channel: 1, Number: 21 (), Value: 16
Controller, Channel: 1, Number: 21 (), Value: 28
Controller, Channel: 1, Number: 21 (), Value: 54
Controller, Channel: 1, Number: 21 (), Value: 65
Controller, Channel: 1, Number: 21 (), Value: 76
Controller, Channel: 1, Number: 21 (), Value: 88
Controller, Channel: 1, Number: 21 (), Value: 111
Controller, Channel: 1, Number: 21 (), Value: 2
Controller, Channel: 1, Number: 21 (), Value: 20
Controller, Channel: 1, Number: 21 (), Value: 45
Controller, Channel: 1, Number: 21 (), Value: 67
Controller, Channel: 1, Number: 21 (), Value: 79
Controller, Channel: 1, Number: 21 (), Value: 88
Controller, Channel: 1, Number: 21 (), Value: 101
Controller, Channel: 1, Number: 21 (), Value: 117
Controller, Channel: 1, Number: 21 (), Value: 3
Controller, Channel: 1, Number: 21 (), Value: 18
Controller, Channel: 1, Number: 21 (), Value: 32
Controller, Channel: 1, Number: 21 (), Value: 44
Controller, Channel: 1, Number: 21 (), Value: 60
Controller, Channel: 1, Number: 21 (), Value: 75
Controller, Channel: 1, Number: 21 (), Value: 92
Controller, Channel: 1, Number: 21 (), Value: 107
Controller, Channel: 1, Number: 21 (), Value: 0
Controller, Channel: 1, Number: 21 (), Value: 17
Controller, Channel: 1, Number: 21 (), Value: 32
Controller, Channel: 1, Number: 21 (), Value: 54
Controller, Channel: 1, Number: 21 (), Value: 71
Controller, Channel: 1, Number: 21 (), Value: 100
Controller, Channel: 1, Number: 21 (), Value: 123
Controller, Channel: 1, Number: 21 (), Value: 26
Controller, Channel: 1, Number: 21 (), Value: 46
Controller, Channel: 1, Number: 21 (), Value: 68
Controller, Channel: 1, Number: 21 (), Value: 87
Controller, Channel: 1, Number: 21 (), Value: 103
Controller, Channel: 1, Number: 21 (), Value: 114
Controller, Channel: 1, Number: 21 (), Value: 4
Controller, Channel: 1, Number: 21 (), Value: 17
Controller, Channel: 1, Number: 20 (), Value: 52
Controller, Channel: 1, Number: 21 (), Value: 38
Controller, Channel: 1, Number: 21 (), Value: 45
Controller, Channel: 1, Number: 20 (), Value: 44

the same thing is true for the X values.

this is the latest and greatest code im working with:

// 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;
 int pin1 = 1;

 
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(pin1, val);
}
{
  int val = analogRead(1);
  val = map(val, 0, 1023, 0, 127);
  analogWrite(pin1, val);
}
{
  int val = analogRead(2);
  val = map(val, 0, 1023, 0, 127);
  analogWrite(pin1, val);
}
{
  int val = analogRead(3);
  val = map(val, 0, 1023, 0, 127);
  analogWrite(pin1, val);
}




   
   
 touchX = constrain(touchX, 0, 127);
  touchY = constrain(touchY, 0, 127);


   
   
}

void touchOut()
{
   if(touchX > 0 && touchY > 0)
   {
        if(touchX > lastTouchX + 5 || touchX < lastTouchX - 5) //the 5 can be any whole number step
       {
         sendCC(20, char(touchX));  // send CC message 10
           lastTouchX = touchX;
       }

        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); 
}

You get values for touchX and touchY in touchScan. Then, you write them out in touchOut. After writing the values, you constrain them to be in a valid range.

You want to constrain the values after reading, but before writing.

What is connected to pins 0, 1, 2, and 3? You are reading something from these pins, and then doing an analogWrite to pin 1. The analogWrite function affects digital pins, and is only useful for writing to PWM pins. Digital pin 1 is not a PWM pin.

 [glow]  {[/glow]
  int val = analogRead(0);
  val = map(val, 0, 1023, 0, 127);
  analogWrite(pin1, val);
[glow]}[/glow]

The highlighted { and } are not needed.

hi Paul,
figured out that those 4 map entries in the void loop writing to pin 1 were not needed/not doing anything.
i placed the map functions at the end where they seem to be semi working.
I now get a good max value of 127 but the problem is the value int he bottom corner of the screen is not 0, it is more like 88.

here is the section of code with the working map function on place.

//xLow has analog port -14 !!
  touchX=analogRead(yLow -14);
  touchX = map(touchX , 0, 1023, 0, 127); 
 
  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); 
  touchY = map(touchY , 0, 1023, 0, 127);
}

maybe i should try out something like:

touchX = constrain (touchX, 0 , 127);
touchY = constrain (touchY, 0 , 127);

do the map and constrain codes work together or should i use one or the other?

okay, i tried adding the constrain function under the map functions and it didn't seem to do anything.

then i tried removing the map function leaving just the constrain functions and that stopped any midi messages from being sent out.

finally i put the map function back in under the constrain functions and midi was still not being sent out.

this leads me to think my constrain function is wrong and/or in the wrong area. :-?

You would normally use either map or constrain.

Without the map calls, do the values of touchX and touchY range from 0 to 1023?

Without the map calls, do the values of touchX and touchY range from 0 to 1023?

no, they start at an oddball number like 60 or 80 and then increase to 127 then from 1-127 5 more times before reaching the top of the x value.

that would be due to the data limits enforced by this right?

  Serial.print(cmd, BYTE);
  Serial.print((control & 0x7F), BYTE);
  Serial.print((data & 0x7F), BYTE);

That code is sending data to the midi controller. It has nothing to do with the touchscreen.

I have no idea why you are not getting values from 0 to 1023.

i may have misunderstood you Paul, i am only checking the values with my midi input monitor, should i be checking the data with the serial monitor? i tried setting Serial.begin(9600) and checking serial monitor but the only thing that came up was random characters, no numbers

oh man! i am an idiot…my cat knocked my board over last night and while hooking up the connections that came loose i wired up the screen wrong :-[
now i have things working almost 100%

with a little tweaking i should git it spot on.

thank you so much for the help and i’m sorry for my mistake!