#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
const char* LED_GREEN = 12;
const char* LED_RED = 13;
/* Display */
LiquidCrystal_I2C lcd(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);
float min_weight;
float max_weight;
String readKeypad(int col, int row) {
String key_cache;
int index = 0;
lcd.setCursor(col, row);
while (true) {
char key = keypad.getKey();
String _key = String(key);
if (key) {
if (key_cache.length() <= 4 && _key != "A" && _key != "B" && _key != "C" && _key != "D") {
if (key_cache.indexOf(".") == -1 ) {
key_cache += String(_key);
lcd.setCursor(col + index, row);
lcd.print(key_cache[index]);
index++;
} else if (_key != ".") {
key_cache += String(_key);
lcd.setCursor(col + index, row);
lcd.print(key_cache[index]);
index++;
}
} else if (_key == "D" && index > 0) {
lcd.noBlink();
key_cache = key_cache.substring(0, key_cache.length() - 1);
index -= 1;
lcd.setCursor((col + index), row);
lcd.print(key_cache[index] + " ");
lcd.setCursor((col + index), row);
lcd.blink();
} else if (_key == "C") {
return key_cache;
} else if (_key == "B") {
return "";
}
}
// Serial.println("index: " + String(index) + "key_cache: " + key_cache);
}
}
void setMinMax() {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("SET MIN-MAX WEIGHT");
lcd.setCursor(0, 3);
lcd.print("MIN:");
lcd.setCursor(10, 3);
lcd.print("MAX:");
lcd.blink();
String min_weight_str = "";
String max_weight_str = "";
while (min_weight_str == "" || max_weight_str == "") {
min_weight_str = "";
max_weight_str = "";
lcd.setCursor(14, 3);
lcd.print(" ");
lcd.setCursor(4, 3);
lcd.print(" ");
min_weight_str = readKeypad(4, 3);
if (min_weight_str == "") {
continue;
} else {
max_weight_str = readKeypad(14, 3);
if (max_weight_str.toFloat() < min_weight_str.toFloat())
max_weight_str = "";
}
}
min_weight = min_weight_str.toFloat();
max_weight = max_weight_str.toFloat();
}
void setup() {
Serial.begin(115200);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_RED, OUTPUT);
lcd.init();
lcd.backlight();
lcd.clear();
if (!min_weight && !max_weight) {
setMinMax();
lcd.clear();
}
}
bool scr_main_state = false;
void loop() {
if (!scr_main_state) {
lcd.noBlink();
lcd.setCursor(4, 0);
lcd.print("<< READY >>");
lcd.setCursor(0, 1);
lcd.print("TOTAL: " + String(12056) + " PCS");
lcd.setCursor(0, 2);
lcd.print("MIN:" + String(min_weight));
lcd.setCursor(10, 2);
lcd.print("MAX:" + String(max_weight));
lcd.setCursor(0, 3);
lcd.print("WEIGHING: ");
scr_main_state = !scr_main_state;
lcd.blink();
}
lcd.setCursor(10, 3);
lcd.print(" ");
float current_weight = readKeypad(10, 3).toFloat();
lcd.noBlink();
lcd.setCursor(7, 0);
lcd.print("Wait...");
if (current_weight < min_weight || current_weight > max_weight)
alert();
else
digitalWrite(LED_GREEN, HIGH);
delay(200);
digitalWrite(LED_GREEN, LOW);
scr_main_state = !scr_main_state;
}
void alert() {
for (int i = 0; i < 10; i++) {
digitalWrite(LED_RED, HIGH);
lcd.noBacklight();
delay(100);
lcd.backlight();
digitalWrite(LED_RED, LOW);
delay(100);
}
}