Skocz do zawartości

Jak połączyć menu na wyświetlaczu z klawiaturą 4x4?


Runner321

Pomocna odpowiedź

Cześć, proszę Was o pomoc jak połączyć ten kod "menu na wyświetlaczu 16x2" z klawiaturą membranową 4x4? 
Początkowo w kodzie "menu" przyciski BACK, NEXT, PREV, OK były realizowane na fizycznych przyciskach. 
Ja chcę zmienić kod tak, aby można było przyciski BACK, NEXT, PREV, OK realizować na przyciskach z klawiatury A, B, C, D, niestety nie wiem jak przerobić ten kod. 

 

Link skąd uzyskałem kod "menu na wyświetlaczu 16x2"

https://starter-kit.nettigo.pl/2017/04/menu-wyswietlaczu-16x2/#comment-402754

 

Kod "menu na wyświetlaczu 16x2"

//---------------------------------------------------------------------------
//CZUJNIK TEMPERATURY
//---------------------------------------------------------------------------
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
#include <Wire.h>


#define LCD_ADDR  0x27

#define BTN_BACK  0  // 0
#define BTN_NEXT  A4  // 1
#define BTN_PREV  8  // 8
#define BTN_OK    9  // 9
#define dioda     A0

typedef struct {
  String label;
  int minVal;
  int maxVal;
  int currentVal;
  void (*handler)();
} STRUCT_MENUPOS;

typedef enum {
  BACK, NEXT, PREV, OK, NONE
} ENUM_BUTTON;

STRUCT_MENUPOS menu[5];

int currentMenuPos = 0;
int menuSize;
bool isInLowerLevel = false;
int tempVal;






/*
#include <Keypad.h> //biblioteka od klawiatury
 
const byte ROWS = 4; // ile wierszy
const byte COLS = 4; //ile kolumn
 
byte rowPins[ROWS] = {0, 1, 8, 9}; //piny wierszy
byte colPins[COLS] = {10, 11, 12, 13}; //piny kolumn


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

Keypad klawiatura = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); //inicjalizacja klawiatury



*/






void setup() {


  Serial.begin(9600);
  lcd.begin(16, 2);

  pinMode(BTN_NEXT, INPUT_PULLUP);
  pinMode(BTN_PREV, INPUT_PULLUP);
  pinMode(BTN_BACK, INPUT_PULLUP);
  pinMode(BTN_OK, INPUT_PULLUP);
  pinMode(dioda, OUTPUT);

  menu[0] = {"cos1", 0, 9, 5, NULL};
  menu[1] = {"cos2", 10, 1000, 15, NULL};
  menu[2] = {"System Alarmowy", 0, 1, 0, formatalarm};
  menu[3] = {"Temperatura", 0, 30, 15, formatTemperatura};
  menu[4] = {"Zapal swiatlo", 0, 1, 0, actiondioda};

  menuSize = sizeof(menu)/sizeof(STRUCT_MENUPOS);
}

void loop() {
  drawMenu();
}

ENUM_BUTTON getButton() {
  if(!digitalRead(BTN_BACK)) return BACK;
  if(!digitalRead(BTN_NEXT)) return NEXT;
  if(!digitalRead(BTN_PREV)) return PREV;
  if(!digitalRead(BTN_OK)) return OK;

  return NONE;
}

void drawMenu() {
  static unsigned long lastRead = 0;
  static ENUM_BUTTON lastPressedButton = OK;
  static unsigned int isPressedSince = 0;
  int autoSwitchTime = 500;

  ENUM_BUTTON pressedButton = getButton();

  if(pressedButton == NONE && lastRead != 0) {
    isPressedSince = 0;
    return;
  }
  if(pressedButton != lastPressedButton) {
    isPressedSince = 0;
  }

  if(isPressedSince > 3) autoSwitchTime = 70;
  if(lastRead != 0 && millis() - lastRead < autoSwitchTime && pressedButton == lastPressedButton) return;

  isPressedSince++;
  lastRead = millis();
  lastPressedButton = pressedButton;
  
  switch(pressedButton) {
    case NEXT: handleNext(); break;
    case PREV: handlePrev(); break;
    case BACK: handleBack(); break;
    case OK: handleOk(); break;
  }

  lcd.home();
  lcd.clear();
  if(isInLowerLevel) {
    lcd.print(menu[currentMenuPos].label);
    lcd.setCursor(0, 1);
    lcd.print(F("> "));

    if(menu[currentMenuPos].handler != NULL) {
      (*(menu[currentMenuPos].handler))();
    } else {
      lcd.print(tempVal);
    }
  } else {
    lcd.print(F("Menu glowne"));
    lcd.setCursor(0, 1);
    lcd.print(F("> "));

    lcd.print(menu[currentMenuPos].label);
  }
}

void handleNext() {
  if(isInLowerLevel) {
    tempVal++;
    if(tempVal > menu[currentMenuPos].maxVal) tempVal = menu[currentMenuPos].maxVal;
  } else {
    currentMenuPos = (currentMenuPos + 1) % menuSize;
  }
}

void handlePrev() {
  if(isInLowerLevel) {
    tempVal--;
    if(tempVal < menu[currentMenuPos].minVal) tempVal = menu[currentMenuPos].minVal;
  } else {
    currentMenuPos--;
    if(currentMenuPos < 0) currentMenuPos = menuSize - 1;
  }
}

void handleBack() {
  if(isInLowerLevel) {
    isInLowerLevel = false;
  }
}

void handleOk() {
  if(menu[currentMenuPos].handler != NULL && menu[currentMenuPos].maxVal <= menu[currentMenuPos].minVal) {
    (*(menu[currentMenuPos].handler))();
    return;
  }
  if(isInLowerLevel) {
    menu[currentMenuPos].currentVal = tempVal;
    isInLowerLevel = false;
  } else {
    tempVal = menu[currentMenuPos].currentVal;
    isInLowerLevel = true;
  }
}

/* Funkcje-uchwyty użytkownika */
void actiondioda() {
  String dictonary[] = {"ON", "OFF"};

  lcd.print(dictonary[tempVal]);
  if (dictonary[tempVal]=="ON")
  {digitalWrite(dioda, HIGH);}
  if (dictonary[tempVal]=="OFF")
  {digitalWrite(dioda, LOW);}
  

  
}

void formatalarm() {
  String dictonary[] = {"Czuwanie ON", "Czuwanie OFF"};
  lcd.print(dictonary[tempVal]);
}

void formatTemperatura() {

  lcd.begin(16,2);
  lcd.setCursor(0, 0);
  lcd.print("temperatura:");
  lcd.setCursor(0, 1);
  lcd.print("T=      stopnie");

  lcd.setCursor(2,1);
  lcd.print(analogRead(A5)*0.48828125);
  
}



/*
void setup()
{
  pinMode(8, INPUT_PULLUP);
  
  
}

void loop()
{


aktualnyCzas = millis();

if (aktualnyCzas - zapamietanyCzas1 >= 10000UL) {
  zapamietanyCzas1 = aktualnyCzas;
  lcd.begin(16,2);
  lcd.setCursor(0, 0);
  lcd.print("temperatura:");
  lcd.setCursor(0, 1);
  lcd.print("T=      stopnie");

}

 if (aktualnyCzas - zapamietanyCzas2 >= 1000UL) { 
  zapamietanyCzas2 = aktualnyCzas;
  lcd.setCursor(2,1);
  lcd.print(analogRead(A5)*0.48828125);
   
}
}
*/
 

 

 

Kod którym wyświetlałem wciśnięty znak na wyświetlaczu

#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
#include <Wire.h>

#include <Keypad.h> //biblioteka od klawiatury
 
const byte ROWS = 4; // ile wierszy
const byte COLS = 4; //ile kolumn
 
byte rowPins[ROWS] = {0, A4, 8, 9}; //piny wierszy
byte colPins[COLS] = {10, 11, 12, 13}; //piny kolumn


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

Keypad klawiatura = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); //inicjalizacja klawiatury

char znak;

void setup() {
  Serial.begin(9600);
 
}

void loop() {

  char klawisz = klawiatura.getKey();

  znak = klawisz;

  



if (klawisz){
lcd.begin(16,2);
  lcd.setCursor(0, 0);
  lcd.print(klawisz);
Serial.println(znak);
  
 

}

 

 

 

Takie coś nic nie daje:

ENUM_BUTTON getButton() {

  
char klawisz = klawiatura.getKey();
znak = klawisz;
Serial.println(znak);


 
if(znak == 'A') return BACK;
if(znak == 'B') return NEXT;
if(znak == 'C') return OK; return PREV;
if(znak == 'D') return OK;

  return NONE;
}

 

Link do komentarza
Share on other sites

10 godzin temu, Runner321 napisał:

Takie coś nic nie daje:

Ta funkcja będzie zwracać PREV o ile nie będzie wciśnięty żaden z klawiszy A, B lub C.

Przeanalizuj sobie działanie tej funkcji i powiedz:

  1. Co i dlaczego będzie zwracać po naciśnięciu klawisza C?
  2. Co i dlaczego będzie zwracać po naciśnięciu klawisza D?

 

  • Lubię! 1
Link do komentarza
Share on other sites

Bądź aktywny - zaloguj się lub utwórz konto!

Tylko zarejestrowani użytkownicy mogą komentować zawartość tej strony

Utwórz konto w ~20 sekund!

Zarejestruj nowe konto, to proste!

Zarejestruj się »

Zaloguj się

Posiadasz własne konto? Użyj go!

Zaloguj się »
×
×
  • Utwórz nowe...

Ważne informacje

Ta strona używa ciasteczek (cookies), dzięki którym może działać lepiej. Więcej na ten temat znajdziesz w Polityce Prywatności.