// https://wokwi.com/projects/419072038170188801
# include <ezButton.h>
int balance;
// water - do we want it? is it aready running?
bool wantsWater;
bool isWatering;
const byte waterRequest = 6;
const byte waterControl = 7;
ezButton coinButton(2), waterButton(3);
unsigned long now; // the time. always.
void setup() {
Serial.begin(115200);
Serial.println("no free water.\n");
coinButton.setDebounceTime(20);
waterButton.setDebounceTime(20);
pinMode(waterControl, OUTPUT);
pinMode(waterRequest, OUTPUT);
}
// loop calls the version we are working on, which should alway run
// each subsequent version builds on success
void loop()
{
loop3();
}
void loop3() {
now = millis();
static int printedBalance = -1;
if (printedBalance != balance) {
Serial.print("balance = ");
Serial.println(balance);
printedBalance = balance;
}
if (isWatering) {
static unsigned long lastDebit;
if (now - lastDebit > 777) {
balance -= 3;
if (balance < 0) {
balance = 0; // well, there is some lefover money
Serial.println(". INSERT MORE COINS");
wantsWater = false; // who cares what this deadbeat wants?
isWatering = false;
}
lastDebit = now;
}
}
// input. let ezButton have a lok at the buttons
coinButton.loop();
waterButton.loop();
if (coinButton.isPressed()) {
balance += 10;
}
bool waterInput = waterButton.isPressed();
if (waterInput) {
if (isWatering) {
wantsWater = !wantsWater;
}
else {
if (balance) {
wantsWater = true;
isWatering = true;
}
else {
wantsWater = false;
Serial.println("INSERT COINS");
}
}
}
isWatering = wantsWater;
// OUTPUT adjust status LEDs and turn on or off the water
digitalWrite(waterRequest, wantsWater ? HIGH : LOW);
digitalWrite(waterControl, isWatering);
}
void loop2() {
now = millis();
static int printedBalance = -1;
if (printedBalance != balance) {
Serial.print("balance = ");
Serial.println(balance);
printedBalance = balance;
}
// input. let ezButton have a lok at the buttons
coinButton.loop();
waterButton.loop();
if (coinButton.isPressed()) {
balance += 10;
}
bool waterInput = waterButton.isPressed();
if (waterInput) {
if (isWatering) {
wantsWater = !wantsWater;
}
else {
if (balance) {
wantsWater = true;
isWatering = true;
}
else {
wantsWater = false;
Serial.println("INSERT COINS");
}
}
}
isWatering = wantsWater;
// OUTPUT adjust status LEDs and turn on or off the water
digitalWrite(waterRequest, wantsWater ? HIGH : LOW);
digitalWrite(waterControl, isWatering);
}
void loop1() {
now = millis();
static int printedBalance = -1;
if (printedBalance != balance) {
Serial.print("balance = ");
Serial.println(balance);
printedBalance = balance;
}
// input. let ezButton have a lok at the buttons
coinButton.loop();
waterButton.loop();
if (coinButton.isPressed()) {
balance += 10;
}
if (waterButton.isPressed())
wantsWater = !wantsWater;
// OUTPUT adjust status LEDs and turn on or off the water
digitalWrite(waterRequest, wantsWater ? HIGH : LOW);
}
void loop0() {
now = millis();
static int printedBalance = -1;
if (printedBalance != balance) {
Serial.print("balance = ");
Serial.println(balance);
printedBalance = balance;
}
// input. let ezButton have a lok at the buttons
coinButton.loop();
waterButton.loop();
if (coinButton.isPressed()) {
balance += 10;
}
}