Wiring Connections:
• buttons
- 1st leg of all buttons --> GND Arduino
- 2nd leg of all buttons --> pin 2-7 arduino
• water pump 5v
- Positive wire (water pump) --> NO (Normally Open) terminal on the relay module
- Negative wire (water pump) --> Negative wire (battery)
• relay module
- IN (relay module) --> pin 9 arduino
- VCC (relay module) --> VCC pin arduino
- GND (relay module) --> GND pin arduino
• battery/power supply
- Positive wire (battery) --> C (Common) terminal on the relay module
• infrared sensor
- VCC pin --> VCC of the arduino
- GND pin --> GND of the arduino
- OUT pin --> pin 10 of the arduino
• LCD 16x2 with I2C
- VCC pin --> VCC of the arduino
- GND pin --> GND of the arduino
- SDA pin --> SDA of the arduino
- SCL pin --> SCL of the arduino
Code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int motorPin = 9; // relay module IN pin for water pump
const int lowShotButton = 2;
const int mediumShotButton = 3;
const int highShotButton = 4;
const int veryHighShotButton = 5;
const int autoModeButton = 6;
const int manualModeButton = 7;
const int infraredPin = 10;
int selectedDrink = 0;
int selectedMode = 0;
bool manualControl = false;
bool cupDetected = false;
bool autoModePump = false;
void setup() {
pinMode(motorPin, OUTPUT);
pinMode(lowShotButton, INPUT_PULLUP);
pinMode(mediumShotButton, INPUT_PULLUP);
pinMode(highShotButton, INPUT_PULLUP);
pinMode(veryHighShotButton, INPUT_PULLUP);
pinMode(autoModeButton, INPUT_PULLUP);
pinMode(manualModeButton, INPUT_PULLUP);
pinMode(infraredPin, INPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(3, 0);
lcd.print("Beverage ");
lcd.setCursor(3, 1);
lcd.print("Dispenser");
delay(1000);
}
void loop() {
// Select drink
if (digitalRead(lowShotButton) == LOW) {
selectedDrink = 1;
lcd.setCursor(0, 0);
lcd.print("Selected Drink: ");
lcd.setCursor(0, 1);
lcd.print("Low ");
delay(1500);
} else if (digitalRead(mediumShotButton) == LOW) {
selectedDrink = 2;
lcd.setCursor(0, 0);
lcd.print("Selected Drink: ");
lcd.setCursor(0, 1);
lcd.print("Medium ");
delay(1500);
} else if (digitalRead(highShotButton) == LOW) {
selectedDrink = 3;
lcd.setCursor(0, 0);
lcd.print("Selected Drink: ");
lcd.setCursor(0, 1);
lcd.print("High ");
delay(1500);
} else if (digitalRead(veryHighShotButton) == LOW) {
selectedDrink = 4;
lcd.setCursor(0, 0);
lcd.print("Selected Drink: ");
lcd.setCursor(0, 1);
lcd.print("Very High ");
delay(1500);
}
// Select mode
if (digitalRead(autoModeButton) == LOW) {
if (selectedDrink == 0) {
lcd.setCursor(0, 0);
lcd.print("Choose Drink ");
lcd.setCursor(0, 1);
lcd.print("First ");
delay(1000);
} else {
selectedMode = 1; // Automatic mode
autoModePump = true;
lcd.setCursor(0, 0);
lcd.print("Mode: Automatic");
lcd.setCursor(0, 1);
lcd.print(" ");
delay(1000);
}
} else if (digitalRead(manualModeButton) == LOW) {
if(selectedDrink == 0) {
lcd.setCursor(0, 0);
lcd.print("Choose Drink ");
lcd.setCursor(0, 1);
lcd.print("First ");
delay(1000);
} else {
selectedMode = 2; // Manual mode
lcd.setCursor(0, 0);
lcd.print("Mode: Manual ");
lcd.setCursor(0, 1);
lcd.print(" ");
delay(1000);
}
}
if (selectedMode == 1) { // Automatic mode
cupDetected = detectCup();
if (!cupDetected) {
lcd.setCursor(0, 0);
lcd.print("Waiting for the");
lcd.setCursor(0, 1);
lcd.print("cup.... ");
delay(2000);
} else {
lcd.setCursor(0, 0);
lcd.print("Cup detected! ");
lcd.setCursor(0, 1);
lcd.print("AutoMode: ON");
delay(1000);
activatePump(selectedDrink);
autoModePump = false;
delay(1000);
reset();
}
} else if (selectedMode == 2) { // Manual mode
if (digitalRead(manualModeButton) == LOW) {
digitalWrite(motorPin, HIGH);
lcd.setCursor(0, 1);
lcd.print("Manual mode: ON");
delay(1000);
manualControl = true;
} else {
if (manualControl) {
digitalWrite(motorPin, LOW);
lcd.setCursor(0, 1);
lcd.print("Manual Mode: OFF");
delay(1000);
lcd.clear();
manualControl = false;
reset();
}
}
}
}
bool detectCup() {
int infraredValue = digitalRead(infraredPin);
if (infraredValue == LOW) {
return true;
} else {
return false;
}
}
// Water pump will turn off based on the set durations(Auto mode)
void activatePump(int drink) {
int duration = 0;
switch (drink) {
case 1: // Low Shot
duration = 3000; // duration
break;
case 2: // Medium Shot
duration = 3000; // duration
break;
case 3: // High Shot
duration = 3000; // duration
break;
case 4: // Very High Shot
duration = 3000; // duration
break;
}
digitalWrite(motorPin, HIGH);
delay(duration);
digitalWrite(motorPin, LOW);
}
void reset() {
selectedDrink = 0;
selectedMode = 0;
autoModePump = false;
lcd.setCursor(0, 0);
lcd.print("Done");
delay(1500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Choose Drink");
lcd.setCursor(0, 1);
lcd.print("Level First...");
}
I will explain the function. First you choose from the selected drinks (low, medium, high, very high) when you have selected for example medium among the selected drinks then there is a print on the LCD that says "Selected drink: medium". after you choose from the selected drinks, you will choose auto or manual mode. when you choose auto mode and you have not yet chosen from the selected drinks, there is a print on the LCD that says "Choose Drink First...". When you have selected something from the selected drinks and then the glass is not there or the infrared sensor has not detected the glass, it will print on the LCD "Waiting for the cup..." then when the glass has been placed or the sensor has detected it, it will be printed. Cup detected..." then the water pump will turn on. Then the pump will only turn off based on the duration set in the code. When in manual mode, it's the same as in auto mode, if you haven't selected anything from the selected drinks, there is also a print on the LCD that says "Choose Drink First..." when you hold the button of the manual button, the pump will continuously turn on and when you released the pump it will turn off. Then when the water pump is finished pouring from the cup in any of the two modes (auto or manual) selected, it will reset the selected drink and the selected mode, then back to the first step. choose again from selected drink then selected mode then the will open pump after that, it will reset again. back to that step again.
sorry for my English i hope you understand and help me check if its the wiring connection or the code is wrong
