Coffee Machine Code

hello

i have just written a code to control a coffee machine ... not tested yet on Proteus or board

the system shall works as follows:

  • the system gives me 4 choices: 1: tea 2: coffee 3: tea with milk 4: coffee with milk
  • once i choose the drink i press # to start filling the cup
  • 3 valves are attached to A0, A1 and A2 pins
  • A0 represents tea, A1 represents coffee and A2 represents milk
  • if i press * any time the system shows me a message and i start over to choose

please review the code and let me know if you have any recommendations.

many thanks

Ahmed Darwish

CoffeeMachine.txt (3.34 KB)

Please stop cross-posting. Other thread removed.

Why are you using an analog pin for output rather than digital pin? You are using digitalWrite() on an analogue pin. Try using pins 1 2 and 3 instead.

Sporech:
Why are you using an analog pin for output rather than digital pin? You are using digitalWrite() on an analogue pin. Try using pins 1 2 and 3 instead.

Pin 1 is used by hardware serial. Not a good idea. There is nothing wrong with using an analog pin for digital output. I do it any time it's convenient for me.

If the user selects a choice which is not available, example 6, shouldn't he get an invalid choice message ?

Sporech:
Why are you using an analog pin for output rather than digital pin? You are using digitalWrite() on an analogue pin. Try using pins 1 2 and 3 instead.

There is nothing wrong with using digitalWrite() on analogue pins as long as the pinMode is set correctly. As for using digital pin 1, that is not generally a good habit to get into as it is used by hardware Serial.

As to the code itself, the first obvious comment would be the use of ints instead of bytes, not making these variables const and not using meaningful names for them

int controlPin1 = A1; //constants for Control Pin 1
int controlPin2 = A2; //constants for Control Pin 2
int controlPin3 = A3; //constants for Control Pin 3

As mentioned above, do not use pins 0 or 1 unless you know what you are doing

LiquidCrystal lcd(13, 12, 3, 2, 1, 0);

You are trying to use the LCD before setting it up

  DisplayChoicesScreen(); //display main screen
  lcd.begin(16, 2); // initialize the lcd
  int l;

Not a good choice for a variable name

        if (currentState == 2) {
          if (int(key) != 0) {
            if (key == '*') {
              CurrentChoiceValue[0] = '0';
              ShowCanceledChoice;
              DisplayChoicesScreen();
              currentState = 1;

You already know that (int)key is not zero and you have dealt with the case where key is '*' so will this code ever be executed ? This block of code is within the switch/case. Will any of it be executed ?

            }
          }
        }
    }
  }
}

This is not necessarily wrong but usually indicates that the code could be restructured to make it more readable.

if(itWorks && youLikeIt)
{
  Serial.println(F("Hooray"));
  whoTheHeckCaresWhatAnyoneElseThinks();
}

so... load it up and 1) see if it works and 2) verify that you like it!