#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
unsigned long cas = 0; //premenna pre cas
int led = 10; // PWM pin pre LED
float per = 0; //Nastavenie percent /100
// Rozlozenie klavesnice (4x4)
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns
// definovanie klavesov
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
//Matica znakov
int znak[11][8] = {
{1,1,1,1,1,1,0,1},
{0,1,1,0,0,0,0,1},
{1,1,0,1,1,0,1,1},
{1,1,1,1,0,0,1,1},
{0,1,1,0,0,1,1,1},
{1,0,1,1,0,1,1,1},
{1,0,1,1,1,1,1,1},
{1,1,1,0,0,0,0,1},
{1,1,1,1,1,1,1,1},
{1,1,1,0,0,1,1,1},
{1,0,0,0,1,1,1,0}
};
byte colPins[ROWS] = {5, 4, 3, 2}; // Pins used for the rows of the keypad
byte rowPins[COLS] = {9, 8, 7, 6}; // Pins used for the columns of the keypad
// inicializovanie klavesnice
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup()
{
pinMode(led, OUTPUT); //Nastav pin pre led na vystup
Serial.begin(9600);
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Zadanie 2");
lcd.setCursor(2,1);
lcd.print("Samuel");
delay(2000);
lcd.clear();
}
void zobraz(String(btt))
{
//Vykreslenie znaku
delay(50); //Oneskorenie pred zmenou stavu led (min. 30)
float led1=255*(per/10); //Prepocet intenzity led
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(per*10);
lcd.print(" %");
delay(50); //Oneskorenie pred zmenou stavu led (min. 30)
Serial.print("Tlacidlo ");
Serial.print(btt); //Vypis na seriovy port
Serial.print(" Hodnota "); //Vypis na seriovy port
Serial.println(led1); //Vypis na seriovy port
analogWrite(led,led1); //Zmena stavu led
}
void loop()
{
lcd.setCursor(0,0); //nastavy kurzor
cas = millis()/60/10; //prepocet na sekundy
lcd.print("Cas ");
lcd.print(cas); //zobrazi cas
lcd.print(" s");
char button = customKeypad.getKey(); //Nacita stlacene tlacidlo
if (String(button)=="0") { //Ak je stlacene tlacidlo "0"
per = 0; //Nastavenie percent /100
zobraz(String(button)); //Zobrazenie znaku a zmena stavu led
}
else if (String(button)=="1") {
per = 1;
zobraz(String(button));
}
else if (String(button)=="2") {
per = 2;
zobraz(String(button));
}
else if (String(button)=="3") {
per = 3;
zobraz(String(button));
}
else if (String(button)=="4") {
per = 4;
zobraz(String(button));
}
else if (String(button)=="5") {
per = 5;
zobraz(String(button));
}
else if (String(button)=="6") {
per = 6;
zobraz(String(button));
}
else if (String(button)=="7") {
per = 7;
zobraz(String(button));
}
else if (String(button)=="8") {
per = 8;
zobraz(String(button));
}
else if (String(button)=="9") {
per = 9;
zobraz(String(button));
}
else if (String(button)=="A" || String(button)=="B" || String(button)=="C" || String(button)=="D") { //Ak je stlacene tlacidlo "ABCD"
per = 10;
zobraz(String(button));
}
else if (String(button)=="*") { //Ak je stlacene tlacidlo "*"
if (per!=10){per += 1;}; //Pripocitaj percenta /100
zobraz(String(button));
}
else if (String(button)=="#") { //Ak je stlacene tlacidlo "#"
if (per!=0){per -= 1;}; //Odpocitaj percenta /100
zobraz(String(button));
}
}