This is my bathroom rig. It annoyingly advocates sanitation by locking the door when it senses a flush until it determines that the occupant has washed his/her hands.
The black button on the right is the flush button. Once that is pressed, the “wash your hands” LED blinks and the door locks via a solenoid deadbolt (behind protodoor). The occupant’s intent to leave is signified by the doorknob switch (optimally a proximity sensor so no touching is necessary). Initially it would re-remind the occupant to “wash your hands.” If the water, which is represented by the potentiometer faucet, is turned more than a specified min threshold, the system registers that the occupant has rinsed his/her hands, but not washed them. Pushing the which soap button tells the system that soap has been dispensed, but unless the water is running after that, it determines that the occupant is trying to trick the system! Once the occupant has properly washed, the door unlocks and the “Thanks” LED blinks.
wash your hands!
int flushpin = 12;
int soappin = 11;
int waterpin = 5;
int lockpin = 8;
int doorpin = 2;
int thankspin = 7;
int washpin = 6;
int rinsepin = 4;
int fakepin = 3;
int state = 0; // 0=off, 1=on, 2=rinse, 3=soap
void setup()
{
Serial.begin(9600);
pinMode(flushpin, INPUT);
pinMode(soappin, INPUT);
pinMode(waterpin, INPUT);
pinMode(lockpin, OUTPUT);
pinMode(doorpin, INPUT);
pinMode(thankspin, OUTPUT);
pinMode(washpin, OUTPUT);
pinMode(rinsepin, OUTPUT);
pinMode(fakepin, OUTPUT);
digitalWrite(lockpin, LOW);
digitalWrite(thankspin, LOW);
digitalWrite(washpin, LOW);
digitalWrite(rinsepin, LOW);
digitalWrite(fakepin, LOW);
}
void loop()
{
if(digitalRead(flushpin)==HIGH) {blink(washpin); state=1;}
if(state>0) {digitalWrite(lockpin, HIGH);}
if(state==1 && digitalRead(doorpin)==HIGH) {blink(washpin);}
if(state==1 && analogRead(waterpin)<800) {state=2;}
if(state==1 && digitalRead(soappin)==HIGH) {state=3;}
if(state==2 && digitalRead(doorpin)==HIGH) {blink(rinsepin); state=1;}
if(state==2 && digitalRead(soappin)==HIGH) {state=3;}
if(state==3 && digitalRead(doorpin)==HIGH) {blink(fakepin); state=1;}
if(state==3 && analogRead(waterpin)<800)
{
digitalWrite(lockpin, LOW);
blink(thankspin);
state=0;
}
}
void blink(int pin)
{
digitalWrite(pin, HIGH); delay(100);
digitalWrite(pin, LOW); delay(100);
digitalWrite(pin, HIGH); delay(100);
digitalWrite(pin, LOW); delay(100);
digitalWrite(pin, HIGH); delay(100);
digitalWrite(pin, LOW);
}