//Constants don't change
const int returnButton = 13; //Orange button to return coins
const int selectButton = 12; //Putple button to select options
const int scrollButton = 11; //Pink button to scroll through options
//The selection number, varies 0 - 5 (6 choices)
int selection = 0;
// used for dollar / cent selection
bool dollCentSelection = false; // false= dollars
int lastScrollState = 1; // < with INPUT_PULLUP the pin starts HIGH
int lastSelectState = 1; // < with INPUT_PULLUP the pin starts HIGH
int lastReturnState = 1; // < with INPUT_PULLUP the pin starts HIGH
int credit = 0; //The amount of money the user put into the machine
void selectSelect() {
int btnState = digitalRead(selectButton); // read the button pin
if (btnState != lastSelectState) { // see if it changed
lastSelectState = btnState; // remember where it is
if (btnState == LOW) { // if LOW it is pressed
Serial.println("Select pressed");
Serial.print("You have chosen ");
Serial.println(selection? "dollars" : "cents");
} else { // else it's HIGH, it was released
Serial.println("Select released");
}
delay(20); // for debounce
}
}
void returnSelect() {
int btnState = digitalRead(returnButton); // read the button pin
if (btnState != lastReturnState) { // see if it changed
lastReturnState = btnState; // remember where it is
if (btnState == LOW) { // if LOW it is pressed
Serial.println("Return pressed");
if (credit == 0) {
Serial.println("Nothing to return");
} else {
Serial.print("We owe you ");
Serial.println(credit);
}
} else { // else it's HIGH, it was released
Serial.println("Return released");
}
delay(20); // for debounce
}
}
//scrolls through the options of paying with coins or bills
void dollCentSelect() { // < "int" here means "this function returns an integer"
// read the pushbutton input pin
int scrollState = digitalRead(scrollButton); // < EDIT
// compare the buttonState to its previous state
if (scrollState != lastScrollState) {
// save the new state right when it's detected
// save the current state as the last state, for
// the next time through the loop
lastScrollState = scrollState;
// if the state has changed, increment the counter
if (scrollState == LOW) { // went from off to on
selection = ! selection;
Serial.println("Doll / Cent pressed");
Serial.println(selection? "Dollars" : "Cents");
//Serial.println(selection);
//selection = selection + 1; // print the selection before you increment it
//if (selection > 1) selection = 0;
} else { // went from on to off
Serial.println("Doll / Cent released");
}
// delay a little bit to avoid debouncing
delay(20); // Wait for 5 millisecond(s)
}
}
void setup() {
Serial.begin(9600);
// use the constants defined for each pin
pinMode(returnButton, INPUT_PULLUP);
pinMode(selectButton, INPUT_PULLUP);
pinMode(scrollButton, INPUT_PULLUP);
}
void loop() {
if (digitalRead(returnButton) == LOW) {
returnSelect();
} else if (digitalRead(selectButton) == LOW) {
selectSelect();
} else if (digitalRead(scrollButton) == LOW) {
dollCentSelect();
}
}