Vending machine using cardboard, arduino, lcd screen, 4 servo motor, buttons. Help needed to make circuit and code

Hi,
I am trying to make a vending Machine that had an option to dispense one of 4 items. I am trying to make the circuit but it isn't working out very well(link for picture below). There is also some code that I have written(code below). I don't understand exactly what i am doing wrong. I am using an arduino uno. There is 4 buttons which each are connected to one of servo motors. There is also an lcd screen that displays messages when a button is clicked. The servo motors move when button is clicked and are supposed to drop one item.

#include <Servo.h>
#include <LiquidCrystal_I2C.h>

// Pins for servo motors
const int servo1Pin = 9;
const int servo2Pin = 10;
const int servo3Pin = 11;
const int servo4Pin = 12;

// Pins for buttons
const int button1Pin = 2;
const int button2Pin = 3;
const int button3Pin = 4;
const int button4Pin = 5;

// Pins for LCD screen
const int lcdAddress = 0x27; // Address of the LCD screen
const int lcdCols = 16;     // Number of columns in the LCD screen
const int lcdRows = 2;      // Number of rows in the LCD screen

// Global variables
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
LiquidCrystal_I2C lcd(lcdAddress, lcdCols, lcdRows);

// Button states
int button1State = 0;
int button2State = 0;
int button3State = 0;
int button4State = 0;

void setup()
{
  // Initialize servo motors
  servo1.attach(servo1Pin);
  servo2.attach(servo2Pin);
  servo3.attach(servo3Pin);
  servo4.attach(servo4Pin);

  // Initialize LCD screen
  lcd.begin(lcdCols, lcdRows);
  lcd.print("Select an option");

  // Initialize button pins
  pinMode(button1Pin, INPUT_PULLUP);
  pinMode(button2Pin, INPUT_PULLUP);
  pinMode(button3Pin, INPUT_PULLUP);
  pinMode(button4Pin, INPUT_PULLUP);
}

void loop()
{
  // Read button states
  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);
  button3State = digitalRead(button3Pin);
  button4State = digitalRead(button4Pin);

  // Check button states
  if (button1State == LOW)
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Button 1 Pressed");
    moveServo(servo1, 90);
  }
  else if (button2State == LOW)
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Button 2 Pressed");
    moveServo(servo2, 90);
  }
  else if (button3State == LOW)
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Button 3 Pressed");
    moveServo(servo3, 90);
  }
  else if (button4State == LOW)
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Button 4 Pressed");
    moveServo(servo4, 90);
  }
}

void moveServo(Servo servo, int angle)
{
  servo.write(angle);
  delay(1000); // Adjust the delay time as needed
  servo.write(0);
}

Link for picture of circuit: Screenshot 2023-06-14 204655.png - Google Drive

Your sketch (code) works. What is your question, or what is not working?

Your drawing shows the buttons are connected correctly. Verify your hardware is connected correctly. The button pins are programmed INPUT_PULLUP, so when your button is pressed, a path should be from the pin, through the button, to ground.

Hello tak123

What is going wrong?

Just a comment…. since you’re starting out, get into good habits.

Putting your buttons and servos into arrays, as well as naming them rather than just 1,2:3 etc.

As you move forward, look at getting rid of the delay() calls, use millis() and look into state-machines.

Do not feed the servos from the Arduino. Use an external power supply to run the servos, e.g. Sensor Shield V5.o.

is it possible to do the same thing without anything extra. like maybe telling the arduino to only give power to the servo that's button is being pressed or something?

Possible with relays, but explain how moving the servo to the initial position when the power is applied will help with your project.

Your code does that. Check your power supply to the servos and your wiring. Here is a simulation of your code...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.