Need help with checking code and Wiring Connection

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

  • First off, switches should be dealt with when they change state.

  • Do you know what State Machines are ?

  • Avoid using delay( ) as if it were a plague.

  • Always show us a good schematic of your proposed circuit.
    Show us good images of your ‘actual’ wiring.
    Give links to components.

Hi, @newbieinarduino
Welcome to the forum.

Thanks for using code tags, :+1:

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Tom.. :smiley: :+1: :coffee: :australia:

You can check the wiring by using simple example sketches like "Button" or "Blink" or "HelloWorld" to read or toggle your various inputs and outputs. You may need to edit the pins to match the hardware connections you are using. If all the hardware works as connected, then the problems must be in the interactions or in the code.

Hi, @newbieinarduino

Have you built this project?

Did you write the code?

Did you write the code in stages and test each stage separately?

Tom.. :smiley: :+1: :coffee: :australia:

Your word schematic is useless, I will not spend the time to figure it out. A real schematic the language of electronics would be a big help in trying to solve your problem.

Hello newbieinarduino

I did a little review of the code, which seems to have grown dynamically.
I didn't want to use the word spaghetti code.

And now?

Take a sheet of paper and a pencil and design a programme structure plan based on the IPO model before you start coding.
Identify functions and their dependencies. The following functions might be coded:

  1. timer
  2. button handler
  3. LCD output handler
  4. function i´ve forgoten to mention

Now start coding and testing, function by function. A controllable timer function is very important. Using the delay() function blocks the real-time behaviour of the program and should be avoided. The BlinkOutWithDelay, aka BWOD, example from the IDE, the mother of all Arduino timers, is ideal as a basis for this.

At the end, merge all tested functions to get the final design.
The basics for some functions can be found in the IDE as examples.

Finally, I would like to mention that ARRAY and ENUM are your friends.

Have a nice day and enjoy coding in C++.