Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Keypad.h" // khai bao thư vien keypad
- #include "Servo.h"
- #include "Adafruit_GFX.h" // khai bao thư vien Adafruit graphics library
- #include "Adafruit_ILI9341.h" // khai bao thư vien Adafruit ILI9341 TFT library
- #include "Thread.h"
- #define TFT_CS 8 // khai bao chan cs
- #define TFT_RST 9 // khai bao chan reset
- #define TFT_DC 53 // khai bao chan dc
- // -------------------------LCD Setup-----------------------------------------
- // initialize ILI9341 TFT library
- Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
- // -------------------------Keypad Setup---------------------------------------
- const byte ROWS = 4; // Four Rows
- const byte COLS = 4; // Four Columns
- char keys[ROWS][COLS] = {
- {'7','8','9','A'},
- {'4','5','6','B'},
- {'1','2','3','C'},
- {'*','0','#','D'}
- };
- const int MOTOR_PIN_SIZE = 12;
- const int MOTOR_PINS[13] = {-1, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33};
- int MOTOR_PIN_STATUS[13] = {-1, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
- Servo myservo;
- int degree = 30;
- String key_tittle_mapping[16][2] = {
- {"7", "START"},
- {"8", "RESET"},
- {"9", "<"},
- {"A", ">"},
- {"4", "BACK RISE"},
- {"5", "BACK DOWN"},
- {"6", "^"},
- {"B", "v"},
- {"1", "SIT UP"},
- {"2", "LEG DOWN"},
- {"3", "LEFT TURN"},
- {"C", "RIGHT TURN"},
- {"*", "BEDPAN ON"},
- {"0", "BEDPAN OFF"},
- {"#", "AUTO TURN A"},
- {"D", "AUTO TURN B"},
- };
- byte rowPins[ROWS] = {A8, A9, A10, A11}; // Arduino pins connected to the row pins of the keypad
- byte colPins[COLS] = {A12, A13, A14, A15}; // Arduino pins connected to the column pins of the keypad
- Keypad keypad_key = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); // Keypad Library definition
- Thread bed_animation = Thread();
- int DOWN_MOVES[8][2] = {
- {194, 80},
- {194, 83},
- {194, 86},
- {195, 89},
- {196, 92},
- {197, 95},
- {199, 98},
- {200, 100}
- };
- int TOP_MOVES[8][2] = {
- {306, 80},
- {306, 77},
- {306, 73},
- {305, 70},
- {304, 68},
- {303, 66},
- {302, 63},
- {300, 60}
- };
- int current_top_pos = 0;
- int current_down_pos = 0;
- int find_str(String str, char find_chr){
- for (int pos=0; pos<str.length(); pos++){
- if (str[pos] == find_chr){
- return pos;
- }
- }
- return -1;
- }
- String get_key_tittle_mapping(char key){
- String str_key = String(key);
- for (int i=0; i<ROWS * COLS; i++){
- if (key_tittle_mapping[i][0] == str_key){
- return key_tittle_mapping[i][1];
- }
- }
- return "";
- }
- void get_pos(char key, int &pos_x, int &pos_y) { // get (x, y)
- for (int col=1; col <= COLS; col++){
- for (int row=1; row <= ROWS; row++){
- if (keys[row - 1][col - 1] == key){
- pos_x = col;
- pos_y = row;
- return;
- }
- }
- }
- pos_x = 0;
- pos_y = 0;
- }
- void draw_button(String tittle, int pos_x, int pos_y, int color = 0){
- pos_x = 15 + (pos_x - 1) * 45;
- pos_y = 40 + (pos_y - 1) * 45;
- uint16_t button_color;
- if (color == 0) { // green
- button_color = tft.color565(64, 237, 39);
- }
- else { // red
- button_color = tft.color565(238, 38, 91);
- }
- int pos_first_space = find_str(tittle, ' ');
- if (pos_first_space != -1) {
- String word_up = tittle.substring(0, pos_first_space);
- String word_down = tittle.substring(pos_first_space + 1);
- tft.fillRect(pos_x, pos_y, 40, 40, button_color);
- tft.setCursor(pos_x + 3, pos_y + 10);
- tft.setTextColor(tft.color565(0, 0, 0));
- tft.setTextSize(1);
- tft.println(word_up);
- tft.setCursor(pos_x + 3, pos_y + 20);
- tft.setTextColor(tft.color565(0, 0, 0));
- tft.setTextSize(1);
- tft.println(word_down);
- }
- else {
- int text_size = 1;
- if (tittle == ">" || tittle == "<" || tittle == "v") {
- text_size = 3;
- }
- else if (tittle == "^"){
- text_size = 4;
- }
- tft.fillRect(pos_x, pos_y, 40, 40, button_color);
- tft.setCursor(pos_x + 5, pos_y + 15);
- tft.setTextColor(tft.color565(0, 0, 0));
- tft.setTextSize(text_size);
- tft.println(tittle);
- }
- }
- void poweron_screen() {
- tft.fillRect(0, 0, 320, 240, tft.color565(200,200, 200));
- tft.fillRoundRect(20, 20, 280, 200, 20, tft.color565(243,87, 35));
- tft.setCursor(90, 90);
- tft.setTextColor(tft.color565(255, 255, 255));
- tft.setTextSize(8);
- tft.println("M2D ");
- }
- void startup_screen() {
- tft.fillRect(0, 0, 320, 240, tft.color565(164,255, 255));
- tft.setCursor(50, 80);
- tft.setTextColor(tft.color565(0, 164, 164));
- tft.setTextSize(3);
- tft.println("GIUONG HO TRO ");
- tft.setCursor(40, 110);
- tft.setTextColor(tft.color565(243, 87, 35));
- tft.setTextSize(3);
- tft.println("NGUOI TAI BIEN");
- }
- void init_screen(){
- tft.fillScreen(tft.color565(0, 0, 0));
- tft.fillRect(10, 10, 300, 220, tft.color565(116, 139, 248));
- tft.setCursor(20, 20);
- tft.setTextColor(tft.color565(159, 30, 6));
- tft.setTextSize(2);
- tft.println("MAIN CONTROL");
- tft.drawLine(230, 80, 194, 80, ILI9341_WHITE);
- tft.drawLine(230, 80, 270, 80, ILI9341_WHITE);
- tft.drawLine(270, 80, 106, 80, ILI9341_WHITE);
- }
- void reset_screen(){
- for (int col=1; col <= COLS; col++){
- for (int row=1; row <= ROWS; row++){
- String tittle = get_key_tittle_mapping(keys[row - 1][col - 1]);
- draw_button(tittle, col, row, 0);
- }
- }
- }
- void handle_motor_step(
- String type,
- int data_1 = 0,
- int data_2 = 0,
- int data_3 = 0,
- int data_4 = 0,
- int data_5 = 0,
- int data_6 = 0,
- int data_7 = 0,
- int data_8 = 0,
- int data_9 = 0,
- int data_10 = 0,
- int data_11 = 0,
- int data_12 = 0
- ){
- if (type == "pin") {
- 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};
- for (int pin_no = 1; pin_no <= MOTOR_PIN_SIZE; pin_no++){
- if (signal[pin_no - 1] != -1){
- digitalWrite(MOTOR_PINS[pin_no], signal[pin_no - 1]);
- MOTOR_PIN_STATUS[pin_no] = signal[pin_no - 1];
- }
- }
- }
- else if (type == "pin_reset_high") {
- for (int pin_no = 1; pin_no <= MOTOR_PIN_SIZE; pin_no++){
- digitalWrite(MOTOR_PINS[pin_no], HIGH);
- MOTOR_PIN_STATUS[pin_no] = HIGH;
- }
- }
- else if (type == "delay") {
- int delay_time = data_1*1000;
- delay(delay_time);
- }
- else if (type == "degree_add") {
- int degree_add_value = data_1;
- degree += degree_add_value;
- }
- else if (type == "degree") {
- int degree_new_value = data_1;
- degree = degree_new_value;
- }
- }
- void handle_motor(char key) {
- switch (key)
- {
- case '8': // RESET
- handle_motor_step("pin", LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, HIGH, HIGH);
- handle_motor_step("delay", 4);
- handle_motor_step("pin_reset_high");
- break;
- case '9': // <
- handle_motor_step("degree", min(60, degree + 10));
- break;
- case 'A': // >
- handle_motor_step("degree", max(0, degree - 10));
- break;
- case '6': // ^
- handle_motor_step("pin", -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, HIGH, LOW);
- handle_motor_step("delay", 3);
- handle_motor_step("pin_reset_high");
- break;
- case 'B': // v
- handle_motor_step("pin", -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, LOW, HIGH);
- handle_motor_step("delay", 3);
- handle_motor_step("pin_reset_high");
- break;
- case '4': // BACK RISE
- handle_motor_step("pin", HIGH, LOW, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
- handle_motor_step("delay", 4);
- handle_motor_step("pin_reset_high");
- break;
- case '5': // BACK DOWN
- handle_motor_step("pin", LOW, HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
- handle_motor_step("delay", 4);
- handle_motor_step("pin_reset_high");
- break;
- case '1': // SIT UP
- handle_motor_step("pin", HIGH, LOW, HIGH, LOW, -1, -1, -1, -1, -1, -1, -1, -1);
- handle_motor_step("delay", 4);
- handle_motor_step("pin_reset_high");
- break;
- case '2': // LEG DOWN
- handle_motor_step("pin", -1, -1, HIGH, LOW, -1, -1, -1, -1, -1, -1, -1, -1);
- handle_motor_step("delay", 4);
- handle_motor_step("pin_reset_high");
- break;
- case 'C': // RIGHT TURN
- handle_motor_step("pin", -1, -1, -1, -1, -1, -1, HIGH, LOW, -1, -1, -1, -1);
- handle_motor_step("delay", 4);
- handle_motor_step("pin_reset_high");
- break;
- case '3': // LEFT TURN
- handle_motor_step("pin", -1, -1, -1, -1, HIGH, LOW, -1, -1, -1, -1, -1, -1);
- handle_motor_step("delay", 4);
- handle_motor_step("pin_reset_high");
- break;
- case '#': // AUTO TURN A
- handle_motor_step("pin", -1, -1, -1, -1, HIGH, LOW, -1, -1, -1, -1, -1, -1);
- handle_motor_step("delay", 4);
- handle_motor_step("pin_reset_high");
- handle_motor_step("delay", 5);
- handle_motor_step("pin", -1, -1, -1, -1, LOW, HIGH, -1, -1, -1, -1, -1, -1);
- handle_motor_step("delay", 4);
- handle_motor_step("pin_reset_high");
- handle_motor_step("pin", -1, -1, -1, -1, -1, -1, HIGH, LOW, -1, -1, -1, -1);
- handle_motor_step("delay", 4);
- handle_motor_step("pin_reset_high");
- handle_motor_step("delay", 5);
- handle_motor_step("pin", -1, -1, -1, -1, -1, -1, LOW, HIGH, -1, -1, -1, -1);
- handle_motor_step("delay", 4);
- handle_motor_step("pin_reset_high");
- break;
- case 'D': // AUTO TURN B
- handle_motor_step("pin", HIGH, LOW, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
- handle_motor_step("delay", 4);
- handle_motor_step("pin_reset_high");
- handle_motor_step("delay", 5);
- handle_motor_step("pin", LOW, HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
- handle_motor_step("delay", 4);
- handle_motor_step("pin_reset_high");
- handle_motor_step("delay", 5);
- handle_motor_step("pin_reset_high");
- break;
- case '*': // BEDPAN ON
- handle_motor_step("pin", -1, -1, -1, -1, -1, -1, -1, -1, HIGH, LOW, -1, -1);
- handle_motor_step("delay", 4);
- handle_motor_step("pin_reset_high");
- break;
- case '0': // BEDPAN OFF
- handle_motor_step("pin", -1, -1, -1, -1, -1, -1, -1, -1, LOW, HIGH, -1, -1);
- handle_motor_step("delay", 4);
- handle_motor_step("pin_reset_high");
- break;
- default:
- break;
- }
- }
- void make_red_start_button(){
- char key = '7';
- int pos_x = 0;
- int pos_y = 0;
- get_pos(key, pos_x, pos_y);
- String tittle = get_key_tittle_mapping(key);
- draw_button(tittle, pos_x, pos_y, 1);
- }
- void handle_bed_animation(){
- tft.fillRect(194, 10, 300, 220, tft.color565(116, 139, 248));
- if (MOTOR_PIN_STATUS[1] != MOTOR_PIN_STATUS[2]){
- if (MOTOR_PIN_STATUS[1] = LOW){
- current_top_pos = max(0, current_top_pos - 1);
- }
- else{
- current_top_pos = min(7, current_top_pos + 1);
- }
- }
- if (MOTOR_PIN_STATUS[3] != MOTOR_PIN_STATUS[4]){
- if (MOTOR_PIN_STATUS[3] = LOW){
- current_down_pos = max(0, current_down_pos - 1);
- }
- else{
- current_down_pos = min(7, current_down_pos + 1);
- }
- }
- tft.drawLine(230, 80, DOWN_MOVES[current_down_pos][0], DOWN_MOVES[current_down_pos][1], ILI9341_WHITE);
- tft.drawLine(230, 80, 270, 80, ILI9341_WHITE);
- tft.drawLine(270, 80, TOP_MOVES[current_top_pos][0], TOP_MOVES[current_top_pos][1], ILI9341_WHITE);
- }
- void setup() {
- bed_animation.setInterval(200);
- bed_animation.onRun(handle_bed_animation);
- if (bed_animation.shouldRun()){
- bed_animation.run();
- }
- for (int pin_no = 1; pin_no <= MOTOR_PIN_SIZE; pin_no++){
- pinMode(MOTOR_PINS[pin_no], OUTPUT);
- }
- tft.begin();
- tft.setRotation(3);
- bool is_init_screen = false;
- poweron_screen();
- while (!is_init_screen){
- char key = keypad_key.waitForKey();
- if (key != NO_KEY){
- if (key == '7'){
- startup_screen();
- delay(1000);
- init_screen();
- is_init_screen = true;
- int pos_x = 0;
- int pos_y = 0;
- get_pos(key, pos_x, pos_y);
- String tittle = get_key_tittle_mapping(key);
- reset_screen();
- draw_button(tittle, pos_x, pos_y, 1);
- }
- }
- myservo.attach(11, 500, 2500);
- }
- }
- void loop() {
- char key = keypad_key.getKey();
- if (key != NO_KEY){
- int pos_x = 0;
- int pos_y = 0;
- get_pos(key, pos_x, pos_y);
- String tittle = get_key_tittle_mapping(key);
- reset_screen();
- make_red_start_button();
- draw_button(tittle, pos_x, pos_y, 1);
- handle_motor(key);
- } myservo.write(degree);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement