What Is .map? What does it do?

Ok, I know, this is kinda silly, but what is .map from the TouchScreen Library?

p.x = map(p.x, TS_LEFT, TS_RT, 0, tft.width());
p.y = map(p.y, TS_TOP, TS_BOT, 0, tft.width());

That is the code that I want to know what it does.

What values does it return and how can I use those values?

I am trying to make a paint program with MCUFRIEND.kbv

This is my entire code, if it’s needed:

#include <TouchScreen.h>
#include <MCUFRIEND_kbv.h>
#include <Adafruit_GFX.h>

const int XP = 8, XM = A2, YP = A3, YM = 9; //320x480 ID=0x9486
const int TS_LEFT = 138, TS_RT = 911, TS_TOP = 929, TS_BOT = 145;

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);


MCUFRIEND_kbv tft;

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF


int pixel_x, pixel_y;
void setup() {
  Serial.begin(9600);

}

void loop() {
  TSPoint p = ts.getPoint();

  if (p.z > ts.pressureThreshhold) {

    p.x = map(p.x, TS_LEFT, TS_RT, 0, tft.width());
    p.y = map(p.y, TS_TOP, TS_BOT, 0, tft.width());

    tft.drawCircle(p.x, p.y, 5, BLACK);

    Serial.print("\nX:");
    Serial.print(p.x);
    Serial.print("\tY:");
    Serial.print(p.y);


  }}

Thanks in Advance!

What you are showing is not .map but map - a standard maths function:
map() - Arduino Reference

Ok, cool, but then…

How do I find out where someone is touching the screen?

I’m not familiar with the libraries you are using, but surely p.x and p.y give you the location of where the screen was touched - possibly before being maped?

It looks like p.x is in the range 138 to 911 and p.y is in the range 929 to 145. The map() calls are mapping those ranges to the full width and the full height of the display. After the mapping, p.x and p.y represent the point on the display that was touched.

1 Like

Thank you so much! I now understand, however…

In this line of code, I give p.x & p.y as the location to draw a circle.

tft.drawCircle(p.x, p.y, 5, BLACK);

It doesn’t work.

I don’t think that p.x & p.y are the problems though.

That is because I tried to draw a rectangle, I did it correctly because I’ve done it before.

It does not show up.

I’m not quite sure what the problem is as it compiled and uploaded just fine.

Maybe the TouchScreen & GFX library are competing for pins?

But I don’t know how to fix that.

Thanks in advance!