Touch panel module

Touch panel module

This page is to Setup and Config Touch panel module

Hardware and Software

Hardware

Touch panel module

Software

Arduino IDE

Sanki Notes

Examples

This example is ..........

            1. /*

            2. * Created By: Ankush Verma & Cory Malantonio

            3. * Kushy04@gmail.com

            4. * cgmalantonio@gmail.com

            5. *

            6. * Y- to analog 0

            7. * X+ to analog 1

            8. * Y+ to analog 2

            9. * X- to analog 3

            10. *

            11. */

            12. int xVal = 0;

            13. int yVal = 0;

            14. int touchX;

            15. int touchY;

            16. int xPos;

            17. int yPos;

            18. int redPin = 11;

            19. int greenPin = 9;

            20. int bluePin = 10;

            21. int rL = 0;

            22. int bL = 0;

            23. int gL = 0;

            24. void setup()

            25. {

            26. pinMode(redPin, OUTPUT);

            27. pinMode(bluePin, OUTPUT);

            28. pinMode(greenPin, OUTPUT);

            29. Serial.begin(38400);

            30. }

            31. void loop()

            32. {

            33. // Set up the analog pins in preparation for reading the Y value

            34. pinMode( A1, INPUT ); // Analog pin 1

            35. pinMode( A3, INPUT ); // Analog pin 3

            36. pinMode( A0, OUTPUT ); // Analog pin 0

            37. digitalWrite( A0, LOW ); // Analog pin 0 as GND connection

            38. pinMode( A2, OUTPUT ); // Analog pin 2

            39. digitalWrite( A2, HIGH ); // Analog pin 2 as +5V connection

            40. yVal = analogRead( 1 ); // Read the Y value

            41. // Set up the analog pins in preparation for reading the Y value

            42. // from the touchscreen

            43. pinMode( A0, INPUT ); // Analog pin 0

            44. pinMode( A2, INPUT ); // Analog pin 2

            45. pinMode( A1, OUTPUT ); // Analog pin 1

            46. digitalWrite( A1, LOW ); // Analog pin 1 as GND connection

            47. pinMode( A3, OUTPUT ); // Analog pin 3

            48. digitalWrite( A3, HIGH ); // Analog pin 3 as +5V connection

            49. xVal = analogRead( 0 ); // Read the x value

            50. //when touchscreen is not pressed it rests at certain coordinates

            51. //this makes sure the coordinates at rest don't get calculated

            52. touchX = xVal;

            53. if( touchX > 20 && touchX < 900 ) {

            54. //// Here is where you need your minimum X value and Range of X

            55. //// (touchX, minimum x, range of x, converts to 0-1024)

            56. xPos = map(touchX, 108.0, 899.0, 0, 1024);

            57. xPos = constrain(xPos, 0, 1024);

            58. }

            59. touchY = yVal;

            60. if( touchY > 20 && touchY < 900 ) {

            61. //// Here is where you need your minimum Y value and Range of Y

            62. //// (touchY, minimum y, range of y, converts to 0-1024)

            63. yPos = map(touchY, 145.0, 892.0, 0, 1024);

            64. yPos = constrain(yPos, 0, 1024);

            65. }

            66. float brightness;

            67. brightness = int (yPos)/1024.0;

            68. //this lovely bit converts 0-1024, into three 255 values

            69. int value = int (xPos);

            70. float RGBslider = (float)value/1024.0;

            71. float redLevel = 128.0 * ( 2 * cos( 2 * PI * (RGBslider + 0.125)));

            72. float greenLevel = 128.0 * ( 2 * cos( 2 * PI * (RGBslider + 0.375)));

            73. float blueLevel = 128.0 * ( 2 * cos( PI * RGBslider));

            74. //allows values of color to be affected by value of brightness

            75. redLevel = redLevel * (brightness);

            76. greenLevel = greenLevel * (brightness);

            77. blueLevel = blueLevel * (brightness);

            78. /*

            79. if (redLevel > 255) redLevel = 255;

            80. if (redLevel < 0) redLevel = 0;

            81. if (greenLevel > 255) greenLevel = 255;

            82. if (greenLevel < 0) greenLevel = 0;

            83. if (blueLevel > 255) blueLevel = 255;

            84. if (blueLevel < 0) blueLevel = 0;

            85. */

            86. //does same thing as section commented out above

            87. redLevel = constrain(redLevel, 0, 255);

            88. greenLevel = constrain(greenLevel, 0, 255);

            89. blueLevel = constrain(blueLevel, 0, 255);

            90. //rgb led's being used work backwards

            91. //0 = on || 255 = off

            92. //this reverses normal values

            93. rL = 255 - int(redLevel);

            94. bL = 255 - int(blueLevel);

            95. gL = 255 - int(greenLevel);

            96. analogWrite(redPin, rL);

            97. analogWrite(greenPin, gL);

            98. analogWrite(bluePin, bL);

            99. Serial.print(brightness);

            100. Serial.print(":");

            101. Serial.print(rL);

            102. Serial.print(".");

            103. Serial.print(gL);

            104. Serial.print(".");

            105. Serial.print(bL);

            106. Serial.print("-----");

            107. Serial.print(xPos);

            108. Serial.print(" / ");

            109. Serial.println(yPos);

            110. //Serial.print(xPos);

            111. //Serial.println(yPos);

            112. delay(100);

            113. }