Yes/no condiment dispenser questionnaire with 16x2 Grove LCD screen

Hi there! I need help regarding this. Basically I am trying to programme a LCD screen with buttons for a condiment dispenser. Unfortunately, I am unable to continue with my code after it asks me for the portion size. For example, I click the 1 button for the ketchup to be dispensed. I then click the 1 button again for the small portion but it goes back to the ketchup part. I do not really know what went wrong with my code.

Another thing is after that, I also need help to go back to restart the loop as I am not too sure how to do that. The return function does not work too well. Thanks!

#include <Wire.h>
#include "rgb_lcd.h"

const int buttonPinOne = 8; // button 1
const int buttonPinTwo = 7; // button 2

int buttonOnePushCounter = 0;
int buttonOneState = 0;
int lastButtonOneState = 0;
int buttonTwoPushCounter = 0;
int buttonTwoState = 0;
int lastButtonTwoState = 0;

rgb_lcd lcd;
 
const int colorR = 255;
const int colorG = 255;
const int colorB = 255;

void setup() {
  pinMode(buttonPinOne, INPUT);
  pinMode(buttonPinTwo, INPUT);
  // message one
  lcd.begin(16, 2);
  lcd.setRGB(colorR, colorG, colorB);
  lcd.print("WWhat condiment do you want?");
  for (int positionCounter = 0; positionCounter < 23; positionCounter++) {
    // scroll one position left:
    lcd.scrollDisplayLeft();
    // wait a bit:
    delay(250);
  } // message two 
  delay(200);
  lcd.clear();
  lcd.print("PPress 1 for ketchup and 2 for chilli");
  for (int positionCounter = 0; positionCounter < 23; positionCounter++) {
    lcd.scrollDisplayLeft();
    delay(250);
  }
}

void loop() { {
  buttonOnePushCounter = 0;
  buttonTwoPushCounter = 0;
  buttonOneState = digitalRead(buttonPinOne);
  buttonTwoState = digitalRead(buttonPinTwo);
  if (buttonOneState != lastButtonOneState) {
    if (buttonOneState == HIGH) {
      buttonOnePushCounter++;
      Serial.println(buttonOnePushCounter);
    }
    else {
     Serial.println("");
    }
  }
  else if (buttonTwoState != lastButtonTwoState) {
    if (buttonTwoState == HIGH) {
      buttonTwoPushCounter++;
    }
    else {
      Serial.println("");
    }
  }
}
  if (buttonOnePushCounter == 1) { 
    
    buttonOnePushCounter = 0;
    buttonOneState = digitalRead(buttonPinOne);
    buttonTwoState = digitalRead(buttonPinTwo);
    if (buttonOneState != lastButtonOneState) {
      if (buttonOneState == HIGH) {
        buttonOnePushCounter++;
      }
      else {
        Serial.println("");
      }
    }
    else if (buttonTwoState != lastButtonTwoState) {
      if (buttonTwoState == HIGH) {
        buttonTwoPushCounter++;
      }
      else {
        Serial.println("");
      }
    }
    lcd.clear();
    lcd.begin(16, 2);
    lcd.setRGB(colorR, colorG, colorB);
    lcd.print("Ketchup selected");
    delay(2000);
    lcd.clear();
    lcd.print("WWhat portion would you like?");
    for (int positionCounter = 0; positionCounter < 24; positionCounter++) {
    // scroll one position left
    lcd.scrollDisplayLeft();
    // wait a bit:
    delay(250);
    }
    delay(100);
    lcd.clear();
    lcd.print("PPress 1 for small and 2 for large");
    for (int positionCounter = 0; positionCounter < 24; positionCounter++) {
      lcd.scrollDisplayLeft();
      delay(250); 
    }
    
    
    if (buttonOnePushCounter == 1) {  
      lcd.clear();
      lcd.begin(16, 2);
      lcd.setRGB(0, 255, 0);
      lcd.print("Dispensing...");
      delay(10000);
      lcd.clear();
      lcd.setRGB(255, 255, 255);
      lcd.print("Dispensed!");
      return;
    }
    else if (buttonTwoPushCounter == 1) {
      lcd.clear();
      lcd.begin(16, 2);
      lcd.setRGB(0, 255, 0);
      lcd.print("Dispensing...");
      delay(15000);
      lcd.clear();
      lcd.setRGB(255, 255, 255);
      lcd.print("Dispensed!");
      return;
    }
  }
  else if (buttonTwoPushCounter == 1) {
   lcd.clear();
   lcd.begin(16, 2);
   lcd.setRGB(colorR, colorG, colorB);
   lcd.print("Chilli selected");
   delay(2000);
   lcd.clear();
   lcd.print("WWhat portion would you like?");
   for (int positionCounter = 0; positionCounter < 24; positionCounter++) {
    // scroll one position left
    lcd.scrollDisplayLeft();
    // wait a bit:
    delay(250);
   }
   delay(100);
   lcd.clear();
   lcd.print("PPress 1 for small and 2 for large");
   for (int positionCounter = 0; positionCounter < 24; positionCounter++) {
    lcd.scrollDisplayLeft();
    delay(250); 
    }
    
   if (buttonOnePushCounter == 1 && buttonTwoPushCounter == 1) {
    lcd.clear();
    lcd.begin(16, 2);
    lcd.setRGB(0, 255, 0);
    lcd.print("Dispensing...");
    delay(10000);
    lcd.clear();
    lcd.setRGB(255, 255, 255);
    lcd.print("Dispensed!");
    return;
   }
   else if (buttonTwoPushCounter == 2) {
    lcd.clear();
    lcd.begin(16, 2);
    lcd.setRGB(0, 255, 0);
    lcd.print("Dispensing...");
    delay(15000);
    lcd.clear();
    lcd.setRGB(255, 255, 255);
    lcd.print("Dispensed!");
    return;
   }
   
      
  }
}


Welcome to the forum

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE

Your code is very complicated, and there is a lot of code repeated. It doesn't need to be.

You need to have a think about "state"... that is, where in the flow of dispensing a drink you are. You have 3 states in your setup.

1). Waiting for drink selection.
2). Waiting for size selection.
3). Dispensing the drink.

After this you repeat, and return to 1).

So with that in mind, the main part of your program is simplified to...



int8_t  state     = 1;       // 1 = waiting for selection, 2 = waiting for size, 3 = dispensing.

int8_t  selection = 0;       // 1 = ketchup, 2 = whatever
int8_t  drinkSize = 0;       // 1 regular, 2 = large
boolean dispensing = false;  // true when pouring a drink


void setup()
{
}


void loop()
{
  switch(state)
  {
    case 1:
      doSelection();
      if (selection > 0)   // Has a selection been made?
        state = 2;         // Move to next state.
      break;
        
    case 2:
      doDrinkSize();
      if (drinkSize > 0)   // Has a size been selected?
        state = 3;         // Move to next state.
      break;
    
    case 3:
      doDispense();        
      if (!dispensing)     // Has the drink finsihed being poured?
        state = 1;         // Start over. 
      break;
    }
}

void doSelection()
{
  // This is where you check buttons and set up variable selection (1 or 2).
}

void doDrinkSize()
{
  // This is where you check the buttons and set up variable drinkSize (1 or 2)
}

void doDispense()
{
  
  // Based on selection & drinkSize dispense the drink... set dispensing to true while dispensing.
  // When done set dispensing to false, selection to 0  & drinkSize to 0.
}
  
    buttonOneState = digitalRead(buttonPinOne);
    if (buttonOneState != lastButtonOneState)
    {
      if (buttonOneState == HIGH)
      {
        buttonOnePushCounter++;
      }
    }

You are comparing 'buttonOneState' to 'lastButtonOneState' to see if there is a change, but you don't seem to set lastButtonOneState anywhere.

void loop()
{
  {
    buttonOnePushCounter = 0;
    buttonTwoPushCounter = 0;

You are clearing the button push counters every time through loop(). I don't see how the counts are going to ever be higher than 1.

It looks like this line can't tell the difference between "Ketchup", "Large" (1, then 2) and "Chilli", "Small" (2, then 1). I think you need to keep those two states separate.

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