admin管理员组文章数量:1125936
I am trying to make an automatic handwash dispenser for a school project with - Arduino uno, a water pump and IR Sensor. Fundamentally it works fine. I know nothing about coding, so i used Pictoblox. Now, i want the water pump to run only for 3 seconds once it starts. tried chatgpt and various codes but every solution just delays the response of the pump by 3 seconds after it gets turned on by IR. Help please.
This works fine but the pump runs continuously as long as IR is detecting something.
//This c++ code is generated by PictoBlox
void setup() { //put your setup code here, to run once:
pinMode(9, OUTPUT); pinMode(2, INPUT);
digitalWrite(9, false);
}
void loop() {
//put your main code here, to run repeatedly:
if(digitalRead(2)) {
delay(0.5 * 1000); while(undefined);
digitalWrite(9, true); }
else {
digitalWrite(9, false);
delay(.5 * 1000);
}
}
I am trying to make an automatic handwash dispenser for a school project with - Arduino uno, a water pump and IR Sensor. Fundamentally it works fine. I know nothing about coding, so i used Pictoblox. Now, i want the water pump to run only for 3 seconds once it starts. tried chatgpt and various codes but every solution just delays the response of the pump by 3 seconds after it gets turned on by IR. Help please.
This works fine but the pump runs continuously as long as IR is detecting something.
//This c++ code is generated by PictoBlox
void setup() { //put your setup code here, to run once:
pinMode(9, OUTPUT); pinMode(2, INPUT);
digitalWrite(9, false);
}
void loop() {
//put your main code here, to run repeatedly:
if(digitalRead(2)) {
delay(0.5 * 1000); while(undefined);
digitalWrite(9, true); }
else {
digitalWrite(9, false);
delay(.5 * 1000);
}
}
Share
Improve this question
edited Jan 9 at 8:41
hcheung
4,0143 gold badges15 silver badges27 bronze badges
asked Jan 9 at 2:07
UdySnkrUdySnkr
111 bronze badge
New contributor
UdySnkr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
7
|
Show 2 more comments
1 Answer
Reset to default 1Reworking on your code I second the suggestions @Computable posted in the comments section. I would leave the else
-block in and also delay the re-read in the if
-block to avoid polling the input too often. Try and refactor your code to this:
void setup() {
pinMode(9, OUTPUT);
pinMode(2, INPUT);
digitalWrite(9, false);
}
void loop() {
if(digitalRead(2)) {
digitalWrite(9, true); // activate pump
delay(3000); // let the pump run for 3s
digitalWrite(9, false); // deactivate pump
while(digitalRead(2)) { // wait until hand is no longer close to IR sensor
delay(.5 * 1000);
}
}
else {
digitalWrite(9, false); // deactivate pump
delay(.5 * 1000); // This avoids looping too often (re-read IR sensor only every 0.5 seconds)
}
}
Please share the results!
本文标签: handwash dispenser with Arduino unoStack Overflow
版权声明:本文标题:handwash dispenser with Arduino uno - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736677985a1947282.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
undefined
, as used inwhile (undefined);
? – Thomas Matthews Commented Jan 9 at 2:18constexpr
identifiers instead of magic numbers. For example, "constexpr unsigned int SENSOR_PORT = 2u;
". The identifier allows you to change its value at only one location, rather than have to search through your code and changing every instance. – Thomas Matthews Commented Jan 9 at 2:27delay... while(undefined)
line, and then add (afterdigitalWrite(9,true)
)delay(3000);
,digitalWrite(9,false)
andwhile(digitalRead(2));
. The original else becomes unnecessary. – Computable Commented Jan 9 at 2:53