Go Down

Topic: Automatic Toilet Flusher (Read 3448 times) previous topic - next topic

Big Oil

#15
Oct 18, 2010, 08:01 pm Last Edit: Oct 18, 2010, 08:05 pm by Big_Oil Reason: 1
I've made another adjustment to the code.  If you're not using a capacitor to smooth out the data from the IR sensor it might randomly falsely trigger it, so now it will take 4 data samples and average them to make sure someone is actually standing there.  Right now there is a 1 second delay, then a 1 second sample time.  If the toilet is next to a bathtub you might want to make that triggering delay longer, because someone might stand there for 5 seconds before getting into the tub.  
If you haven't seen the pictures of the non-wooden apparatus at the bottom of page 1 check that out.
Wortlesoft- no misses yet.  Andy R, let me know how it goes.  You could also make a fun button with lights for the kids, or a clapper (clap twice).
Code: [Select]
#include <ServoTimer1.h>
ServoTimer1 servo1;

int sensor=0;
int sensorPin=5; //distance sensor on analog pin 5

int PreparingToFlush=0;
int sample1;
int sample2;
int sample3;
int sample4;

void setup() {
 Serial.begin(9600);          
 servo1.attach(10); //servo data line on pin 10
 servo1.write(3);
 
// digitalWrite(3,HIGH);
}


void loop() {
sensor=analogRead(sensorPin);

Serial.println(sensor);


if (PreparingToFlush==0){  
if (sensor > 90){ //if distance sensor detects someone
 delay(1000);  // wait
 sample1=analogRead(sensorPin); //take a sample of the data
 Serial.println("Sensing");
 delay(300);
 sample2=analogRead(sensorPin);
 delay(300);
 sample3=analogRead(sensorPin);
 delay(400);
 sample4=analogRead(sensorPin);
 
 sensor= (sample1 + sample2 + sample3 + sample4)/4;
 if (sensor > 90){ // check again to make sure someone is actually there
 PreparingToFlush=1;
 digitalWrite(13, HIGH);
 
 Serial.println(sensor);
 }
}
}

if (PreparingToFlush==1){ //if a person has been detected
 if (sensor < 60){ // if the person has now left
 delay(1000);
 sensor=analogRead(sensorPin);
 if (sensor < 60){
 Serial.println("Flushing");
 servo1.write(175); //FLUSH
 digitalWrite(13, LOW);
 delay(9000);
 servo1.write(3);
 delay(1000);
 
 PreparingToFlush=0;  //reset the trigger
}
}  
}  
 
 
 delay(10);
}

wortelsoft

#16
Oct 19, 2010, 04:28 pm Last Edit: Oct 19, 2010, 04:28 pm by wortelsoft Reason: 1
I saw the picture but didn't realise this was a new situation. That sure is a lot better.
I'm surprised that such a servo is powerfull enough.

paulof

LOL that is awesome... it made my day haha

Go Up