Advertisement
Merevoli

Untitled

May 21st, 2022
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.57 KB | None | 0 0
  1. #include "Keypad.h" // khai bao thư vien keypad
  2. #include "Servo.h"
  3. #include "Adafruit_GFX.h" // khai bao thư vien Adafruit graphics library
  4. #include "Adafruit_ILI9341.h" // khai bao thư vien Adafruit ILI9341 TFT library
  5.  
  6. #define TFT_CS 8 // khai bao chan cs
  7. #define TFT_RST 9 // khai bao chan reset
  8. #define TFT_DC 53 // khai bao chan dc
  9.  
  10. // -------------------------LCD Setup-----------------------------------------
  11. // initialize ILI9341 TFT library
  12. Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
  13.  
  14. // -------------------------Keypad Setup---------------------------------------
  15. const byte ROWS = 4; // Four Rows
  16. const byte COLS = 4; // Four Columns
  17. char keys[ROWS][COLS] = {
  18. {'7','8','9','A'},
  19. {'4','5','6','B'},
  20. {'1','2','3','C'},
  21. {'*','0','#','D'}
  22. };
  23. const int MOTOR_PIN_SIZE = 12;
  24. const int MOTOR_PINS[13] = {-1, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33};
  25. int MOTOR_PIN_STATUS[13] = {-1, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
  26.  
  27. Servo myservo;
  28. int degree = 30;
  29.  
  30. String key_tittle_mapping[16][2] = {
  31. {"7", "START"},
  32. {"8", "RESET"},
  33. {"9", "<"},
  34. {"A", ">"},
  35. {"4", "BACK RISE"},
  36. {"5", "BACK DOWN"},
  37. {"6", "^"},
  38. {"B", "v"},
  39. {"1", "SIT UP"},
  40. {"2", "LEG DOWN"},
  41. {"3", "LEFT TURN"},
  42. {"C", "RIGHT TURN"},
  43. {"*", "BEDPAN ON"},
  44. {"0", "BEDPAN OFF"},
  45. {"#", "AUTO TURN A"},
  46. {"D", "AUTO TURN B"},
  47. };
  48.  
  49. byte rowPins[ROWS] = {A8, A9, A10, A11}; // Arduino pins connected to the row pins of the keypad
  50. byte colPins[COLS] = {A12, A13, A14, A15}; // Arduino pins connected to the column pins of the keypad
  51. Keypad keypad_key = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); // Keypad Library definition
  52. int DOWN_MOVES[8][2] = {
  53. {194, 80},
  54. {194, 83},
  55. {194, 86},
  56. {195, 89},
  57. {196, 92},
  58. {197, 95},
  59. {199, 98},
  60. {200, 100}
  61. };
  62.  
  63. int TOP_MOVES[8][2] = {
  64. {306, 80},
  65. {306, 77},
  66. {306, 73},
  67. {305, 70},
  68. {304, 68},
  69. {303, 66},
  70. {302, 63},
  71. {300, 60}
  72. };
  73.  
  74. int current_top_pos = 0;
  75. int current_down_pos = 0;
  76.  
  77.  
  78. int find_str(String str, char find_chr){
  79. for (int pos=0; pos<str.length(); pos++){
  80. if (str[pos] == find_chr){
  81. return pos;
  82. }
  83. }
  84. return -1;
  85. }
  86.  
  87. String get_key_tittle_mapping(char key){
  88. String str_key = String(key);
  89.  
  90. for (int i=0; i<ROWS * COLS; i++){
  91. if (key_tittle_mapping[i][0] == str_key){
  92. return key_tittle_mapping[i][1];
  93. }
  94. }
  95. return "";
  96. }
  97.  
  98. void get_pos(char key, int &pos_x, int &pos_y) { // get (x, y)
  99. for (int col=1; col <= COLS; col++){
  100. for (int row=1; row <= ROWS; row++){
  101. if (keys[row - 1][col - 1] == key){
  102. pos_x = col;
  103. pos_y = row;
  104. return;
  105. }
  106. }
  107. }
  108.  
  109. pos_x = 0;
  110. pos_y = 0;
  111. }
  112.  
  113. void draw_button(String tittle, int pos_x, int pos_y, int color = 0){
  114. pos_x = 15 + (pos_x - 1) * 45;
  115. pos_y = 40 + (pos_y - 1) * 45;
  116.  
  117. uint16_t button_color;
  118. if (color == 0) { // green
  119. button_color = tft.color565(64, 237, 39);
  120. }
  121. else { // red
  122. button_color = tft.color565(238, 38, 91);
  123. }
  124. int pos_first_space = find_str(tittle, ' ');
  125. if (pos_first_space != -1) {
  126. String word_up = tittle.substring(0, pos_first_space);
  127. String word_down = tittle.substring(pos_first_space + 1);
  128.  
  129. tft.fillRect(pos_x, pos_y, 40, 40, button_color);
  130. tft.setCursor(pos_x + 3, pos_y + 10);
  131. tft.setTextColor(tft.color565(0, 0, 0));
  132. tft.setTextSize(1);
  133. tft.println(word_up);
  134.  
  135. tft.setCursor(pos_x + 3, pos_y + 20);
  136. tft.setTextColor(tft.color565(0, 0, 0));
  137. tft.setTextSize(1);
  138. tft.println(word_down);
  139. }
  140. else {
  141. int text_size = 1;
  142.  
  143. if (tittle == ">" || tittle == "<" || tittle == "v") {
  144. text_size = 3;
  145. }
  146. else if (tittle == "^"){
  147. text_size = 4;
  148. }
  149. tft.fillRect(pos_x, pos_y, 40, 40, button_color);
  150. tft.setCursor(pos_x + 5, pos_y + 15);
  151. tft.setTextColor(tft.color565(0, 0, 0));
  152. tft.setTextSize(text_size);
  153. tft.println(tittle);
  154. }
  155. }
  156.  
  157. void poweron_screen() {
  158. tft.fillRect(0, 0, 320, 240, tft.color565(200,200, 200));
  159. tft.fillRoundRect(20, 20, 280, 200, 20, tft.color565(243,87, 35));
  160. tft.setCursor(90, 90);
  161. tft.setTextColor(tft.color565(255, 255, 255));
  162. tft.setTextSize(8);
  163. tft.println("M2D ");
  164. }
  165.  
  166. void startup_screen() {
  167. tft.fillRect(0, 0, 320, 240, tft.color565(164,255, 255));
  168. tft.setCursor(50, 80);
  169. tft.setTextColor(tft.color565(0, 164, 164));
  170. tft.setTextSize(3);
  171. tft.println("GIUONG HO TRO ");
  172. tft.setCursor(40, 110);
  173. tft.setTextColor(tft.color565(243, 87, 35));
  174. tft.setTextSize(3);
  175. tft.println("NGUOI TAI BIEN");
  176. }
  177.  
  178. void init_screen(){
  179. tft.fillScreen(tft.color565(0, 0, 0));
  180.  
  181. tft.fillRect(10, 10, 300, 220, tft.color565(116, 139, 248));
  182. tft.setCursor(20, 20);
  183. tft.setTextColor(tft.color565(159, 30, 6));
  184. tft.setTextSize(2);
  185. tft.println("MAIN CONTROL");
  186.  
  187. tft.drawLine(230, 80, 194, 80, ILI9341_WHITE);
  188. tft.drawLine(230, 80, 270, 80, ILI9341_WHITE);
  189. tft.drawLine(270, 80, 206, 80, ILI9341_WHITE);
  190. }
  191.  
  192. void reset_screen(){
  193. for (int col=1; col <= COLS; col++){
  194. for (int row=1; row <= ROWS; row++){
  195. String tittle = get_key_tittle_mapping(keys[row - 1][col - 1]);
  196. draw_button(tittle, col, row, 0);
  197. }
  198. }
  199. }
  200.  
  201. void delay_with_animation(int delay_time){
  202. unsigned long start_mili = millis();
  203.  
  204. while (true){
  205. unsigned long current_mili = millis();
  206. if (current_mili - start_mili >= delay_time * 1000) {
  207. break;
  208. }
  209.  
  210. if (start_mili % 200){
  211. handle_bed_animation();
  212. }
  213. }
  214.  
  215. }
  216.  
  217. void handle_motor_step(
  218. String type,
  219. int data_1 = 0,
  220. int data_2 = 0,
  221. int data_3 = 0,
  222. int data_4 = 0,
  223. int data_5 = 0,
  224. int data_6 = 0,
  225. int data_7 = 0,
  226. int data_8 = 0,
  227. int data_9 = 0,
  228. int data_10 = 0,
  229. int data_11 = 0,
  230. int data_12 = 0
  231. ){
  232. if (type == "pin") {
  233. int signal[12] = {data_1, data_2, data_3, data_4, data_5, data_6, data_7, data_8, data_9, data_10, data_11, data_12};
  234.  
  235. for (int pin_no = 1; pin_no <= MOTOR_PIN_SIZE; pin_no++){
  236. if (signal[pin_no - 1] != -1){
  237. digitalWrite(MOTOR_PINS[pin_no], signal[pin_no - 1]);
  238. MOTOR_PIN_STATUS[pin_no] = signal[pin_no - 1];
  239. }
  240. }
  241. }
  242. else if (type == "pin_reset_high") {
  243. for (int pin_no = 1; pin_no <= MOTOR_PIN_SIZE; pin_no++){
  244. digitalWrite(MOTOR_PINS[pin_no], HIGH);
  245. MOTOR_PIN_STATUS[pin_no] = HIGH;
  246. }
  247. }
  248. else if (type == "delay") {
  249. int delay_time = data_1*1000;
  250. delay_with_animation(delay_time);
  251. }
  252. else if (type == "degree_add") {
  253. int degree_add_value = data_1;
  254. degree += degree_add_value;
  255. }
  256. else if (type == "degree") {
  257. int degree_new_value = data_1;
  258. degree = degree_new_value;
  259. }
  260. }
  261.  
  262.  
  263. void handle_motor(char key) {
  264. switch (key)
  265. {
  266. case '8': // RESET
  267. handle_motor_step("pin", LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, HIGH, HIGH);
  268. handle_motor_step("delay", 4);
  269. handle_motor_step("pin_reset_high");
  270. break;
  271.  
  272. case '9': // <
  273. handle_motor_step("degree", min(60, degree + 10));
  274. break;
  275.  
  276. case 'A': // >
  277. handle_motor_step("degree", max(0, degree - 10));
  278. break;
  279.  
  280. case '6': // ^
  281. handle_motor_step("pin", -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, HIGH, LOW);
  282. handle_motor_step("delay", 3);
  283. handle_motor_step("pin_reset_high");
  284. break;
  285.  
  286. case 'B': // v
  287. handle_motor_step("pin", -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, LOW, HIGH);
  288. handle_motor_step("delay", 3);
  289. handle_motor_step("pin_reset_high");
  290. break;
  291.  
  292. case '4': // BACK RISE
  293. handle_motor_step("pin", HIGH, LOW, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
  294. handle_motor_step("delay", 4);
  295. handle_motor_step("pin_reset_high");
  296. break;
  297.  
  298. case '5': // BACK DOWN
  299. handle_motor_step("pin", LOW, HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
  300. handle_motor_step("delay", 4);
  301. handle_motor_step("pin_reset_high");
  302. break;
  303.  
  304. case '1': // SIT UP
  305. handle_motor_step("pin", HIGH, LOW, HIGH, LOW, -1, -1, -1, -1, -1, -1, -1, -1);
  306. handle_motor_step("delay", 4);
  307. handle_motor_step("pin_reset_high");
  308. break;
  309.  
  310. case '2': // LEG DOWN
  311. handle_motor_step("pin", -1, -1, HIGH, LOW, -1, -1, -1, -1, -1, -1, -1, -1);
  312. handle_motor_step("delay", 4);
  313. handle_motor_step("pin_reset_high");
  314. break;
  315.  
  316. case 'C': // RIGHT TURN
  317. handle_motor_step("pin", -1, -1, -1, -1, -1, -1, HIGH, LOW, -1, -1, -1, -1);
  318. handle_motor_step("delay", 4);
  319. handle_motor_step("pin_reset_high");
  320. break;
  321.  
  322. case '3': // LEFT TURN
  323. handle_motor_step("pin", -1, -1, -1, -1, HIGH, LOW, -1, -1, -1, -1, -1, -1);
  324. handle_motor_step("delay", 4);
  325. handle_motor_step("pin_reset_high");
  326. break;
  327. case '#': // AUTO TURN A
  328. handle_motor_step("pin", -1, -1, -1, -1, HIGH, LOW, -1, -1, -1, -1, -1, -1);
  329. handle_motor_step("delay", 4);
  330. handle_motor_step("pin_reset_high");
  331. handle_motor_step("delay", 5);
  332. handle_motor_step("pin", -1, -1, -1, -1, LOW, HIGH, -1, -1, -1, -1, -1, -1);
  333. handle_motor_step("delay", 4);
  334. handle_motor_step("pin_reset_high");
  335. handle_motor_step("pin", -1, -1, -1, -1, -1, -1, HIGH, LOW, -1, -1, -1, -1);
  336. handle_motor_step("delay", 4);
  337. handle_motor_step("pin_reset_high");
  338. handle_motor_step("delay", 5);
  339. handle_motor_step("pin", -1, -1, -1, -1, -1, -1, LOW, HIGH, -1, -1, -1, -1);
  340. handle_motor_step("delay", 4);
  341. handle_motor_step("pin_reset_high");
  342. break;
  343. case 'D': // AUTO TURN B
  344. handle_motor_step("pin", HIGH, LOW, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
  345. handle_motor_step("delay", 4);
  346. handle_motor_step("pin_reset_high");
  347. handle_motor_step("delay", 5);
  348. handle_motor_step("pin", LOW, HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
  349. handle_motor_step("delay", 4);
  350. handle_motor_step("pin_reset_high");
  351. handle_motor_step("delay", 5);
  352. handle_motor_step("pin_reset_high");
  353. break;
  354. case '*': // BEDPAN ON
  355. handle_motor_step("pin", -1, -1, -1, -1, -1, -1, -1, -1, HIGH, LOW, -1, -1);
  356. handle_motor_step("delay", 4);
  357. handle_motor_step("pin_reset_high");
  358. break;
  359. case '0': // BEDPAN OFF
  360. handle_motor_step("pin", -1, -1, -1, -1, -1, -1, -1, -1, LOW, HIGH, -1, -1);
  361. handle_motor_step("delay", 4);
  362. handle_motor_step("pin_reset_high");
  363. break;
  364. default:
  365. break;
  366. }
  367. }
  368.  
  369. void make_red_start_button(){
  370. char key = '7';
  371. int pos_x = 0;
  372. int pos_y = 0;
  373. get_pos(key, pos_x, pos_y);
  374. String tittle = get_key_tittle_mapping(key);
  375. draw_button(tittle, pos_x, pos_y, 1);
  376. }
  377.  
  378. void handle_bed_animation(){
  379. tft.fillRect(194, 10, 300, 220, tft.color565(116, 139, 248));
  380. Serial.println("bed animation");
  381.  
  382. if (MOTOR_PIN_STATUS[1] != MOTOR_PIN_STATUS[2]){
  383. if (MOTOR_PIN_STATUS[1] = LOW){
  384. current_top_pos = max(0, current_top_pos - 1);
  385. }
  386. else{
  387. current_top_pos = min(7, current_top_pos + 1);
  388. }
  389. }
  390.  
  391. if (MOTOR_PIN_STATUS[3] != MOTOR_PIN_STATUS[4]){
  392. if (MOTOR_PIN_STATUS[3] = LOW){
  393. current_down_pos = max(0, current_down_pos - 1);
  394. }
  395. else{
  396. current_down_pos = min(7, current_down_pos + 1);
  397. }
  398. }
  399.  
  400. tft.drawLine(230, 80, DOWN_MOVES[current_down_pos][0], DOWN_MOVES[current_down_pos][1], ILI9341_WHITE);
  401. tft.drawLine(230, 80, 270, 80, ILI9341_WHITE);
  402. tft.drawLine(270, 80, TOP_MOVES[current_top_pos][0], TOP_MOVES[current_top_pos][1], ILI9341_WHITE);
  403. }
  404.  
  405. void setup() {
  406. Serial.begin(9600);
  407. Serial.println("start");
  408.  
  409. for (int pin_no = 1; pin_no <= MOTOR_PIN_SIZE; pin_no++){
  410. pinMode(MOTOR_PINS[pin_no], OUTPUT);
  411. }
  412.  
  413. tft.begin();
  414. tft.setRotation(3);
  415. bool is_init_screen = false;
  416. poweron_screen();
  417.  
  418. while (!is_init_screen){
  419. char key = keypad_key.waitForKey();
  420. if (key != NO_KEY){
  421. if (key == '7'){
  422. startup_screen();
  423. delay_with_animation(1000);
  424. init_screen();
  425. is_init_screen = true;
  426. int pos_x = 0;
  427. int pos_y = 0;
  428. get_pos(key, pos_x, pos_y);
  429. String tittle = get_key_tittle_mapping(key);
  430. reset_screen();
  431. draw_button(tittle, pos_x, pos_y, 1);
  432. }
  433. }
  434.  
  435. myservo.attach(11, 500, 2500);
  436. }
  437. }
  438.  
  439. void loop() {
  440. char key = keypad_key.getKey();
  441.  
  442. if (key != NO_KEY){
  443. int pos_x = 0;
  444. int pos_y = 0;
  445. get_pos(key, pos_x, pos_y);
  446. String tittle = get_key_tittle_mapping(key);
  447. reset_screen();
  448. make_red_start_button();
  449. draw_button(tittle, pos_x, pos_y, 1);
  450. handle_motor(key);
  451. } myservo.write(degree);
  452.  
  453. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement