int mvm = 1; // cointaster
int mv1 = 2; // choiseswitch cabin 1
int mv2 = 3; // choiseswitch cabin 2
int mv3 = 4; // choiseswitch cabin 3
int sch1 = 5; // pulseswitch cabin 1
int sch2 = 6; // pulseswitch cabin 2
int sch3 = 7; // pulseswitch cabin 3
int lamp1 = 8; // indication-led shower 1
int lamp2 = 9; // indication-led shower 2
int lamp3 = 10; // indication-led shower 3
int klep1 = 11; // valve cabin 1
int klep2 = 12; // valve cabin 2
int klep3 = 13; // valve cabin 3
int state1 = LOW; // the current state of the output pin
int reading1; // the current reading from the input pin
int previous1 = LOW; // the previous reading from the input pin
int state2 = LOW; // the current state of the output pin
int reading2; // the current reading from the input pin
int previous2 = LOW; // the previous reading from the input pin
int state3 = LOW; // the current state of the output pin
int reading3; // the current reading from the input pin
int previous3 = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time1 = 0; // the last time the output pin was toggled
long debounce1 = 1000; // the debounce time, increase if the output flickers
long time2 = 0; // the last time the output pin was toggled
long debounce2 = 1000; // the debounce time, increase if the output flickers
long time3 = 0; // the last time the output pin was toggled
long debounce3 = 1000; // the debounce time, increase if the output flickers
long starttime1; // starttime coin-detection choiseswitch cabin 1
long starttime2; // starttime coin-detection choiseswitch cabin 1 2
long starttime3; // starttime coin-detection choiseswitch cabin 1 3
long currenttime; // current time of the Arduino
boolean set1; // variable for (true/false) coin-detection cabin 1
boolean set2; // variable for (true/false) coin-detection cabin 2
boolean set3; // variable for (true/false) coin-detection cabin 3
boolean shower1; // variable for (true/false) opening/closing valve on pulseswitch 1
boolean shower2; // variable for (true/false) opening/closing valve on pulseswitch 2
boolean shower3; // variable for (true/false) opening/closing valve on pulseswitch 3
long timetoshower = 180000; //standard showertime (this value + current value on influence of inserting coin)
long showertime1 = 0; // variable for showertime of shower 1 (depends on the amount of coins)
long showertime2 = 0; // variable for showertime of shower 2 (depends on the amount of coins)
long showertime3 = 0; // variable for showertime of shower 3 (depends on the amount of coins)
int mvm_debounce = 1000; // the time to wait to let the user insert more coins
long mvm_time1 = 0;
long mvm_time2 = 0;
long mvm_time3 = 0;
boolean insert_coin1 = false;
boolean insert_coin2 = false;
boolean insert_coin3 = false;
void setup()
{
pinMode(mvm, INPUT);
pinMode(mv1, INPUT);
pinMode(mv2, INPUT);
pinMode(mv3, INPUT);
pinMode(sch1, INPUT);
pinMode(sch2, INPUT);
pinMode(sch3, INPUT);
pinMode(klep1, OUTPUT);
pinMode(klep2, OUTPUT);
pinMode(klep3, OUTPUT);
pinMode(lamp1, OUTPUT);
pinMode(lamp2, OUTPUT);
pinMode(lamp3, OUTPUT);
}
void loop()
{
if (digitalRead(mv1) == HIGH) // mv1
{
insert_coin1 = false;
if (digitalRead(mvm) == HIGH && millis() - mvm_time1 > mvm_debounce)
{
insert_coin1 = true;
if (insert_coin1)
{
showertime1 = showertime1 + timetoshower;
insert_coin1 = false;
}
if (showertime1 > 0)
{
digitalWrite(lamp1, LOW);
digitalWrite(klep1, LOW);
set1 = true;
starttime1 = millis();
insert_coin1 = false;
}
mvm_time1 = millis();
}
}
if (digitalRead(mv2) == HIGH) // mv2
{
insert_coin2 = false;
if (digitalRead(mvm) == HIGH && millis() - mvm_time2 > mvm_debounce)
{
insert_coin2 = true;
if (insert_coin2)
{
showertime2 = showertime2 + timetoshower;
insert_coin2 = false;
}
if (showertime2 > 0)
{
digitalWrite(lamp2, LOW);
digitalWrite(klep2, LOW);
set2 = true;
starttime2 = millis();
insert_coin2 = false;
}
mvm_time2 = millis();
}
}
if (digitalRead(mv3) == HIGH) // mv3
{
insert_coin3 = false;
if (digitalRead(mvm) == HIGH && millis() - mvm_time3 > mvm_debounce)
{
insert_coin3 = true;
if (insert_coin3)
{
showertime3 = showertime3 + timetoshower;
insert_coin3 = false;
}
if (showertime3 > 0)
{
digitalWrite(lamp3, LOW);
digitalWrite(klep3, LOW);
set3 = true;
starttime3 = millis();
insert_coin3 = false;
}
mvm_time3 = millis();
}
}
if (set1)
{
currenttime = millis();
if (currenttime - starttime1 < showertime1)
{
shower1 = true;
digitalWrite(lamp1, HIGH);
}
else
{
digitalWrite(lamp1, LOW);
digitalWrite(klep1, LOW);
state1 = LOW;
shower1 = false;
set1 = false;
insert_coin1 = false;
showertime1 = 0;
}
}
if (set2)
{
currenttime = millis();
if (currenttime - starttime2 < showertime2)
{
shower2 = true;
digitalWrite(lamp2, HIGH);
}
else
{
digitalWrite(lamp2, LOW);
digitalWrite(klep2, LOW);
state2 = LOW;
shower2 = false;
set2 = false;
insert_coin2 = false;
showertime2 = 0;
}
}
if (set3)
{
currenttime = millis();
if (currenttime - starttime3 < showertime3)
{
shower3 = true;
digitalWrite(lamp3, HIGH);
}
else
{
digitalWrite(lamp3, LOW);
digitalWrite(klep3, LOW);
state3 = LOW;
shower3 = false;
set3 = false;
insert_coin3 = false;
showertime3 = 0;
}
}
if (shower1)
{
reading1 = digitalRead(sch1);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading1 == HIGH && previous1 == LOW && millis() - time1 > debounce1) {
if (state1 == HIGH)
state1 = LOW;
else
state1 = HIGH;
time1 = millis();
}
digitalWrite(klep1, state1);
previous1 = reading1;
}
if (shower2)
{
reading2 = digitalRead(sch2);
if (reading2 == HIGH && previous2 == LOW && millis() - time2 > debounce2) {
if (state2 == HIGH)
state2 = LOW;
else
state2 = HIGH;
time2 = millis();
}
digitalWrite(klep2, state2);
previous2 = reading2;
}
if (shower3)
{
reading3 = digitalRead(sch3);
if (reading3 == HIGH && previous3 == LOW && millis() - time3 > debounce3) {
if (state3 == HIGH)
state3 = LOW;
else
state3 = HIGH;
time3 = millis();
}
digitalWrite(klep3, state3);
previous3 = reading3;
}
}