I took one of those Sony Ericsson touchscreens I got from DealExtreme, along with a SparkFun Nintendo DS touchscreen breakout board (it works with this touchscreen too), and put it on my KS0108 MONOCHRON GLCD screen after a lot of messing around with wires. The touchscreen is actually a really good size for this screen, it’s just slightly larger but it’s a very good fit. I knew it was a good idea to buy two of them though, because I ended up breaking one of them (the wires are very fragile and it got torn off), which is why I decided to use twenty layers of tape to protect the wires on the second one. The only downside to using this touchscreen with the GLCD is that now I only have one analog input pin left.

GLCD touchscreen 1

Random scribbles on the touchscreen GLCD

Walfas!Ika Musume!Mils!Marisa!BIRD!!Tree!!Another tree!

I based the code on this, which is also a useful guide if you want to do something similar with a touchscreen.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// GLCD library can be found here:
// http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1279128237/0
#include <glcd.h>
 
// A1 corresponds to analog pin #1, etc
// Same as pin number 15 (that is, add 14 to it)
#define X1_PIN A4
#define X2_PIN A2
#define Y1_PIN A1
#define Y2_PIN A3 
 
#define BOX_SIZE 8
#define DELAY_TIME 3
 
int prevPos[] = {0,0}; // Previous position pressed
int pos[] = {0,0};  // Current position pressed
 
// Change these values to correspond to your touchscreen;
// X and Y values from 0 to 1023 come from the analogRead()
// in the getTCoords() function
int topLeftXY[] = {830,715};
int bottomRightXY[] = {55,165};
 
void setup(){
  GLCD.Init(INVERTED);
  GLCD.ClearScreen();
  drawQuitBox();
}
 
void loop(){ 
  if ( getTCoords() ) {
    // If the touched position is not 0,0, draw a dot
    GLCD.SetDot(prevPos[0], prevPos[1], BLACK);
    delay(DELAY_TIME);
  }
  if ( inQuitBox() ) {
    if ( justPressed() )
      // Screen is cleared if the quit box is 'clicked'
      GLCD.ClearScreen(); 
    drawQuitBox();
  }
}
 
boolean justPressed() {
  // Checks to see the input is a "click"; that is,
  // right before, there was nothing touching the screen
  if (prevPos[0] == 0 && prevPos[1] == 0 && pos[0] != 0 && pos[1] != 0) {
    return true;
  }
  else
    return false;
}
 
void drawQuitBox() {
  // Box at the top right corner with an X in it
  GLCD.FillRect(GLCD.Right-BOX_SIZE, 0, BOX_SIZE, BOX_SIZE-1, WHITE);
  GLCD.DrawRect(GLCD.Right-BOX_SIZE, 0, BOX_SIZE, BOX_SIZE);
  GLCD.DrawLine(GLCD.Right-BOX_SIZE, 0, GLCD.Right, BOX_SIZE);
  GLCD.DrawLine(GLCD.Right, 0, GLCD.Right-BOX_SIZE, BOX_SIZE);
}
 
boolean inQuitBox() {
  // Returns true if the current touched position
  // is in the quit box
  if ( (GLCD.Right-BOX_SIZE-1 <= pos[0]) && (pos[1] <= BOX_SIZE+1) )
    return true;
  else
    return false;
}
 
boolean getTCoords() {  
  // Gets the touched position on the screen
  pinMode(X1_PIN,OUTPUT);
  pinMode(X2_PIN,OUTPUT);
  digitalWrite(X1_PIN,LOW);
  digitalWrite(X2_PIN,HIGH);
 
  digitalWrite(Y1_PIN,LOW);
  digitalWrite(Y2_PIN,LOW);
 
  pinMode(Y1_PIN,INPUT);
  pinMode(Y2_PIN,INPUT);
 
  prevPos[0] = pos[0];
  pos[0] = analogRead(Y1_PIN-14); // Subtract 14 to get the analog pin number
 
  pinMode(Y1_PIN,OUTPUT);
  pinMode(Y2_PIN,OUTPUT);
  digitalWrite(Y1_PIN,LOW);
  digitalWrite(Y2_PIN,HIGH);
 
  digitalWrite(X1_PIN,LOW);
  digitalWrite(X2_PIN,LOW);
 
  pinMode(X1_PIN,INPUT);
  pinMode(X2_PIN,INPUT);
 
  prevPos[1] = pos[1];
  pos[1] = analogRead(X1_PIN - 14); // Subtract 14 to get the analog pin number
 
  // Non-zero values get converted to a point on the screen
  if (pos[0] != 0 && pos[1] != 0) {
    pos[0] = (int) map(pos[0], topLeftXY[0], bottomRightXY[0], 0, GLCD.Width);
    pos[1] = (int) map(pos[1], topLeftXY[1], bottomRightXY[1], 0, GLCD.Height);
    return true;
  }
  else {
    return false;
  }
}
Tagged with:
 

3 Responses to GLCD with touchscreen

  1. htfkid2000 says:

    Very well done! now you have your self an electronic drawing tablet without the mess of an Operating System! It would be great to interface this thing with an SD Card and you can store your pictures with a push of a button.

    • Walfas says:

      I was thinking of something like that, actually! I don’t think I’d be able to fit any more stuff into that box though.

  2. Walfas says:

    Whoops, in one of the comments in the code it says add 13 to the value, should say 14. Not that comments affect the code or anything though. fixed