/**
Arduino Electronic Safe
Copyright (C) 2020, Uri Shaked.
Released under the MIT License.
*/
#include <LiquidCrystal.h>
#include <Keypad.h>
/* Display */
LiquidCrystal lcd(12, 11, 10, 9, 8, 13);
/* Keypad setup */
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {7, 6,5 , 4};
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);
const int heaterValues[] = {4, 6, 10, 12, 15, 18, 35, 35, 70, 70, 70, 140}; // Heater power values
const int numHeaters = sizeof(heaterValues) / sizeof(heaterValues[0]);
const int maxLoad = 485;
int currentLoad = 0; // Initial load value
String input = "";
void setup() {
lcd.begin(16, 2);
for (int pin = 22; pin <= 33; pin++) {
pinMode(pin, OUTPUT); // Set digital output pins for SSRs
digitalWrite(pin, LOW); // Initially turn off all SSRs
}
lcd.print("Welcome!");
delay(2000); // Display welcome for 2 seconds
lcd.clear();
updateDisplay();
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '#') { // Confirm input
if (input.length() > 0) {
int newLoad = input.toInt();
if (newLoad >= 0 && newLoad <= 880) {
currentLoad = newLoad; // Update current load
calculateAndControlLoad(currentLoad);
}
input = ""; // Clear input
updateDisplay();
}
} else if (key == '*') { // Clear input
input = "";
} else {
if (input.length() < 3) {
input += key; // Append key to input
}
}
updateInputDisplay();
}
}
void calculateAndControlLoad(int targetLoad) {
int bestCombination[numHeaters] = {0}; // Track which heaters are on
int bestLoad = 0;
for (int i = 0; i < (1 << numHeaters); i++) { // Iterate through all combinations
int totalLoad = 0;
for (int j = 0; j < numHeaters; j++) {
if (i & (1 << j)) {
totalLoad += heaterValues[j]; // Add heater load if it's on
}
}
// Check if this combination is valid and closer to the target load
if (totalLoad <= maxLoad && totalLoad <= targetLoad && totalLoad > bestLoad) {
bestLoad = totalLoad;
for (int j = 0; j < numHeaters; j++) {
bestCombination[j] = (i & (1 << j)) ? 1 : 0; // Store the combination
}
}
}
// Control the SSRs based on the best combination
for (int pin = 22; pin <= 33; pin++) {
digitalWrite(pin, LOW); // Turn off all SSRs
}
for (int j = 0; j < numHeaters; j++) {
if (bestCombination[j]) {
digitalWrite(22 + j, HIGH); // Turn on selected SSRs
}
}
// Update the display with the best load applied
currentLoad = bestLoad; // Update current load to the best load found
}
void updateDisplay() {
lcd.clear();
lcd.print("Load: ");
lcd.print(currentLoad < 100 ? "0" : ""); // Leading zeros
lcd.print(currentLoad < 10 ? "0" : "");
lcd.print(currentLoad);
lcd.print(" kw");
}
void updateInputDisplay() {
lcd.setCursor(0, 1);
lcd.print("Input: ");
lcd.print(input);
lcd.print(" "); // Clear the rest of the line
}Loading
cd74hc4067
cd74hc4067