Help with Vending Machine

I m building a vending machine through Arduino(Without coin machine). So the idea is whenever someone presses a button, LCD will display 25 cents along with LED will light up and buzzer will sound. you would have to press button up to 75 cents before the servo motor rotates and dispense an item. So my question is how do i sync the button with LCD, BUZZER and LED. We have to use “state machine” to code. Please help me, here is what i got so far:

int button;
int state=0;
int nxtState=0;


#include <LiquidCrystal.h>
#include "pitches.h"

//notes in melody:
int melody[] = {
 NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
 4, 8, 8, 4, 4, 4, 4, 4
};



LiquidCrystal lcd(4,5,6,7,8,9); // pins for RS, E, DB4, DB5, DB6, DB7;

void setup() {
pinMode(11, OUTPUT);  
pinMode(2, INPUT);
pinMode(10, OUTPUT);
Serial.begin(9600);

lcd.begin(16,2); //initialize LCD interface & send width/height

lcd.clear(); //clear screen

 const int buttonPin = 2;
 int Buzzer1 = 11;
 int buttonState =0;
 int size = sizeof(melody) / sizeof(int);
for (int thisNote = 0; thisNote < size; thisNote++) {

   // to calculate the note duration, take one second
   // divided by the note type.
   //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
   int noteDuration = 1000 / noteDurations[thisNote];
   tone(11, melody[thisNote], noteDuration);

   // to distinguish the notes, set a minimum time between them.
   // the note's duration + 30% seems to work well:
   int pauseBetweenNotes = noteDuration * 1.30;
   delay(pauseBetweenNotes);
   // stop the tone playing:
   noTone(11);
 }
}


void loop() {

 lcd.setCursor(1,0); //Sets Location with indexing starts at 0

lcd.print("Please Insert!"); //What do you want to say?

lcd.setCursor(4,1); //Sets Location with indexing starts at 0

lcd.print("Payment"); //What do you want to say?

 for (int positionCounter = 0; positionCounter < 16; positionCounter++) 


 Serial.println(button);
 
 switch (state)
 {
   case 0:
   digitalWrite(10, LOW);
   break;
   case 1:
   digitalWrite(10, HIGH);
   break;
   case 2:
   digitalWrite(10, LOW);
   break;
   case 3:
   digitalWrite(10, HIGH);
   break;
   case 4:
   digitalWrite(10,LOW);
   break;
   case 5:
   digitalWrite(10,HIGH);
   break;
   default:
   digitalWrite(10, LOW);
   break;
 }

 int buttonState = digitalRead(2);

Serial.println(button);

 switch (state)
 {
   case 0:
   digitalWrite(11, LOW);
   break;
   case 1:
   digitalWrite(11 , HIGH);
   break;
   case 2:
   digitalWrite(11, LOW);
   break;
   case 3:
   digitalWrite(11, HIGH);
   break;
   case 4:
   digitalWrite(11,LOW);
   break;
   case 5:
   digitalWrite(11,HIGH);
   break;
   default:
   digitalWrite(11, LOW);
   break;
 }

button = digitalRead(2);
 switch (state)
 {
   case 0:
     if (button == 1)
     {nxtState = 1;}
     else
     {nxtState = 0;}
     break;
     
   case 1:
     if (button == 0)
     {nxtState = 1;}
     else
     {nxtState = 2;}
     break;
     
   case 2:
     if (button == 1)
     {nxtState = 3;}
     else
     {nxtState = 2;}
     break;
     
   case 3:
     if (button == 1)
     {nxtState = 4;}
     else
     {nxtState = 3;}
     break;

   case 4:
   if (button == 1)
   {nxtState = 5;}
   else
   {nxtState = 4;}
   break;

   case 5:
   if(button == 1)
   {nxtState = 5;}
   else
   {nxtState = 0;}
   break;  
     
   default:
     {state = 0;}
     break;
     
}
state = nxtState;
}

Please put your code in its own window as seen in other posts. This can be done by placing     [code]  and [/code]  around the code or use the </> icon. This makes it easier for others to read.

How to use this forum

You can edit the original post.

Weedpharma

Your 3 switch statements all have the same variable (state) which is both confusing and prone to errors.
Give each switch an individual variable.
Your first two switch statements can both be changed to:

if (state is even){
  do something;
}
else {
  do something else;
}

I'll let you find out how to determine if a variable is odd or even. I'll not do your homework for you.