#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);
/* Keypad setup */
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {5, 4, 3, 2};
byte colPins[KEYPAD_COLS] = {A3, A2, A1, A0};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'.', '0', '#', 'D'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
bool bolusMode = false;
float volume = 0.0;
float time = 0.0;
bool editMode = false;
char buffer[5];
int bufferIndex = 0;
char lastButton = ' ';
unsigned long blinks = 0;
unsigned long savedmillis = 0;
bool blinking = false;
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
lcd.setCursor(8, 0);
lcd.print("TEST");
}
void loop() {
getKeys();
if (editMode && millis() - blinks > 250) {
lcd.setCursor(bufferIndex + 8, 2);
lcd.blink();
blinks = millis();
}
else if (!editMode) {
if ( millis() - savedmillis == 2000) {
clearLCDLine(3);
}
}
}
void getKeys() {
char key = keypad.getKey();
if (bufferIndex > 5) bufferIndex = 0;
if (key != NO_KEY) {
Serial.println(key);
if (!editMode && key == 'A')
MODES();
if (key == 'B' || key == 'C') {
lastButton = key;
showValue(key);
} else if (key == 'D' && (lastButton == 'B' || lastButton == 'C')) {
enterEditMode(lastButton);
} else if (key == '#') {
saveAndExit();
}
else if (editMode && key == 'A') {
bufferIndex--;
lcd.setCursor(8 + bufferIndex, 2);
lcd.print(' ');
}
else {
if (editMode && (key >= '0' && key <= '9' || key == '.')) {
buffer[bufferIndex++] = key;
lcd.setCursor(8, 2);
lcd.print(buffer);
Serial.print("BUFFER: ");
Serial.println(bufferIndex);
}
}
}
}
void showValue(char key) {
if (key == 'B') {
buttonB();
} else if (key == 'C') {
buttonC();
}
}
void enterEditMode(char key) {
editMode = true;
bufferIndex = 0;
lcd.setCursor(8, 2);
}
void buttonB() {
clearLCDLine(1);
clearLCDLine(2);
clearLCDLine(3);
if (bolusMode == false) {
lcd.setCursor(0, 1);
lcd.print("INFUSION MODE");
}
// else if (bolusMode == true) {
else {
lcd.setCursor(0, 1);
lcd.print("BOLUS MODE");
}
lcd.setCursor(0, 2);
lcd.print("VALUE:");
lcd.setCursor(8, 2);
lcd.print(formatFloat(volume));
}
void buttonC() {
clearLCDLine(1);
clearLCDLine(2);
clearLCDLine(3);
if (bolusMode == false) {
lcd.setCursor(0, 1);
lcd.print("INFUSION MODE");
}
else {
lcd.setCursor(0, 1);
lcd.print("BOLUS MODE");
}
lcd.setCursor(0, 2);
lcd.print("TIME:");
lcd.setCursor(8, 2);
lcd.print(formatFloat(time));
}
void saveAndExit() {
if (editMode) {
buffer[bufferIndex] = '\0';
float newValue = atof(buffer);
if (lastButton == 'B') {
volume = newValue;
showValue('B');
} else if (lastButton == 'C') {
time = newValue;
showValue('C');
}
editMode = false;
lcd.setCursor(5, 3);
lcd.print("SAVED");
lcd.noBlink();
savedmillis = millis();
}
}
String formatFloat(float value) {
char temp[6];
dtostrf(value, 5, 2, temp);
return String(temp);
}
void MODES() {
// Serial.println("key 1 is pressed");
bolusMode = !bolusMode;
clearLCDLine(1);
clearLCDLine(2);
clearLCDLine(3);
if (bolusMode == true) {
lcd.setCursor(0, 1);
lcd.print("MODE : ");
lcd.setCursor(7, 1);
lcd.print("BOLUS");
}
else if (bolusMode == false) {
lcd.setCursor(0, 1);
lcd.print("MODE : ");
lcd.setCursor(7, 1);
lcd.print("INFUSION");
}
lcd.setCursor(5, 3);
lcd.print("test");
}
void clearLCDLine(int line)
{
lcd.setCursor(0, line);
for (int n = 0; n < 20; n++) // 20 indicates symbols in line. For 2x16 LCD write - 16
{
lcd.print(" ");
}
}