/*
Arduino | coding-help
Coding Help for the servo motor
November 2, 2024
Moong at 1:10 PM
Hello, I am currently making a small project where im
creating a "Coffee Maker" and I am having issues with the servo motor
Fixes i tried to make
Make a Seperate file do sweep on servo (Servo worked)
Used a external power and used the project code (Servo did not work)
Not really sure what to and getting confused on what to fix
If youre interested the code is here
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <Servo.h>
// DHT Sensor Settings
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Servo
Servo myServo;
int ServoMotor = 6;
// LCD Display Settings
LiquidCrystal_I2C lcd(0x27, 16, 2);
// IR Sensor for Cup Detection
int CupSensor = 3;
// Water Pump
int Pump = 4;
int Beeper = 7;
// State tracking variables
bool isCupPlaced = false;
bool isHeating = false;
bool isHeated = false;
unsigned long current, previous, interval = 1000;
// LCD message tracking
String lastMessage1 = "";
String lastMessage2 = "";
unsigned long beeperStartTime = 0;
bool beeping = false;
// Setup function
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(CupSensor, INPUT_PULLUP);
pinMode(Pump, OUTPUT);
pinMode(Beeper, OUTPUT);
digitalWrite(Pump, HIGH); // Added missing semicolon
dht.begin();
myServo.attach(ServoMotor);
myServo.write(0); // Initial servo position
myServo.write(180);
delay(1000);
myServo.write(0);
}
void updateLCD(String message1, String message2) {
// Only update if the message has changed
if (lastMessage1 != message1 || lastMessage2 != message2) {
lastMessage1 = message1;
lastMessage2 = message2;
lcd.clear(); // Clear before printing new messages
lcd.setCursor(0, 0);
lcd.print(message1);
lcd.setCursor(0, 1);
lcd.print(message2);
}
}
void checkCup() {
if (digitalRead(CupSensor) == LOW) { // Check the actual sensor state
isCupPlaced = true;
updateLCD("Coffee Maker:", "Mug Is placed!");
} else if (digitalRead(CupSensor) == HIGH) {
isCupPlaced = false;
current = millis();
if (current - previous >= interval) {
previous = current;
updateLCD("Coffee Maker:", "Put a Mug");
}
}
}
void Boiling(float temperature) {
if (!isCupPlaced) {
myServo.write(0);
return;
}
if (isCupPlaced && temperature < 35 && !isHeated) {
myServo.write(180);
isHeating = true;
current = millis();
if (current - previous >= interval) {
previous = current;
updateLCD("Coffee Maker:", "Heating....");
}
} else if (isCupPlaced && temperature > 35) {
myServo.write(0);
isHeating = false;
isHeated = true;
current = millis();
if (current - previous >= interval) {
previous = current;
updateLCD("Coffee Maker:", "Finished Heating");
}
}
}
void PumpingWater(int waterLevel) {
if (isCupPlaced) { // Check if the cup is present
if (isHeated && waterLevel <= 100) { // Use '==' for comparison
digitalWrite(Pump, LOW); // Activate pump
current = millis();
if (current - previous >= interval) {
previous = current;
updateLCD("Coffee Maker:", "Pouring....");
}
} else if (waterLevel > 100) {
digitalWrite(Pump, HIGH); // Deactivate pump when water level is too high
if (!beeping) { // Start beeping if not already beeping
digitalWrite(Beeper, HIGH);
beeping = true;
beeperStartTime = millis(); // Start timer for beeper
updateLCD("Coffee Maker:", "Coffee Ready!");
} else if (millis() - beeperStartTime >= 1000) { // Beep for 1 second
digitalWrite(Beeper, LOW);
beeping = false; // Reset beeping state
}
}
} else {
digitalWrite(Pump, HIGH); // Ensure pump is off when no cup is detected
digitalWrite(Beeper, LOW); // Ensure beeper is off when no cup
}
}
void loop() {
int waterLevel = analogRead(A0);
float temperature = dht.readTemperature();
// Check if temperature reading is valid
if (isnan(temperature)) {
updateLCD("Coffee Maker:", "Temp Error!");
return; // Exit if temperature is invalid
}
checkCup();
Boiling(temperature);
PumpingWater(waterLevel);
}