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