Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.87 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. double Num1, Num2, Number;
  42. bool lastEqual;
  43.  
  44. void setup() {
  45. Serial.begin(9600); //Use serial monitor for debugging
  46. uint16_t ID = tft.readID();
  47. if (ID == 0xD3D3)
  48. ID = 0x9486; // write-only shield
  49. tft.reset(); //Always reset at start
  50. tft.begin(ID); // My LCD uses LIL9341 Interface driver IC
  51. tft.setRotation(0); // I just roated so that the power jack faces up - optional
  52. tft.fillScreen(WHITE);
  53.  
  54. IntroScreen();
  55.  
  56. draw_BoxNButtons();
  57. lastEqual = false;
  58. }
  59.  
  60. void loop() {
  61. TSPoint p = waitTouch();
  62. X = p.x;
  63. Y = p.y;
  64.  
  65. DetectButtons();
  66.  
  67. DisplayResult();
  68.  
  69. delay(300);
  70. }
  71.  
  72. TSPoint waitTouch() {
  73. TSPoint p;
  74. do {
  75. p = ts.getPoint();
  76. pinMode(XM, OUTPUT);
  77. pinMode(YP, OUTPUT);
  78. } while ((p.z < MINPRESSURE) || (p.z > MAXPRESSURE));
  79. p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240);
  80. p.y = map(p.y, TS_MINY, TS_MAXY, 0, 320);
  81. return p;
  82. }
  83.  
  84. void DetectButtons() {
  85. Serial.print("X = ");
  86. Serial.println(X);
  87. Serial.print("Y = ");
  88. Serial.println(Y);
  89.  
  90. if (X < 150 && X > 65) { //Detecting Buttons on Column 1
  91. if (Y > 350 && Y < 410) { //If cancel Button is pressed
  92. Serial.println("Button Cancel");
  93. Number = Num1 = Num2 = 0;
  94. action = -1;
  95. }
  96. else if (Y > 280 && Y < 335) { //If Button 1 is pressed
  97. Serial.println("Button 1");
  98. if (lastEqual) {
  99. lastEqual = false;
  100. Number = Num1 = Num2 = 0;
  101. }
  102. Number = (Number * 10) + 1;
  103. }
  104. else if (Y > 225 && Y < 270) { //If Button 4 is pressed
  105. Serial.println("Button 4");
  106. if (lastEqual) {
  107. lastEqual = false;
  108. Number = Num1 = Num2 = 0;
  109. }
  110. Number = (Number * 10) + 4;
  111. }
  112. else if (Y > 120 && Y < 200) { //If Button 7 is pressed
  113. Serial.println("Button 7");
  114. if (lastEqual) {
  115. lastEqual = false;
  116. Number = Num1 = Num2 = 0;
  117. }
  118. Number = (Number * 10) + 7;
  119. }
  120. }
  121. else if (X < 210 && X > 150) { //Detecting Buttons on Column 2
  122. if (Y > 350 && Y < 410) {
  123. Serial.println("Button 0"); //Button 0 is Pressed
  124. if (lastEqual) {
  125. lastEqual = false;
  126. Number = Num1 = Num2 = 0;
  127. }
  128. Number = (Number * 10) + 0;
  129. }
  130. else if (Y > 280 && Y < 335) {
  131. Serial.println("Button 2");
  132. if (lastEqual) {
  133. lastEqual = false;
  134. Number = Num1 = Num2 = 0;
  135. }
  136. Number = (Number * 10) + 2;
  137. }
  138. else if (Y > 225 && Y < 270) {
  139. Serial.println("Button 5");
  140. if (lastEqual) {
  141. lastEqual = false;
  142. Number = Num1 = Num2 = 0;
  143. }
  144. Number = (Number * 10) + 5;
  145. }
  146. else if (Y > 120 && Y < 200) {
  147. Serial.println("Button 8");
  148. if (lastEqual) {
  149. lastEqual = false;
  150. Number = Num1 = Num2 = 0;
  151. }
  152. Number = (Number * 10) + 8;
  153. }
  154. }
  155. else if (X < 280 && X > 210) { //Detecting Buttons on Column 3
  156. if (Y > 350 && Y < 410) {
  157. Serial.println("Button Equal");
  158. CalculateResult();
  159. lastEqual = true;
  160. }
  161. else if (Y > 280 && Y < 335) {
  162. Serial.println("Button 3");
  163. if (lastEqual) {
  164. lastEqual = false;
  165. Number = Num1 = Num2 = 0;
  166. }
  167. Number = (Number * 10) + 3;
  168. }
  169. else if (Y > 225 && Y < 270) {
  170. Serial.println("Button 6");
  171. if (lastEqual) {
  172. lastEqual = false;
  173. Number = Num1 = Num2 = 0;
  174. }
  175. Number = (Number * 10) + 6;
  176. }
  177. else if (Y > 120 && Y < 200) {
  178. Serial.println("Button 9");
  179. if (lastEqual) {
  180. lastEqual = false;
  181. Number = Num1 = Num2 = 0;
  182. }
  183. Number = (Number * 10) + 9;
  184. }
  185. }
  186. else if (X < 345 && X > 280) { //Detecting Buttons on Column 4
  187. tft.setCursor(200, 20);
  188. tft.setTextColor(RED);
  189. if (Y > 350 && Y < 410) {
  190. Serial.println("Addition");
  191. action = 0;
  192. tft.println('+');
  193. Num1 = Number;
  194. Number = 0;
  195. }
  196. else if (Y > 280 && Y < 335) {
  197. Serial.println("Subtraction");
  198. action = 1;
  199. tft.println('-');
  200. Num1 = Number;
  201. Number = 0;
  202. }
  203. else if (Y > 225 && Y < 270) {
  204. Serial.println("Multiplication");
  205. action = 2;
  206. tft.println('*');
  207. Num1 = Number;
  208. Number = 0;
  209. }
  210. else if (Y > 120 && Y < 200) {
  211. Serial.println("Division");
  212. action = 3;
  213. tft.println('/');
  214. Num1 = Number;
  215. Number = 0;
  216. }
  217. }
  218. X = Y = -1;
  219. delay(300);
  220. }
  221.  
  222. void CalculateResult() {
  223. if (action == -1)
  224. Num1 = Number;
  225. Num2 = Number;
  226. if (action == -1) {
  227. Number = Num1;
  228. Num2 = 0;
  229. }
  230. else if (action == 0) {
  231. Number = Num1 + Num2;
  232. Num1 = Number;
  233. Num2 = 0;
  234. }
  235. else if (action == 1) {
  236. Number = Num1 - Num2;
  237. Num1 = Number;
  238. Num2 = 0;
  239. }
  240. else if (action == 2) {
  241. Number = Num1 * Num2;
  242. Num1 = Number;
  243. Num2 = 0;
  244. }
  245. else if (action == 3) {
  246. if(Num2 == 0){
  247. tft.println("Invalid");
  248. delay(1800);
  249. }
  250. else{
  251. Number = Num1 / Num2;
  252. }
  253. Num1 = Number;
  254. Num2 = 0;
  255. }
  256. action = -1;
  257. }
  258.  
  259. void DisplayResult() {
  260. tft.fillRect(0, 0, 240, 80, CYAN); //clear result box
  261. tft.setCursor(10, 20);
  262. tft.setTextSize(4);
  263. tft.setTextColor(BLACK);
  264. tft.println(Number); //update new value
  265. }
  266.  
  267. void IntroScreen() {
  268. tft.setCursor(55, 120);
  269. tft.setTextSize(3);
  270. tft.setTextColor(RED);
  271. tft.println("ARDUINO");
  272. tft.setCursor(30, 160);
  273. tft.println("CALCULATOR");
  274. tft.setCursor(30, 220);
  275. tft.setTextSize(2);
  276. tft.setTextColor(BLUE);
  277. tft.println(" CSED 19");
  278. delay(1800);
  279. }
  280.  
  281. void draw_BoxNButtons() {
  282. //Draw the Result Box
  283. tft.fillRect(0, 0, 240, 80, CYAN);
  284.  
  285. //Draw First Column
  286. tft.fillRect(0, 260, 60, 60, RED);
  287. tft.fillRect(0, 200, 60, 60, BLACK);
  288. tft.fillRect(0, 140, 60, 60, BLACK);
  289. tft.fillRect(0, 80, 60, 60, BLACK);
  290.  
  291. //Draw Third Column
  292. tft.fillRect(120, 260, 60, 60, GREEN);
  293. tft.fillRect(120, 200, 60, 60, BLACK);
  294. tft.fillRect(120, 140, 60, 60, BLACK);
  295. tft.fillRect(120, 80, 60, 60, BLACK);
  296.  
  297. //Draw Secound & Fourth Column
  298. for (int b = 260; b >= 80; b -= 60) {
  299. tft.fillRect(180, b, 60, 60, BLUE);
  300. tft.fillRect(60, b, 60, 60, BLACK);
  301. }
  302.  
  303. //Draw Horizontal Lines
  304. for (int h = 80; h <= 320; h += 60)
  305. tft.drawFastHLine(0, h, 240, WHITE);
  306.  
  307. //Draw Vertical Lines
  308. for (int v = 0; v <= 240; v += 60)
  309. tft.drawFastVLine(v, 80, 240, WHITE);
  310.  
  311. //Display keypad lables
  312. for (int j = 0; j < 4; j++) {
  313. for (int i = 0; i < 4; i++) {
  314. tft.setCursor(22 + (60 * i), 100 + (60 * j));
  315. tft.setTextSize(3);
  316. tft.setTextColor(WHITE);
  317. tft.println(symbol[j][i]);
  318. }
  319. }
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement