Advertisement
Merevoli

Untitled

May 21st, 2022
794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.99 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.  
  26. Servo myservo;
  27. int degree = 0;
  28.  
  29. String key_tittle_mapping[16][2] = {
  30.     {"7", "START"},
  31.     {"8", "RESET"},
  32.     {"9", "<"},
  33.     {"A", ">"},
  34.     {"4", "BACK RISE"},
  35.     {"5", "BACK DOWN"},
  36.     {"6", "^"},
  37.     {"B", "v"},
  38.     {"1", "SIT UP"},
  39.     {"2", "LEG DOWN"},
  40.     {"3", "LEFT TURN"},
  41.     {"C", "RIGHT TURN"},
  42.     {"*", "BEDPAN ON"},
  43.     {"0", "BEDPAN OFF"},
  44.     {"#", "AUTO TURN A"},
  45.     {"D", "AUTO TURN B"},
  46. };
  47.  
  48. byte rowPins[ROWS] = {A8, A9, A10, A11}; // Arduino pins connected to the row pins of the keypad
  49. byte colPins[COLS] = {A12, A13, A14, A15}; // Arduino pins connected to the column pins of the keypad
  50. Keypad keypad_key = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); // Keypad Library definition
  51.  
  52. int find_str(String str, char find_chr){
  53.     for (int pos=0; pos<str.length(); pos++){
  54.         if (str[pos] == find_chr){
  55.             return pos;
  56.         }
  57.     }
  58.     return -1;
  59. }
  60.  
  61. String get_key_tittle_mapping(char key){
  62.     String str_key = String(key);
  63.  
  64.     for (int i=0; i<ROWS * COLS; i++){
  65.         if (key_tittle_mapping[i][0] == str_key){
  66.             return key_tittle_mapping[i][1];
  67.         }
  68.     }
  69.     return "";
  70. }
  71.  
  72. void get_pos(char key, int &pos_x, int &pos_y) {  // get (x, y)
  73.     for (int col=1; col <= COLS; col++){
  74.         for (int row=1; row <= ROWS; row++){
  75.             if (keys[row - 1][col - 1] == key){
  76.                 pos_x = col;
  77.                 pos_y = row;
  78.                 return;
  79.             }
  80.         }
  81.     }
  82.    
  83.     pos_x = 0;
  84.     pos_y = 0;
  85. }
  86.  
  87. void draw_button(String tittle, int pos_x, int pos_y, int color = 0){
  88.     pos_x = 15 + (pos_x - 1) * 45;
  89.     pos_y = 40 + (pos_y - 1) * 45;
  90.  
  91.     uint16_t button_color;
  92.     if (color == 0) { // green
  93.         button_color = tft.color565(64, 237, 39);
  94.     }
  95.     else { // red
  96.         button_color = tft.color565(238, 38, 91);
  97.     }
  98.     int pos_first_space = find_str(tittle, ' ');
  99.     if (pos_first_space != -1) {
  100.         String word_up = tittle.substring(0, pos_first_space);
  101.         String word_down = tittle.substring(pos_first_space + 1);
  102.  
  103.         tft.fillRect(pos_x, pos_y, 40, 40, button_color);
  104.         tft.setCursor(pos_x + 3, pos_y + 10);
  105.         tft.setTextColor(tft.color565(0, 0, 0));
  106.         tft.setTextSize(1);
  107.         tft.println(word_up);
  108.  
  109.         tft.setCursor(pos_x + 3, pos_y + 20);
  110.         tft.setTextColor(tft.color565(0, 0, 0));
  111.         tft.setTextSize(1);
  112.         tft.println(word_down);
  113.     }
  114.     else {
  115.         int text_size = 1;
  116.  
  117.         if (tittle == ">" || tittle == "<" || tittle == "v") {
  118.             text_size = 3;
  119.         }
  120.         else if (tittle == "^"){
  121.             text_size = 4;
  122.         }
  123.         tft.fillRect(pos_x, pos_y, 40, 40, button_color);
  124.         tft.setCursor(pos_x + 5, pos_y + 15);
  125.         tft.setTextColor(tft.color565(0, 0, 0));
  126.         tft.setTextSize(text_size);
  127.         tft.println(tittle);
  128.     }
  129. }
  130.  
  131. void poweron_screen() {
  132.     tft.fillRect(0, 0, 320, 240, tft.color565(200,200, 200));
  133.     tft.fillRoundRect(20, 20, 280, 200, 20, tft.color565(243,87, 35));
  134.     tft.setCursor(90, 90);
  135.     tft.setTextColor(tft.color565(255, 255, 255));
  136.     tft.setTextSize(8);
  137.     tft.println("M2D ");
  138. }
  139.  
  140. void startup_screen() {
  141.     tft.fillRect(0, 0, 320, 240, tft.color565(164,255, 255));
  142.     tft.setCursor(50, 80);
  143.     tft.setTextColor(tft.color565(0, 164, 164));
  144.     tft.setTextSize(3);
  145.     tft.println("GIUONG HO TRO ");
  146.     tft.setCursor(40, 110);
  147.     tft.setTextColor(tft.color565(243, 87, 35));
  148.     tft.setTextSize(3);
  149.     tft.println("NGUOI TAI BIEN");
  150. }
  151.  
  152. void init_screen(){
  153.     tft.fillScreen(tft.color565(0, 0, 0));
  154.  
  155.     tft.fillRect(10, 10, 300, 220, tft.color565(116, 139, 248));
  156.     tft.setCursor(20, 20);
  157.     tft.setTextColor(tft.color565(159, 30, 6));
  158.     tft.setTextSize(2);
  159.     tft.println("MAIN CONTROL");
  160.    
  161.     tft.drawLine(230, 130, 200, 150, ILI9341_WHITE);
  162.     tft.drawLine(230, 130, 270, 130, ILI9341_WHITE);
  163.     tft.drawLine(270, 130, 300, 100, ILI9341_WHITE);
  164. }
  165.  
  166. void reset_screen(){
  167.     for (int col=1; col <= COLS; col++){
  168.         for (int row=1; row <= ROWS; row++){
  169.             String tittle = get_key_tittle_mapping(keys[row - 1][col - 1]);
  170.             draw_button(tittle, col, row, 0);
  171.         }
  172.     }
  173. }
  174.  
  175. void handle_motor_step(
  176.     String type,
  177.     int data_1 = 0,
  178.     int data_2 = 0,
  179.     int data_3 = 0,
  180.     int data_4 = 0,
  181.     int data_5 = 0,
  182.     int data_6 = 0,
  183.     int data_7 = 0,
  184.     int data_8 = 0,
  185.     int data_9 = 0,
  186.     int data_10 = 0,
  187.     int data_11 = 0,
  188.     int data_12 = 0
  189. ){
  190.     if (type == "pin") {
  191.         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};
  192.  
  193.         for (int pin_no = 1; pin_no <= MOTOR_PIN_SIZE; pin_no++){
  194.             if (signal[pin_no - 1] != -1){
  195.                 digitalWrite(MOTOR_PINS[pin_no], signal[pin_no - 1]);
  196.             }
  197.         }
  198.     }
  199.     else if (type == "pin_reset_high") {
  200.         for (int pin_no = 1; pin_no <= MOTOR_PIN_SIZE; pin_no++){
  201.             digitalWrite(MOTOR_PINS[pin_no], HIGH);
  202.         }
  203.     }
  204.     else if (type == "delay") {
  205.         int delay_time = data_1;
  206.         delay(delay_time);
  207.     }
  208.     else if (type == "degree_add") {
  209.         int degree_add_value = data_1;
  210.         degree += degree_add_value;
  211.     }
  212.     else if (type == "degree") {
  213.         int degree_new_value = data_1;
  214.         degree = degree_new_value;
  215.     }
  216. }
  217.  
  218.  
  219. void handle_motor(char key) {
  220.     switch (key)
  221.     {
  222.     case '8': // RESET
  223.         handle_motor_step("pin", LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, HIGH, HIGH);
  224.         handle_motor_step("delay", 4);
  225.         handle_motor_step("pin_reset_high");
  226.         break;
  227.    
  228.     case '9': // <
  229.         handle_motor_step("degree_add", min(30, degree + 10));
  230.         break;
  231.    
  232.     case 'A': // >
  233.         handle_motor_step("degree_add", min(-30, degree - 10));
  234.         break;
  235.  
  236.     case '6': // ^
  237.         handle_motor_step("pin", -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, HIGH, LOW);
  238.         handle_motor_step("delay", 3);
  239.         handle_motor_step("pin_reset_high");
  240.         break;
  241.  
  242.     case 'B': // v
  243.         handle_motor_step("pin", -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, LOW, HIGH);
  244.         handle_motor_step("delay", 3);
  245.         handle_motor_step("pin_reset_high");
  246.         break;
  247.  
  248.     case '4': // BACK RISE
  249.         handle_motor_step("pin", HIGH, LOW, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
  250.         handle_motor_step("delay", 4);
  251.         handle_motor_step("pin_reset_high");
  252.         break;
  253.  
  254.     case '5': // BACK DOWN
  255.         handle_motor_step("pin", LOW, HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
  256.         handle_motor_step("delay", 4);
  257.         handle_motor_step("pin_reset_high");
  258.         break;
  259.  
  260.     case '1': // SIT UP
  261.         handle_motor_step("pin", HIGH, LOW, HIGH, LOW, -1, -1, -1, -1, -1, -1, -1, -1);
  262.         handle_motor_step("delay", 4);
  263.         handle_motor_step("pin_reset_high");
  264.         break;
  265.  
  266.     case '2': // LEG DOWN
  267.         handle_motor_step("pin", -1, -1, HIGH, LOW, -1, -1, -1, -1, -1, -1, -1, -1);
  268.         handle_motor_step("delay", 4);
  269.         handle_motor_step("pin_reset_high");
  270.         break;
  271.  
  272.     case 'C': // RIGHT TURN
  273.         handle_motor_step("pin", -1, -1, -1, -1, -1, -1, HIGH, LOW, -1, -1, -1, -1);
  274.         handle_motor_step("delay", 4);
  275.         handle_motor_step("pin_reset_high");
  276.         break;
  277.  
  278.     case '3': // LEFT TURN
  279.         handle_motor_step("pin", -1, -1, -1, -1, HIGH, LOW, -1, -1, -1, -1, -1, -1);
  280.         handle_motor_step("delay", 4);
  281.         handle_motor_step("pin_reset_high");
  282.  
  283.     case '#': // AUTO TURN A
  284.         handle_motor_step("pin", -1, -1, -1, -1, HIGH, LOW, -1, -1, -1, -1, -1, -1);
  285.         handle_motor_step("delay", 5);
  286.         handle_motor_step("pin", -1, -1, -1, -1, -1, -1, HIGH, LOW, -1, -1, -1, -1);
  287.         handle_motor_step("delay", 5);
  288.         handle_motor_step("pin_reset_high");
  289.        
  290.     case 'D': // AUTO TURN B
  291.         handle_motor_step("pin", HIGH, LOW, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
  292.         handle_motor_step("delay", 5);
  293.         handle_motor_step("pin", LOW, HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
  294.         handle_motor_step("delay", 5);
  295.         handle_motor_step("pin_reset_high");
  296.  
  297.     case '*': // BEDPAN ON
  298.         handle_motor_step("pin", -1, -1, -1, -1, -1, -1, -1, -1, HIGH, LOW, -1, -1);
  299.         handle_motor_step("delay", 4);
  300.         handle_motor_step("pin_reset_high");
  301.  
  302.     case '0': // BEDPAN OFF
  303.         handle_motor_step("pin", -1, -1, -1, -1, -1, -1, -1, -1, LOW, HIGH, -1, -1);
  304.         handle_motor_step("delay", 4);
  305.         handle_motor_step("pin_reset_high");
  306.     default:
  307.         break;
  308.     }
  309. }
  310.  
  311. void make_red_start_button(){
  312.     char key = '7';
  313.     int pos_x = 0;
  314.     int pos_y = 0;
  315.     get_pos(key, pos_x, pos_y);
  316.     String tittle = get_key_tittle_mapping(key);
  317.     draw_button(tittle, pos_x, pos_y, 1);
  318. }
  319.  
  320. void setup() {
  321.     for (int pin_no = 1; pin_no <= MOTOR_PIN_SIZE; pin_no++){
  322.         pinMode(MOTOR_PINS[pin_no], OUTPUT);
  323.     }
  324.  
  325.     tft.begin();
  326.     tft.setRotation(3);
  327.     bool is_init_screen = false;
  328.     poweron_screen();
  329.  
  330.     while (!is_init_screen){
  331.         char key = keypad_key.waitForKey();
  332.         if (key != NO_KEY){
  333.             if (key == '7'){
  334.                 startup_screen();
  335.                 delay(1000);
  336.                 init_screen();
  337.                 is_init_screen = true;
  338.                 int pos_x = 0;
  339.                 int pos_y = 0;
  340.                 get_pos(key, pos_x, pos_y);
  341.                 String tittle = get_key_tittle_mapping(key);
  342.                 reset_screen();
  343.                 draw_button(tittle, pos_x, pos_y, 1);
  344.             }
  345.         }
  346.        
  347.         myservo.attach(11, 500, 2500);
  348.     }
  349. }
  350.  
  351. void loop() {
  352.     char key = keypad_key.waitForKey();
  353.  
  354.     if (key != NO_KEY){
  355.         int pos_x = 0;
  356.         int pos_y = 0;
  357.         get_pos(key, pos_x, pos_y);
  358.         String tittle = get_key_tittle_mapping(key);
  359.         reset_screen();
  360.         make_red_start_button();
  361.         draw_button(tittle, pos_x, pos_y, 1);
  362.  
  363.         myservo.write(degree);
  364.         handle_motor(key);
  365.     }
  366. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement