boolean buttonPressed = false;
int count = 0;
int totalCount = 0;
int cents = 0;
void setup() {
Serial.begin(19200);
attachInterrupt(digitalPinToInterrupt(2), buttonCB, FALLING);
attachInterrupt(digitalPinToInterrupt(3), coinAcceptorCB, FALLING);
pinMode(4, OUTPUT);
}
void buttonCB() {
Serial.println("Button pressed");
buttonPressed = true;
}
void coinAcceptorCB() {
count = count + 1;
cents = 10 * count;
Serial.print("Count = ");
Serial.println(count);
Serial.print(cents);
Serial.println(" cents received. ");
}
void loop() {
if (buttonPressed) {
if (5 <= totalCount && totalCount < 10) {
// DISPENSE WATER
digitalWrite(4, HIGH);
delay(5000);
digitalWrite(4, LOW);
buttonPressed = false;
// SHOW RECEIPT
Serial.print("Total count: ");
Serial.println(totalCount);
totalCount = 0;
} else {
Serial.println("Insufficient. Minimum is 50 Cents.");
buttonPressed = false;
}
}
delay(1000);
totalCount = totalCount + count;
count = 0;
}
Comments