My program is not exiting one while loop in a perticular fuction of my code. For some reason it appears that the digitalread on the button pin is not being called. I am totally at a loss as to what might/would/could be the culprit here, because if I where to create a new sketch with the code from the function with this while loop the code works properly.
The while loop that this issue is located in is the function Dispense which is just above my main loop
*Edit: I removed a digialread for buttonstate when oritionally posting the code cuz of several serialprint commands. I have put it back in.
/*
Soda-Pop Dispenser Machine V 1.8
By:
Geoffrey Andreychuk
July 30th, 2022
*/
// Including additional required libraries:
#include <LiquidCrystal_I2C.h>
#include <EncoderButton.h>'
#include <Wire.h>
EncoderButton eb1(2, 3, 4); // Declaring the Rotary Encoder & Button along with the Assigned Pins used Encoder+button: EncoderButton(byte encoderPin1, byte encoderPin2, byte switchPin);
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 20 chars and 4 line display
int ButtonPin = 7; // pin that the push button is assigned to to dispense the drink
int ButtonState = 1; // the state of the button that the user presses to dispense drink
int Solenoid = 0; // soleenoid to open the valve to dispence beverage
int Pump = 0; // to turn on or off the pump to dispense syrup
bool IsClicked = false;
int DrinkPointer = 5; // a variable to point to the current position in the Drink List
int TotalNumDrink = 10; // variable to hold the value of Drink Options
int DrinkSelection = 0; // variable to point to the users selected drink option to make the drink
int Increment; // variable to hold a + for CW and - for a CCW turn of Rotary Encoder
unsigned long IdleTime = 15000; // a timer fuction to be used to clear any selected options and return program to main selection screen
unsigned long EventInterval = 12000;
unsigned long PreviousTime = 0;
unsigned long CurrentTime;
char *DrinkChoices[] = {"0.", " Dr. Pepper", "Grape Crush", "Orange Crush", "Cream Soda", " Iced Tea", "Soda Water", " Coke", "Diet Coke", "Root Beer", " Sprite"};
void setup()
{
//Pump A
#define inA 22
//Pump B
#define inB 23
//Pump C
#define inC 24
//Pump D
#define inD 25
//Pump E
#define inE 26
//Pump F
#define inF 27
//Pump G
#define inG 28
//Pump H
#define inH 29
//Relays to control Solenoid Valves 13
#define Sol1 32
#define Sol2 33
#define Sol3 34
//Set all pump and Solenoid pins to outputs
pinMode(inA, OUTPUT);
pinMode(inB, OUTPUT);
pinMode(inC, OUTPUT);
pinMode(inD, OUTPUT);
pinMode(inE, OUTPUT);
pinMode(inF, OUTPUT);
pinMode(inG, OUTPUT);
pinMode(inH, OUTPUT);
pinMode(Sol1, OUTPUT);
pinMode(Sol2, OUTPUT);
pinMode(Sol3, OUTPUT);
//Sets button pin as input
pinMode(ButtonPin, INPUT_PULLUP);
Serial.begin(9600);
lcd.init(); // initialize the display
lcd.backlight();
IdleMenu();
//Link the event(s) to your function
eb1.setClickHandler(onEb1Clicked);
eb1.setEncoderHandler(onEb1Encoder);
eb1.setIdleHandler(onEb1Idle);
CurrentTime = millis ();
}
void onEb1Encoder(EncoderButton& eb)
{
}
void ResetorIdle()
{
IsClicked = false;
DrinkSelection = 4; // To ensure that nothing is accidently dispensed till a selection is actually made
Pump = 0; // Sets the enable pin for the previously selected pump to null
Solenoid = 0; // Sets the previously selected solenoid valve back to null
}
void onEb1Clicked(EncoderButton& eb)
{
DrinkSelection = DrinkPointer; // to ensure that the selected drink is not changed till either drink is poured or idle timer timeout is reached
IsClicked = true;
PlaceGlass();
PourDrink();
ResetorIdle();
}
void onEb1Idle(EncoderButton& eb)
{
// ResetorIdle();
// IdleMenu();
}
void IdleMenu() // Prints the idle screen message to the display
{
lcd.clear();
lcd.setCursor(0, 0); // Sets the cursor to the first row
lcd.print("Drink Selection Menu"); // set the cursor to line 1 which is the second row
lcd.setCursor(2, 1);
lcd.print("Rotate Knob Left"); // print to the second line
lcd.setCursor(2, 2);
lcd.print("or Right & Press"); // print to the fourth line
lcd.setCursor(0, 3);
lcd.print("to make Selection!!");
}
void SelectionMenu()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("<-----------------");
lcd.setCursor(5, 1);
lcd.print(DrinkChoices[DrinkPointer]);
lcd.setCursor(3, 2);
lcd.print("---------------->");
lcd.setCursor(0, 3);
lcd.print("Press knob to Select");
}
void UpdatePointer()
{
Increment = (eb1.position());
DrinkPointer = DrinkPointer + Increment;
eb1.resetPosition();
if (DrinkPointer > TotalNumDrink) // Catches a positive increment overturn and wrap around
{
DrinkPointer = 1;
return;
}
if (DrinkPointer < 1) // Catches a negative increment overturn and wrap around
{
DrinkPointer = TotalNumDrink;
return;
}
}
void PlaceGlass() // Function to tell the user which side to place glass
{ // cleans up the screen before letting user know where to put glass
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("Place Glass");
lcd.setCursor(2, 1);
if (DrinkSelection >= 1 && DrinkSelection <= 7)
{
lcd.print("Right --------->");
}
else if (DrinkSelection > 7 && DrinkSelection <= 10)
{
lcd.print("<---------- Left");
}
lcd.setCursor(1,2);
lcd.print("Press & Hold Button");
lcd.setCursor(0,3);
lcd.print("to dispense Pop");
}
void ErrorMessage()
{
lcd.clear();
lcd.setCursor(6,0);
lcd.print("Error!!");
lcd.setCursor(0,1);
lcd.print("Invalid Selection");
lcd.setCursor(0,2);
lcd.print("Or Out of Syrup");
lcd.setCursor(2,3);
lcd.print("Sorry, Not Sorry!!");
}
void PourDrink()
{
switch (DrinkSelection)
{
case 0:
ErrorMessage();
break;
case 1: // Selection Dr. Pepper
Pump = inA;
Solenoid = Sol1;
Dispense();
break;
case 2: // Selection Grape Crush
Pump = inB;
Solenoid = Sol1;
Dispense();
break;
case 3: // Selection Orange Crush
Pump = inC;
Solenoid = Sol1;
Dispense();
break;
case 4: // Selection Cream Soda
Pump = inD;
Solenoid = Sol1;
Dispense();
break;
case 5: // Selection Iced Tea
Pump = 0;
Solenoid = Sol3;
Dispense();
break;
case 6: // Selection Soda Water
Pump = 0;
Solenoid = Sol1;
Dispense();
break;
case 7: // Selection Coke
// Pump = inE;
// Solenoid = Sol2;
// Dispense();
ErrorMessage();
break;
case 8: // Selection Diet Coke
// Pump = inF;
// Solenoid = Sol2;
// Dispense();
ErrorMessage();
break;
case 9: // Selection Sprite
// Pump = inG;
// Solenoid = Sol2;
// Dispense();
ErrorMessage();
break;
case 10: // Selection Root Beer
// Pump = inH;
// Solenoid = Sol2;
// Dispense();
ErrorMessage();
break;
}
}
void Dispense()
{
ButtonState = digitalRead(ButtonPin);
while (IsClicked == true)
{
if (ButtonState == 0)
{
while (ButtonState == 0)
{
digitalWrite(Pump, 1); // turn on pump
digitalWrite(Solenoid, 1); // turn on Solenoid
ButtonState = digitalRead(ButtonPin); //This appears to be not getting checked!!!!!!
}
IsClicked = false;
break;
}
}
}
void loop()
{
CurrentTime = millis();
eb1.update();
if (eb1.position() != 0)
{
UpdatePointer();
SelectionMenu();
PreviousTime = CurrentTime;
}
if (CurrentTime - PreviousTime >= IdleTime)
{
ResetorIdle();
IdleMenu();
PreviousTime = CurrentTime;
}
}