Given below is the code i am facing issue.I have project submission tomorrow, but my system is not working properly. I have used ultrasonic and PIR sensor with single channel relay and water pump. After sensing the movement the realy keeps turning on and off, making ticking noise and sometimes it goes on for few seconds. The bold text of code I am facing issue with.
code:
/* To allocate the Arduino board's pin to
trigger and echo pins of Ultrasonic sensor HC SR-04
define function is used
*/
#define triggerPin 3
#define echoPin 2
/* To perform the simulation of a pump, led is used.
When the hand will be closer to the sensor,
the led pin will be high and it will be on for 5 seconds
*/
#define ledPin 12
int sound = 250;
/* I have used also the PIR sensor with Ultrasonic sensor to detect human
hand better
for the inpur of PIR sensor, pin 8 is being used
*/
#define pirpin 8
void setup() {
Serial.begin (9600);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(pirpin, INPUT);
}
void loop() {
digitalWrite(ledPin, LOW);
long pulsetime, interval;
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pulsetime = pulseIn(echoPin, HIGH);
interval = pulsetime * 0.034 / 2; //using the formula of s= v*t
int sensorVal = digitalRead (pirpin);
Serial.println(sensorVal);
if ((sensorVal == HIGH) && (interval < 5))
{
Serial.println("Please keep your hands under, for 5 seconds");
digitalWrite(ledPin, HIGH); // turn the LED on
delay(15);
digitalWrite(ledPin, LOW); // turn the LED off
Serial.println("Your hands are sanitized succesfully");
delay (15);
}
if (sensorVal == LOW || interval > 60 || interval <= 0)
{
digitalWrite(ledPin, LOW);
Serial.println("Keep your hands closer for sanitization");
Serial.println("Thanks for your patience. Together we will beat Corona");
delay(500);
}
}
Serial.println("Please keep your hands under, for seconds");
digitalWrite(ledPin, HIGH); // turn the LED on
delay(15);
digitalWrite(ledPin, LOW); // turn the LED off
Serial.println("Your hands are sanitized succesfully");
delay (15);
Why do you keep turning the output on an off with such a short interval
Hello
I think there are more delay() functions in the sketch that block each other.
p.s.
Well I guess a collection of timers and a small FSM will be extremly usefull to reach the o.m. system function.
I am really thankful to you.
But if I increase delay time, relay turns the pump on for few seconds.
I just want the pump to on only for a fraction of second.
Please help me to correct that logic.
start of loop()
if a hand is detected
start the pump
save the value of millis() as the start time
end if
if millis() minus start time is greater than required period
stop the pump
end if
end of loop()
Hi,
greetings
I am making a automatic hand sanitizer system.
Does not help to proceed in your project.
If you want detailed help you have to provide your new attept to make it work.
Post your full sketch
You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
Just three steps
press Ctrl-T for autoformatting your code
do a rightclick with the mouse and choose "copy for forum"
paste clipboard into write-window of a posting
best regards Stefan