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
  • 1 For simplicity, I don't recommend mixing the C and C++ languages. The C++ language supports overloading of functions, inheritance and exceptions which make linking with C difficult. To make your project simpler, choose a single language and stick with it. (Adjust your language tags accordingly.) – Thomas Matthews Commented Jan 9 at 2:15
  • 1 Where's the definition of undefined, as used in while (undefined);? – Thomas Matthews Commented Jan 9 at 2:18
  • You may want to refactor the delay to the top or bottom of the loop. You have it in both the true and false sections of the comparison. Placing at the top or bottom will accomplish the same thing, but with only once occurrence. – Thomas Matthews Commented Jan 9 at 2:20
  • Since you tagged as C++, you should use some constexpr 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:27
  • 1 Logically you'd want upon digitalRead(2) becoming true to 1) assert pin 9 2) wait 3 seconds 3) deassert pin 9 4) wait for digitalRead(2) to become false 5) resume loop. So to achieve this get rid of the delay... while(undefined) line, and then add (after digitalWrite(9,true)) delay(3000); , digitalWrite(9,false) and while(digitalRead(2));. The original else becomes unnecessary. – Computable Commented Jan 9 at 2:53
 |  Show 2 more comments

1 Answer 1

Reset to default 1

Reworking 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