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;
}
}
}