#include <max6675.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,4); // Initialize the LCD display
#include <Wire.h>
int thermoDO = 6; // Data Out pin for MAX6675 temperature sensor
int thermoCS = 5; // Chip Select pin for MAX6675 temperature sensor
int thermoCLK = 4; // Clock pin for MAX6675 temperature sensor
int heater_LED = 13; // Heater LED pin
int sec=0;
int minutes=0;
const int buttonPin = A0; // Button pin for mode switching
const int keypadPin = A1; // Analog pin for numeric keypad
int Solenoid_Valve = 8; // Solenoid valve pin
int Heater = 9; // Electric heater pin
int buttonState = 0;
int i=0;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO); // Create MAX6675 object for temperature sensing
unsigned long previousMillis = 0; // Timer for LED update
const long interval = 1000; // Interval for LED update
int temp=0; // Temperature variable
int timeonedegree = 0; // Time taken for water to rise by 1 degree
int tempreq = 0; // Required temperature
int timeavail = 0; // Time available for water to reach desired temperature
int tempdiff = 0; // Difference between desired and actual temperature
int timereq = 0; // Time required to reach desired temperature
void setup() {
Serial.begin(9600); // Start serial communication
pinMode(Solenoid_Valve, OUTPUT); // Set solenoid valve pin as output
pinMode(Heater, OUTPUT); // Set electric heater pin as output
digitalWrite(Solenoid_Valve, HIGH); // Close solenoid valve initially
digitalWrite(Heater, HIGH); // Turn off heater initially
pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with internal pull-up resistor
pinMode(heater_LED,OUTPUT); // Set heater LED pin as output
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on LCD backlight
digitalWrite(heater_LED, LOW); // Turn off heater LED initially
delay(500); // Delay for stability
}
void loop() {
buttonState = digitalRead(buttonPin); // Read button state
if(buttonState == 0) { // If button is pressed
lcd.clear(); // Clear LCD
delay(250); // Delay for stability
i=!i; // Toggle mode
if(i == 1) { // If auto mode is turned on
lcd.setCursor(0, 0);
lcd.print("How much temperature do you require?");
lcd.setCursor(0, 1);
lcd.print("Enter: ");
tempreq = readKeypad(); // Read temperature input from keypad
lcd.clear();
lcd.print("After how many minutes do you want hot water?");
lcd.setCursor(0, 1);
lcd.print("Enter: ");
timeavail = readKeypad(); // Read time input from keypad
lcd.clear();
temp = thermocouple.readCelsius();
unsigned long startMillis = millis();
while(temp < tempreq) {
temp = thermocouple.readCelsius();
unsigned long currentMillis = millis();
if(currentMillis - startMillis >= 60000) { // Check every minute
timeonedegree++;
startMillis = currentMillis;
}
}
tempdiff = tempreq - temp;
timereq = tempdiff * timeonedegree;
if(timereq > timeavail) {
digitalWrite(Solenoid_Valve, HIGH); // Close solenoid valve
digitalWrite(Heater, LOW); // Turn on electric heater
digitalWrite(heater_LED, HIGH); // Turn on heater LED
}
}
else {
digitalWrite(Solenoid_Valve, LOW; // Open solenoid valve
digitalWrite(Heater, HIGH); // Turn off electric heater
digitalWrite(heater_LED, LOW); // Turn off heater LED
}
}
unsigned long currentMillis = millis(); // Get current time
if (currentMillis - previousMillis >= interval) { // If time interval passed
sec++; // Increment seconds
if (sec > 59) { // If seconds exceed 59
sec = 0; // Reset seconds
minutes++; // Increment minutes
if (minutes > 59) { // If minutes exceed 59
minutes = 0; // Reset minutes
}
if (sec < 10) { // If seconds less than 10, for formatting
lcd.setCursor(15, 0); // Set cursor position
lcd.print(" "); // Print space
}
}
lcd.setCursor(14, 0); // Set cursor position for seconds
lcd.print(sec); // Print seconds
lcd.setCursor(13, 0); // Set cursor position for colon
lcd.print(":"); // Print colon
lcd.setCursor(11, 0); // Set cursor position for minutes
lcd.print(minutes); // Print minutes
if (i == 0) { // Manual mode
lcd.setCursor(0, 0); // Set cursor position
lcd.print("Manual Mod "); // Print mode
lcd.setCursor(0, 1); // Set cursor position
lcd.print("Temp ="); // Print temperature label
lcd.setCursor(7,1); // Set cursor position for temperature value
lcd.print(temp); // Print temperature
}
// if (i == 1) { // Automatic mode
// lcd.setCursor(0, 0); // Set cursor position
// lcd.print("Auto mod "); // Print mode
// lcd.setCursor(0, 1); // Set cursor position
// lcd.print("Temp = "); // Print temperature label
// lcd.setCursor(7,1); // Set cursor position for temperature value
// lcd.print(temp); // Print temperature
// if (temp >= 60) { // If temperature is greater than or equal to 60°C
// lcd.setCursor(0, 2); // Set cursor position
// lcd.print("Valve ON "); // Print valve status
// lcd.setCursor(0, 3); // Set cursor position
// lcd.print("Heater OFF"); // Print heater status
// digitalWrite(Solenoid_Valve, LOW); // Open solenoid valve
// digitalWrite(Heater, HIGH); // Turn off electric heater
// digitalWrite(heater_LED, LOW); // Turn off heater LED
// }
// else { // If temperature is less than 60°C
// lcd.setCursor(0, 2); // Set cursor position
// lcd.print("Valve OFF"); // Print valve status
// lcd.setCursor(0, 3); // Set cursor position
// lcd.print("Heater ON"); // Print heater status
// digitalWrite(Solenoid_Valve, HIGH); // Close solenoid valve
// digitalWrite(Heater, LOW); // Turn on electric heater
// digitalWrite(heater_LED, HIGH); // Turn on heater LED
// }
// }
previousMillis = currentMillis; // Update previous time
}
}
int readKeypad() {
const int ROWS = 4;
const int COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
int rowPins[ROWS] = {9, 8, 7, 6}; // connect to the row pinouts of the keypad
int colPins[COLS] = {5, 4, 3, 2}; // connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
char key = keypad.getKey();
int value = 0;
if (key) {
value = key - '0';
return value;
}
return 0;
} Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}