Hello. I am pretty new to arduino and was wondering if someone could help me out. I am currently trying to do a "vending machine" project for a class, and I am having issues with some code to display a menu. I am trying to display the menu and allow the user to enter values 1-4 to add what they would like until they enter a 5 to then go on to purchase the items. However, whenever I open the serial console, the menu display starts looping repeatedly. I am still able to enter in numbers as it looping, and the menu does stop when I hit a 5. How can I improve my code so that the menu only displays with every entry? Here is my code. (Please ignore the code after telling the user what they owe, that is something I still need to improve).
int ledPin = 13;
int inputPin1 = 3;
int inputPin2 = 2;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(inputPin1, INPUT);
pinMode(inputPin2, INPUT);
Serial.begin(9600);
}
int value = 0;
int selection;
float total = 0;
float payment = 0;
void loop()
{
while (selection != 5)
{
Serial.print("Menu\n");
Serial.print("1. Banana ($.5)\n");
Serial.print("2. Apple ($.99)\n");
Serial.print("3. Grapes ($1.25)\n");
Serial.print("4. Steak ($2.50)\n");
Serial.print("5. Done\n");
if (Serial.available () > 0)
selection = Serial.parseInt();
if(selection == 1)
{
total += .5;
}
else if (selection == 2)
{
total += .99;
}
else if (selection == 3)
{
total += 1.25;
}
else if (selection == 4)
{
total += 2.5;
}
}
Serial.print("You owe $");
Serial.print(total);
Serial.print("\nPlease pay for your order\n");
Serial.print("(Top Button = $1)\n");
Serial.print("(Bottom Button = $0.50)\n");
*ignore below
while (payment < total)
{
if (digitalRead(inputPin1) == LOW)
{
payment += 1;
}
else if (digitalRead(inputPin2) == LOW);
{
payment += .5;
}
}
if (payment >= total)
{
digitalWrite(ledPin, HIGH);
Serial.print("Change: $");
Serial.print(payment - total);
}
Serial.end();
}