#include <LiquidCrystal.h>
#include <dht.h>
dht DHT;
#define DHT22_PIN 13
class Keyboard
{
private:
int rows[4];
int cols[4];
public:
Keyboard(int, int, int, int, int, int, int, int);
char keys();
void begin();
};
Keyboard::Keyboard(int a, int b, int c, int d, int e, int f, int g, int h)
{
rows[0] = a;
rows[1] = b;
rows[2] = c;
rows[3] = d;
cols[0] = e;
cols[1] = f;
cols[2] = g;
cols[3] = h;
}
void Keyboard::begin()
{
for(int i = 0; i < 4; i++)
{
pinMode(rows[i], INPUT_PULLUP);
pinMode(cols[i], OUTPUT);
}
}
char Keyboard::keys()
{
char key_values[4][4] = {
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', '*'},
{'C', '0', '=', '/'}
};
for(int j = 0; j < 4; j++)
{
for(int col = 0; col < 4; col++)
{
if(col == j)
{
digitalWrite(cols[j], LOW);
}
else
{
digitalWrite(cols[col], HIGH);
}
}
for(int i = 0; i < 4; i++)
{
if(!digitalRead(rows[i]))
{
delay(200);
return key_values[i][j];
}
}
}
return 10;
}
LiquidCrystal display(12, 11, 10, 9, 8, 7);
Keyboard kb(5, 4, 3, 2, A0, A1, A2, A3);
int s1 = 0, s2 = 0, ch1 = 0, ch2 = 0;
char c = 0;
String a1;
void setup()
{
display.begin(20, 4);
kb.begin();
}
void loop()
{
char key = 0;
key = kb.keys();
switch(key)
{
case '1':
display.setCursor(s1, s2);
display.print(key);
a1 += key;
s1++;
break;
case '2':
display.setCursor(s1, s2);
display.print(key);
a1 += key;
s1++;
break;
case '3':
display.setCursor(s1, s2);
display.print(key);
a1 += key;
s1++;
break;
case '4':
display.setCursor(s1, s2);
display.print(key);
a1 += key;
s1++;
break;
case '5':
display.setCursor(s1, s2);
display.print(key);
a1 += key;
s1++;
break;
case '6':
display.setCursor(s1, s2);
display.print(key);
a1 += key;
s1++;
break;
case '7':
display.setCursor(s1, s2);
display.print(key);
a1 += key;
s1++;
break;
case '8':
display.setCursor(s1, s2);
display.print(key);
a1 += key;
s1++;
break;
case '9':
display.setCursor(s1, s2);
display.print(key);
a1 += key;
s1++;
break;
case '0':
display.setCursor(s1, s2);
display.print(key);
a1 += key;
s1++;
break;
case '+':
display.setCursor(s1, s2);
display.print(key);
s1 = 0;
s2 = !s2;
c = '+';
ch1 = a1.toInt();
a1 = "";
break;
case '-':
display.setCursor(s1, s2);
display.print(key);
s1 = 0;
s2 = !s2;
c = '-';
ch1 = a1.toInt();
a1 = "";
break;
case '*':
display.setCursor(s1, s2);
display.print(key);
s1 = 0;
s2 = !s2;
c = '*';
ch1 = a1.toInt();
a1 = "";
break;
case '/':
display.setCursor(s1, s2);
display.print(key);
s1 = 0;
s2 = !s2;
c = '/';
ch1 = a1.toInt();
a1 = "";
break;
case 'C':
display.clear();
display.setCursor(0, 0);
s1 = s2 = 0;
c = 0;
ch1 = ch2 = 0;
a1 = "";
break;
case '=':
ch2 = a1.toInt();
switch(c)
{
case '+':
ch2 = ch1 + ch2;
break;
case '-':
ch2 = ch1 - ch2;
break;
case '*':
ch2 = ch1 * ch2;
break;
case '/':
ch2 = ch1 / ch2;
break;
}
display.clear();
display.setCursor(0, 0);
display.print(ch2);
int chk = DHT.read22(DHT22_PIN);
display.setCursor(0,2);
display.print("Temp: ");
display.print(DHT.temperature);
display.print((char)223);
display.print("C");
display.setCursor(0,3);
display.print("Humidity: ");
display.print(DHT.humidity);
display.print("%");
delay(1000);
break;
}
}