#define Xin A0 #define Yin A1 #define Xout 0 #define Yout 1 #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 12.5 #define C_y 16.5 int readX; int readY; float X; float Y; bool btn1_prev = 0; bool btn1_current = 0; 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); pinMode(PIN_BTN1, INPUT); Serial.begin(9600); } void loop() { // Get button state btn1_current = digitalRead(PIN_BTN1); // Check to see if the button was pressed if (btn1_current & !btn1_prev) { // If the button has gone from low to high //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // 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 } // Shift our values btn1_prev = btn1_current; } 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; }