Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.42 KB | None | 0 0
  1. /*______Import Libraries_______*/
  2. #include <Adafruit_GFX.h> // Core graphics library
  3. #include <MCUFRIEND_kbv.h> // Hardware-specific library
  4. #include <TouchScreen.h>
  5. /*______End of Libraries_______*/
  6.  
  7. #define YP A1 // must be an analog pin, use "An" notation!
  8. #define XM A2 // must be an analog pin, use "An" notation!
  9. #define YM 7 // can be a digital pin
  10. #define XP 6 // can be a digital pin
  11. #define LCD_CS A3
  12. # define LCD_CD A2
  13. # define LCD_WR A1
  14. # define LCD_RD A0
  15. # define LCD_RESET A4
  16. # define WHITE 0x0000 //Black->White
  17. #define YELLOW 0x001F //Blue->Yellow
  18. #define CYAN 0xF800 //Red->Cyan
  19. #define PINK 0x07E0 //Green-> Pink
  20. #define RED 0x07FF //Cyan -> Red
  21. #define GREEN 0xF81F //Pink -> Green
  22. #define BLUE 0xFFE0 //Yellow->Blue
  23. #define BLACK 0xFFFF //White-> Black
  24. #define MINPRESSURE 2
  25. #define MAXPRESSURE 1000
  26. #define TS_MINX 0
  27. #define TS_MINY 0
  28. #define TS_MAXX 600
  29. #define TS_MAXY 600
  30.  
  31. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300); //300 is the sensitivity
  32. MCUFRIEND_kbv tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET); //Start communication with LCD
  33.  
  34. String symbol[4][4] = {
  35. { "7", "8", "9", "/"},
  36. { "4", "5", "6", "*"},
  37. { "1", "2", "3", "-"},
  38. { "C", "0", "=", "+"}
  39. };
  40. int X, Y, action;
  41. long Num1, Num2, Number;
  42.  
  43. void setup() {
  44. Serial.begin(9600); //Use serial monitor for debugging
  45. uint16_t ID = tft.readID();
  46. if (ID == 0xD3D3)
  47. ID = 0x9486; // write-only shield
  48. tft.reset(); //Always reset at start
  49. tft.begin(ID); // My LCD uses LIL9341 Interface driver IC
  50. tft.setRotation(0); // I just roated so that the power jack faces up - optional
  51. tft.fillScreen(WHITE);
  52.  
  53. IntroScreen();
  54.  
  55. draw_BoxNButtons();
  56. }
  57.  
  58. void loop() {
  59. TSPoint p = waitTouch();
  60. X = p.x;
  61. Y = p.y;
  62.  
  63. DetectButtons();
  64.  
  65. DisplayResult();
  66.  
  67. delay(300);
  68. }
  69.  
  70. TSPoint waitTouch() {
  71. TSPoint p;
  72. do {
  73. p = ts.getPoint();
  74. pinMode(XM, OUTPUT);
  75. pinMode(YP, OUTPUT);
  76. } while ((p.z < MINPRESSURE) || (p.z > MAXPRESSURE));
  77. p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240);
  78. p.y = map(p.y, TS_MINY, TS_MAXY, 0, 320);
  79. return p;
  80. }
  81.  
  82. void DetectButtons() {
  83. Serial.print("X = ");
  84. Serial.println(X);
  85. Serial.print("Y = ");
  86. Serial.println(Y);
  87.  
  88. if (X < 150 && X > 65) { //Detecting Buttons on Column 1
  89. if (Y > 350 && Y < 410) { //If cancel Button is pressed
  90. Serial.println("Button Cancel");
  91. Number = Num1 = Num2 = 0;
  92. action = -1;
  93. }
  94. else if (Y > 280 && Y < 335) { //If Button 1 is pressed
  95. Serial.println("Button 1");
  96. Number = (Number * 10) + 1;
  97. }
  98. else if (Y > 225 && Y < 270) { //If Button 4 is pressed
  99. Serial.println("Button 4");
  100. Number = (Number * 10) + 4;
  101. }
  102. else if (Y > 120 && Y < 200) { //If Button 7 is pressed
  103. Serial.println("Button 7");
  104. Number = (Number * 10) + 7;
  105. }
  106. }
  107. else if (X < 210 && X > 150) { //Detecting Buttons on Column 2
  108. if (Y > 350 && Y < 410) {
  109. Serial.println("Button 0"); //Button 0 is Pressed
  110. Number = (Number * 10) + 0;
  111. }
  112. else if (Y > 280 && Y < 335) {
  113. Serial.println("Button 2");
  114. Number = (Number * 10) + 2;
  115. }
  116. else if (Y > 225 && Y < 270) {
  117. Serial.println("Button 5");
  118. Number = (Number * 10) + 5;
  119. }
  120. else if (Y > 120 && Y < 200) {
  121. Serial.println("Button 8");
  122. Number = (Number * 10) + 8;
  123. }
  124. }
  125. else if (X < 280 && X > 210) { //Detecting Buttons on Column 3
  126. if (Y > 350 && Y < 410) {
  127. Serial.println("Button Equal");
  128. CalculateResult();
  129. }
  130. else if (Y > 280 && Y < 335) {
  131. Serial.println("Button 3");
  132. Number = (Number * 10) + 3;
  133. }
  134. else if (Y > 225 && Y < 270) {
  135. Serial.println("Button 6");
  136. Number = (Number * 10) + 6;
  137. }
  138. else if (Y > 120 && Y < 200) {
  139. Serial.println("Button 9");
  140. Number = (Number * 10) + 9;
  141. }
  142. }
  143. else if (X < 345 && X > 280) { //Detecting Buttons on Column 4
  144. tft.setCursor(200, 20);
  145. tft.setTextColor(RED);
  146. if (Y > 350 && Y < 410) {
  147. Serial.println("Addition");
  148. action = 0;
  149. tft.println('+');
  150. Num1 = Number;
  151. Number = 0;
  152. }
  153. else if (Y > 280 && Y < 335) {
  154. Serial.println("Subtraction");
  155. action = 1;
  156. tft.println('-');
  157. Num1 = Number;
  158. Number = 0;
  159. }
  160. else if (Y > 225 && Y < 270) {
  161. Serial.println("Multiplication");
  162. action = 2;
  163. tft.println('*');
  164. Num1 = Number;
  165. Number = 0;
  166. }
  167. else if (Y > 120 && Y < 200) {
  168. Serial.println("Division");
  169. action = 3;
  170. tft.println('/');
  171. Num1 = Number;
  172. Number = 0;
  173. }
  174. }
  175. X = Y = -1;
  176. delay(300);
  177. }
  178.  
  179. void CalculateResult() {
  180. Num2 = Number;
  181. if (action == -1) {
  182. Number = Num1;
  183. Num2 = 0;
  184. }
  185. else if (action == 0) {
  186. Number = Num1 + Num2;
  187. Num1 = Number;
  188. Num2 = 0;
  189. }
  190. else if (action == 1) {
  191. Number = Num1 - Num2;
  192. Num1 = Number;
  193. Num2 = 0;
  194. }
  195. else if (action == 2) {
  196. Number = Num1 * Num2;
  197. Num1 = Number;
  198. Num2 = 0;
  199. }
  200. else if (action == 3) {
  201. Number = Num1 / Num2;
  202. Num1 = Number;
  203. Num2 = 0;
  204. }
  205. action = -1;
  206. }
  207.  
  208. void DisplayResult() {
  209. tft.fillRect(0, 0, 240, 80, CYAN); //clear result box
  210. tft.setCursor(10, 20);
  211. tft.setTextSize(4);
  212. tft.setTextColor(BLACK);
  213. tft.println(Number); //update new value
  214. }
  215.  
  216. void IntroScreen() {
  217. tft.setCursor(55, 120);
  218. tft.setTextSize(3);
  219. tft.setTextColor(RED);
  220. tft.println("ARDUINO");
  221. tft.setCursor(30, 160);
  222. tft.println("CALCULATOR");
  223. tft.setCursor(30, 220);
  224. tft.setTextSize(2);
  225. tft.setTextColor(BLUE);
  226. tft.println(" CSED 19");
  227. delay(1800);
  228. }
  229.  
  230. void draw_BoxNButtons() {
  231. //Draw the Result Box
  232. tft.fillRect(0, 0, 240, 80, CYAN);
  233.  
  234. //Draw First Column
  235. tft.fillRect(0, 260, 60, 60, RED);
  236. tft.fillRect(0, 200, 60, 60, BLACK);
  237. tft.fillRect(0, 140, 60, 60, BLACK);
  238. tft.fillRect(0, 80, 60, 60, BLACK);
  239.  
  240. //Draw Third Column
  241. tft.fillRect(120, 260, 60, 60, GREEN);
  242. tft.fillRect(120, 200, 60, 60, BLACK);
  243. tft.fillRect(120, 140, 60, 60, BLACK);
  244. tft.fillRect(120, 80, 60, 60, BLACK);
  245.  
  246. //Draw Secound & Fourth Column
  247. for (int b = 260; b >= 80; b -= 60) {
  248. tft.fillRect(180, b, 60, 60, BLUE);
  249. tft.fillRect(60, b, 60, 60, BLACK);
  250. }
  251.  
  252. //Draw Horizontal Lines
  253. for (int h = 80; h <= 320; h += 60)
  254. tft.drawFastHLine(0, h, 240, WHITE);
  255.  
  256. //Draw Vertical Lines
  257. for (int v = 0; v <= 240; v += 60)
  258. tft.drawFastVLine(v, 80, 240, WHITE);
  259.  
  260. //Display keypad lables
  261. for (int j = 0; j < 4; j++) {
  262. for (int i = 0; i < 4; i++) {
  263. tft.setCursor(22 + (60 * i), 100 + (60 * j));
  264. tft.setTextSize(3);
  265. tft.setTextColor(WHITE);
  266. tft.println(symbol[j][i]);
  267. }
  268. }
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement