MENU and SETTINGS

Good day guys!

I have a vending machine project. My prof wanted me to have a Menu and Settings on my vending machine that changes the Amount of each item and save after turning off using EEPROM.

I am only a beginner and mastering C++.

These are the materials:

  1. Arduino Mega 2560
  2. 4 Servos
  3. 1 Keypad
  4. 1 Multi Coin Acceptor
  5. 1 Vending button
  6. 1 LED for indicator process

This is the flow:

  1. Turn on machine.
  2. LCD display "Welcome Vendo" "Choose Item".
    Turn LED on.
  3. button1 - 4 represents the 4 trays inside the vending machine.
  4. if button1 is pressed, LCD displays "Item 01 is selected" "Insert Coin". Turn LED off
  5. If the Coin Acceptor pulse == itemAmount of item 01, display the Amount digits everytime a coin is inserted. Blink LED twice.
  6. Vending item and LCD displays "Vending Item" "Please Wait".
  7. After the process, LCD displays "Item Vended" "Thank you".
  8. Back to the first process.

I hope someone can help me. I want to analyze the source code if someone would give me. Please, do not think of me being a spoonfeeding. I will analyze the code and then try and try again until I understand the whole program and process.

THANK YOU

And when you graduate, will you ask for codes to use in your new job?

Try to be creative. Show that you are willing to learn.
Write a code, post here you will receive help to correct possible errors.

1 Like

You will also need an LCD and power.

You must have had prerequisite classes before being handed this assignment, so you should have written sketches for the listed devices. Making the devices work together might only be a matter of pin assignments and adding EEPROM writing and reading.

Show your work.

What's needed is called a "State Machine"

enum states 
{
  CHOOSE,
  SELECTED,
  PAY,
  VEND,
  THANKS
}

states currentState = CHOOSE;

void loop() {
  
 switch(currenentState)
{
  case CHOOSE:
    // code todo this:
    // 1. LCD display "Welcome Vendo" "Choose Item".
    // 2. Turn LED on.
    currenentState = SELECTED;
    break;

  case SELECTED:
    // code todo this:
    // 3. button1 - 4 represents the 4 trays inside the vending machine.
    // 4. if button1 is pressed, LCD displays "Item 01 is selected" "Insert Coin". Turn LED off 
    currenentState = PAY;
    break;
        
  case PAY:
    // code todo this:
    // 5. If the Coin Acceptor pulse == itemAmount of item 01, display the Amount digits everytime a 
    // coin is inserted. Blink LED twice.
    currenentState = VEND;
    break;

  case VEND:
    // code todo this:
    // 6. Vending item and LCD displays "Vending Item" "Please Wait".
    currenentState = SELECTED;
    break;

  case THANKS:
    // code todo this:
    // 7. After the process, LCD displays "Item Vended" "Thank you".
    currenentState = CHOOSE;
    break;
    
}

}
2 Likes

As what I have said. Please don't think of me spoon feeding. I am just asking about EEPROM.

[type or paste code here](https://wokwi.com/projects/379880149342531585)

check this code and please correct me thanks

Please Correct my code.

#include <Servo.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo servo1, servo2, servo3, servo4; //servo identities

#define button1 2 //----------------
#define button2 3 //|    BUTTON    |
#define button3 4 //|  PIN NUMBERS |
#define button4 5 //----------------
#define coinSlot 6 // coin slot counter wire to pin 6
#define LED 7 // LED Anode to pin 7 for processing indicator
#define vending 8 // vendin button to pin 8

int itemvalue1 = 10; // Replace 10 with the actual value for item #1
int itemvalue2 = 20; // Replace 20 with the actual value for item #2
int itemvalue3 = 30; // Replace 30 with the actual value for item #3
int itemvalue4 = 40; // Replace 40 with the actual value for item #4

// Define the number of items
const int numItems = 4;
// Define an array to store the item values
int itemValues[numItems] = {itemvalue1, itemvalue2, itemvalue3, itemvalue4};

// Define an array to store the button pins
int buttonPins[numItems] = {button1, button2, button3, button4};
int buttonStatus;

// Define coinslot pulse and coinpulse status
int pulse;
int coinSlotStatus;
int buttonPressed;
int pulseValue = pulse; // Assuming you have a variable called 'pulse' with a value

//vending variables
int vendingStatus;

boolean userBalance = false;

void setup() {
  // put your setup code here, to run once:
  lcd.init();
  lcd.backlight();
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("     MARVIN");
  delay(1000);
  lcd.setCursor(0,1);
  lcd.print("  Ma. VICTORIA");
  delay(50);
  digitalWrite(LED, HIGH);
  delay(500);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("  PLEASE PRESS");
  lcd.setCursor(0,1);
  lcd.print("  ITEM  BUTTON");
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);
  pinMode(button4, INPUT_PULLUP);
  pinMode(coinSlot, INPUT_PULLUP);
  pinMode(LED, OUTPUT);
  pinMode(vending, INPUT_PULLUP);
  servo1.attach(30);
  servo2.attach(31);
  servo3.attach(32);
  servo4.attach(33);
}

void loop() {
  // Loop through the items
  //Wait until one of the item buttons is pressed.
  for (int i = 0; i < numItems; i++) {
    int buttonStatus = digitalRead(buttonPins[i]);
    if (buttonStatus == 0) {
      buttonPressed = 1;
      digitalWrite(LED, LOW);
      delay(200);
      digitalWrite(LED, HIGH);
      delay(200);
      digitalWrite(LED, LOW);
      delay(200);
      digitalWrite(LED, HIGH);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("    ITEM #" + String(i + 1, DEC));
      lcd.setCursor(0, 1);
      lcd.print("  IS  SELECTED");
      delay(1000);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("  INSERT COINS");
      lcd.setCursor(0, 1);
      lcd.print("    PHP ");
      lcd.setCursor(8, 1);
      lcd.print(itemValues[i]);
    }
  }
  //After one of the buttons is pressed, execute the LCD display.

  //INSERT COIN
  if(buttonPressed == 1){ // If no button is pressed, coinslot is disabled.
    coinSlotStatus = digitalRead(coinSlot); // wait to insert coins.
    delay(30);
      if (coinSlotStatus == LOW) {
      digitalWrite(LED, LOW);
      userBalance = true;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print(" COINS INSERTED");
      pulse += 1;
      char lcdBuffer[16];
      sprintf(lcdBuffer, "Bal. PHP %d.00", pulse);
      lcd.setCursor(0, 1);
      lcd.print(lcdBuffer);
      delay(30);
    }
  } // INSERT COINS ends here.
    
  // VENDING the item selected

  vendingStatus = digitalRead(vending);
  if(vendingStatus == 0 && buttonPressed == 1 && pulse == itemvalue1){
    pulse = 0;
    buttonPressed = 0;
    userBalance = false;
    servo1.write(180);
    delay(2000);
    servo1.write(0);
    vendingDisplay();
  }
  if(vendingStatus == 0 && buttonPressed == 1 && pulse == itemvalue2){
    pulse = 0;
    buttonPressed = 0;
    userBalance = false;
    servo2.write(180);
    delay(2000);
    servo2.write(0);
    vendingDisplay();
  }
  if(vendingStatus == 0 && buttonPressed == 1 && pulse == itemvalue3){
    pulse = 0;
    buttonPressed = 0;
    userBalance = false;
    servo3.write(180);
    delay(2000);
    servo3.write(0);
    vendingDisplay();
  }
  if(vendingStatus == 0 && buttonPressed == 1 && pulse == itemvalue4){
    pulse = 0;
    buttonPressed = 0;
    userBalance = false;
    servo4.write(180);
    delay(2000);
    servo4.write(0);
    vendingDisplay();
  }
}

void vendingDisplay(){
    lcd.clear();
    delay(500);
    char lcdBuffer[16];
    sprintf(lcdBuffer, "  Bal. PHP%d.00", pulse);
    lcd.setCursor(0, 0);
    lcd.print("  ITEM  VENDED");
    lcd.setCursor(0, 1);
    lcd.print(lcdBuffer);
    delay(2000);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("   THANK  YOU");
    delay(2000);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("  PLEASE PRESS");
    lcd.setCursor(0,1);
    lcd.print("  ITEM  BUTTON");
}

I want to add a Menu and Settings by adding another button using EEPROM. The function is to change the value of the item amount anytime.

Thank you guys.

here your project simulation.
"Menu - Wokwi ESP32, STM32, Arduino Simulator

My first comment:
Name the button with function name, not only " buttonX "
Use like your menu ask to press.
Ex: PLEASE PRESS ITEM BUTTON.. What button is ITEM BUTTON?

Hello thank you for your reply. :slight_smile:

My logic for my vending machine is this:

there are 4 buttons and each of them assigned to the trays 1 2 3 4. The LCD that displays "Please Press Item Button" is for the button that assigned to every item tray. After a button is pressed, LCD will clear and displays "ITEM (num) is selected" "INSERT COINS". If the pulse from the Coin Acceptor is greater than or equal to itemvalue1 then that is the time when vending button is pressed, servo1 will rotate and vend the item selected, reset pulse value to "0", LCD displays "Thank you" and back to origin.

OK, I just finished my code now with no errors

#include <Servo.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo servos[4]; //servo identities

#define button1 2 //----------------
#define button2 3 //|    BUTTON    |
#define button3 4 //|  PIN NUMBERS |
#define button4 5 //----------------
#define coinSlot 6 // coin slot counter wire to pin 6
#define LED 7 // LED Anode to pin 7 for processing indicator
#define vending 8 // vendin button to pin 8

int itemvalue1 = 10; // Replace 10 with the actual value for item #1
int itemvalue2 = 20; // Replace 20 with the actual value for item #2
int itemvalue3 = 30; // Replace 30 with the actual value for item #3
int itemvalue4 = 40; // Replace 40 with the actual value for item #4

const int numItems = 4;
int itemValues[numItems] = {itemvalue1, itemvalue2, itemvalue3, itemvalue4};
int buttonPins[numItems] = {button1, button2, button3, button4};
int buttonStatus;
int coinBalance;
int pulse;
int coinSlotStatus;
int buttonPressed;
int pulseValue = pulse;
int selectedMenuItem; // Track the selected item

int vendingStatus;
boolean userBalance = false;

void setup() {
  // put your setup code here, to run once:
  lcd.init();
  lcd.backlight();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("     MARVIN");
  delay(1000);
  lcd.setCursor(0, 1);
  lcd.print("  Ma. VICTORIA");
  delay(50);
  digitalWrite(LED, HIGH);
  delay(500);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("  PLEASE PRESS");
  lcd.setCursor(0, 1);
  lcd.print("  ITEM  BUTTON");
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);
  pinMode(button4, INPUT_PULLUP);
  pinMode(coinSlot, INPUT_PULLUP);
  pinMode(LED, OUTPUT);
  pinMode(vending, INPUT_PULLUP);
  servos[0].attach(30); // Attach servo1 to pin 30
  servos[1].attach(31); // Attach servo2 to pin 31
  servos[2].attach(32); // Attach servo3 to pin 32
  servos[3].attach(33); // Attach servo4 to pin 33
}

void loop() {
  // Loop through the items
  //Wait until one of the item buttons is pressed.
  for (int i = 0; i < numItems; i++) {
    int buttonStatus = digitalRead(buttonPins[i]);
    if (buttonStatus == 0) {
      buttonPressed = 1;
      selectedMenuItem = i; // Track the selected item
      digitalWrite(LED, LOW);
      delay(200);
      digitalWrite(LED, HIGH);
      delay(200);
      digitalWrite(LED, LOW);
      delay(200);
      digitalWrite(LED, HIGH);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("    ITEM #" + String(i + 1, DEC));
      lcd.setCursor(0, 1);
      lcd.print("  IS  SELECTED");
      delay(1000);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("  INSERT COINS");
      lcd.setCursor(0, 1);
      lcd.print("    PHP ");
      lcd.setCursor(8, 1);
      lcd.print(itemValues[i]);
    }
  }
  //After one of the buttons is pressed, execute the LCD display.

  //INSERT COIN
  if (buttonPressed == 1) { // If no button is pressed, coinslot is disabled.
    coinSlotStatus = digitalRead(coinSlot); // wait to insert coins.
    delay(30);
    if (coinSlotStatus == LOW) {
      digitalWrite(LED, LOW);
      userBalance = true;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print(" COINS INSERTED");
      pulse += 1;
      char lcdBuffer[16];
      sprintf(lcdBuffer, "Bal. PHP %d.00", pulse);
      lcd.setCursor(0, 1);
      lcd.print(lcdBuffer);
      delay(30);
    }
  } // INSERT COINS ends here.

  // VENDING the item selected

  vendingStatus = digitalRead(vending);
  if (vendingStatus == 0 && buttonPressed == 1 && pulse >= itemValues[selectedMenuItem]) {
    coinBalance = pulse - itemValues[selectedMenuItem];
    buttonPressed = 0;
    userBalance = false;
    servos[selectedMenuItem].write(180); // Rotate the selected servo
    delay(2000);
    servos[selectedMenuItem].write(0); // Stop rotating the selected servo
    vendingDisplay();
    pulse = coinBalance;
  }
}

void vendingDisplay() {
  lcd.clear();
  delay(500);
  char lcdBuffer[16];
  sprintf(lcdBuffer, "  Bal. PHP%d.00", coinBalance);
  lcd.setCursor(0, 0);
  lcd.print("  ITEM  VENDED");
  lcd.setCursor(0, 1);
  lcd.print(lcdBuffer);
  delay(2000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("   THANK  YOU");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("  PLEASE PRESS");
  lcd.setCursor(0, 1);
  lcd.print("  ITEM  BUTTON");
}

What I am really asking in this thread is about on how to create a Menu and Settings that will change the value of the Item values using EEPROM. After turning the machine off, the settings that was set will retain.

Please help me. Thank you.

First of all you need to define a button for navigate in a routine of menu to change value.

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