/*
Pinler
9, 8, 7, 6, 5, 4, 3, 2 Keypad Pinleri

20, 21 LCD Pinleri

53 Motor Pini

İptal etmek istediğiniz tuş için bir önceki pine 5v verin.
Örneğin 22. pine 5v verirseniz hasta yukarı işlemi iptal olur.

23 Hasta Yukarı 
25 Hasta Aşağı
27 Hasta Baş Yukarı
29 Hasta Baş Aşağı
31 Hasta Sırt Yukarı
33 Hasta Sırt Aşağı
35 Hasta Ayak Yukarı
37 Hasta Ayak Aşağı
39 Hasta Lateral Sağ
41 Hasta Lateral Sol
43 Ayak Aşağı Baş Yukarı
45 Ayak Yukarı Baş Aşağı
47 Tredenburg ileri
49 Tredenburg geri
*/

#include <Keypad.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x27
#define LCD_COLUMNS 16
#define LCD_LINES   2

#define MOTOR_PIN 53

bool onPress = LOW;
bool noPress = HIGH;

LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);

const uint8_t ROWS = 4;
const uint8_t COLS = 4;

uint8_t rowPins[ROWS] = { 9, 8, 7, 6 };
uint8_t colPins[COLS] = { 5, 4, 3, 2 };

char keys[ROWS][COLS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup() {
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.println("Sistem Kapali");

  for (int i = 22; i <= 48; i+=2)
  pinMode(i, INPUT);
  for (int i = 23; i <= 49; i+=2)
  pinMode(i, OUTPUT);
  pinMode(MOTOR_PIN, OUTPUT);

  
  for (int i = 23; i <= 49; i+=2)
  digitalWrite(i, noPress);
  digitalWrite(MOTOR_PIN, noPress);
}

bool isOpen = false;

void _print (String text1, String text2 = "") {
  while (text1.length() < 16) text1 += ' ';
  while (text2.length() < 16) text2 += ' ';

  lcd.setCursor(0, 0);
  lcd.print(text1);
  lcd.setCursor(0, 1);
  lcd.print(text2);
}

void loop() {

  char last_key;
  char key = keypad.getKey();

  if (key == 'A')
  {
    isOpen = !isOpen;
    delay(100);
  }
  else if (key != NO_KEY && isOpen) {
    last_key = key;
    switch (key) {
      case '1':
        if (digitalRead(22)) break;
        _print("Hasta yukari");
        digitalWrite(23, onPress);
        digitalWrite(MOTOR_PIN, onPress);
        break;
      case '2':
        if (digitalRead(24)) break;
        _print("Hasta asagi");
        digitalWrite(25, onPress);
        digitalWrite(MOTOR_PIN, onPress);
        break;
      case '3':
        if (digitalRead(26)) break;
        _print("Hasta bas yukari");
        digitalWrite(27, onPress);
        digitalWrite(MOTOR_PIN, onPress);
        break;
      case '4':
        if (digitalRead(28)) break;
        _print("Hasta bas asagi");
        digitalWrite(29, onPress);
        digitalWrite(MOTOR_PIN, onPress);
        break;
      case '5':
        if (digitalRead(30)) break;
        _print("Hasta sirt", "yukari");
        digitalWrite(31, onPress);
        digitalWrite(MOTOR_PIN, onPress);
        break;
      case '6':
        if (digitalRead(32))
        {
          _print("Hasta sirt", "en asagida");
          break;
        }
        _print("Hasta sirt", "asagi");
        digitalWrite(33, onPress);
        digitalWrite(MOTOR_PIN, onPress);
        break;
      case '7':
        if (digitalRead(34)) break;
        _print("Hasta ayak", "yukari");
        digitalWrite(35, onPress);
        digitalWrite(MOTOR_PIN, onPress);
        break;
      case '8':
        if (digitalRead(36)) break;
        _print("Hasta ayak", "asagi");
        digitalWrite(37, onPress);
        digitalWrite(MOTOR_PIN, onPress);
        break;
      case '9':
        if (digitalRead(38)) break;
        _print("Hasta lateral", "sag");
        digitalWrite(39, onPress);
        digitalWrite(MOTOR_PIN, onPress);
        break;
      case '0':
        if (digitalRead(40)) break;
        _print("Hasta lateral", "sol");
        digitalWrite(41, onPress);
        digitalWrite(MOTOR_PIN, onPress);
        break;
      case 'B':
        if (digitalRead(42)) break;
        _print("Hasta ayak asagi", "bas yukari");
        digitalWrite(43, onPress);
        digitalWrite(MOTOR_PIN, onPress);
        break;
      case 'C':
        if (digitalRead(44)) break;
        _print("Hasta ayak", "yukari bas asagi");
        digitalWrite(45, onPress);
        digitalWrite(MOTOR_PIN, onPress);
        break;
      case 'D':
        if (digitalRead(46)) break;
        _print("Trenderenburg", "ileri");
        digitalWrite(47, onPress);
        digitalWrite(MOTOR_PIN, onPress);
        break;
      case '#':
        if (digitalRead(48)) break;
        _print("Trenderenburg", "geri");
        digitalWrite(49, onPress);
        digitalWrite(MOTOR_PIN, onPress);
        break;
    
    
    
    
    }
    delay(100);
  }
  else if (isOpen && !keypad.getState()) {
    _print("Sistem Acik");
    for (int i = 23; i <= 49; i+=2)
    digitalWrite(i, noPress);
    digitalWrite(MOTOR_PIN, noPress);
  }
  else if (!isOpen) {
    _print("Sistem Kapali");
    for (int i = 23; i <= 49; i+=2)
    digitalWrite(i, noPress);
    digitalWrite(MOTOR_PIN, noPress);
  }
}