首页 > 解决方案 > 读取 4 线电阻式触摸屏的坐标

问题描述

我用 Arduino Mega2560 连接了我的 4 线触摸屏。我使用了以下指南:https ://www.instructables.com/Resistive-Touch-Screen-on-the-DP32/ 。但是我从触摸屏输出中获得的坐标还不够。当我沿着 X 轴移动时,Y 轴的坐标也在变化。只是一点点,但差异会随着距离的增加而增加。

所以我的主要问题是,我无法正确校准我的 4 线触摸屏。

我从指南第 7 步的计算中得到了 a 和 b 的值。

如有任何问题,这是我的代码:

#define Yin  A0
#define Yout A1
#define Xin  A2
#define Xout A3


#define a_1 0.02281
#define b_1 198.2
#define a_3 -0.01455
#define b_3 704.2

#define a_4 -0.01707
#define b_4 83.41
#define a_6 0.01996
#define b_6 829.0

#define C_x 22.81
#define C_y 30.41

int readX;
int readY;
float X;
float Y;

float correctX(int measuredX, int measuredY);
float correctY(int measuredX, int measuredY);

void setup()
{
  pinMode(Xin, INPUT);
  pinMode(Yin, INPUT);
  pinMode(Xout, INPUT);
  pinMode(Yout, INPUT);

  Serial.begin(9600);
}

void loop()
{
  { 

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Read X
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Set Y to a gradient
    pinMode(Yin, OUTPUT);
    pinMode(Yout, OUTPUT);
    digitalWrite(Yin, LOW);
    digitalWrite(Yout, HIGH);
    delay(5); // Pause to allow lines to power up
    // Read and store X
    readX = analogRead(Xin);
    // Reset Y
    digitalWrite(Yout, LOW);
    delay(5); // Pause to allow lines to power down
    pinMode(Yin, INPUT);
    pinMode(Yout, INPUT);

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Read Y
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Set X to a gradient
    pinMode(Xin, OUTPUT);
    pinMode(Xout, OUTPUT);
    digitalWrite(Xin, LOW);
    digitalWrite(Xout, HIGH);
    delay(5); // Pause to allow lines to power up
    // Read and store Y
    readY = analogRead(Yin);
    // Reset X
    digitalWrite(Xout, LOW);
    delay(5); // Pause to allow lines to power down
    pinMode(Xin, INPUT);
    pinMode(Xout, INPUT);

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Correct X
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    X = correctX(readX, readY); 

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Correct Y
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Y = correctY(readX, readY);

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Output our values
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Serial.print(X);
    Serial.print("\t");
    Serial.println(Y);
    
    delay(250); // Debounce
  }

}

float correctX(int measuredX, int measuredY)
{
  float correctedX;
  float temp;

  temp = (a_4 * measuredY) + b_4;
  correctedX = measuredX - temp;
  temp = (a_6 * measuredY) + b_6;
  correctedX = correctedX / temp;
  correctedX = correctedX * C_x;

  return correctedX;
}

float correctY(int measuredX, int measuredY)
{
  float correctedY;
  float temp;

  temp = (a_1 * measuredX) + b_1;
  correctedY = measuredY - temp;
  temp = (a_3 * measuredX) + b_3;
  correctedY = correctedY / temp;
  correctedY = correctedY * C_y;

  return correctedY;
}

如果有人可以进一步帮助我,将不胜感激。

标签: arduinotouchscreencalibration

解决方案


推荐阅读