I want to make a humidifer

I want to make a humidifer from 3d printer that will detect someone walking past then the humidifer will turn on for 10 seconds then turn off.

Ive found this code on ticker CAD

I know it is turning on a LED but I will later change it to trigger a humidifer

But what do i need to change in the code to make it stay on after someone is not in range

#define Trig 7
#define Echo 6
#define Led 8
void setup()
{
Serial.begin (9600);
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
pinMode(Led, OUTPUT);
}

void loop()
{
long width, distance;
digitalWrite(Trig, 0);
delayMicroseconds(2);
digitalWrite(Trig, 1);
delayMicroseconds(10);
digitalWrite(Trig, 0);
width = pulseIn(Echo, 1);
distance = width/58.2;
Serial.print(" Distance is: ");
Serial.print(distance);
Serial.println(" cm");
if (distance>100)
digitalWrite(Led,0);
else
digitalWrite(Led,1);
delay(100);
}

This is your distance check (and LED off). If the object is greater than 100cm, the LED is OFF. To keep the LED ON for a prescribed time, you can call the delay() function with a delayTime of your choosing...

  if (distance>100) {
    delay(delayTime):
    digitalWrite(Led,0);
  }

Please, format your code in your IDE and paste it in a code block.

1 Like

It will, of course, then be blind, so there's no reaction if they wander back into the zone. That will require a non-blocking timer running, so the timer can be reset if distance drops. Time for some headscratching for the OP - what is the real requirement?

I should have described that ("what would happen next"). Sorry.

No sorry required - I was more pointing out to our OP that we need more information - what's the goal here, a classroom exercise, or something that's truly useful? Sorry, I could have been more explicit, as well as making the usual reference to "How to...", which would also address the embedding code issue you ably pointed out.
HAND! (Have A Nice Day!)

Thx for your input, im trying to make a humidifier. My plan is to run the script on a ATtiny85. I want the unit to be left powered on, then when someone walks past the unit it would turn the humidifier on for 10 seconds then turn off.

And obviously reset the timer so that the unit would be continuously ready.

So a bit like one of those proximity air freshers.

Not my question. The scenario is: a person passes by; your timer is triggered. They leave the space, but before the timer runs down, they re-enter the space. Does the timer restart, or dumbly run to zero? Both possibilities are reasonable, it depends on intent.

The answer given by @xfpd runs to zero, then retriggers only if the person is present. If that's sufficient for you, we need go no further; if not, time to start working on some concepts.

So, which do you want?

That's how I interpreted it. So, something like:

#define Trig 7
#define Echo 6
#define Led 8
void setup()
{
  Serial.begin (9600);
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);
  pinMode(Led, OUTPUT);
}

void loop()
{
  static uint32_t lastDectionTime = millis() - 10000;
  long width, distance;
  digitalWrite(Trig, 0);
  delayMicroseconds(2);
  digitalWrite(Trig, 1);
  delayMicroseconds(10);
  digitalWrite(Trig, 0);
  width = pulseIn(Echo, 1);

  distance = width / 58.2;
  Serial.print(" Distance is: ");
  Serial.print(distance);
  Serial.println(" cm");
  uint32_t currentMillis = millis();
  if (distance <= 100) {
    lastDectionTime = currentMillis;
  }
  if (currentMillis - lastDectionTime < 10000) {
    digitalWrite(Led, 1);
  } else {
    digitalWrite(Led, 0);
  }
}

The timer would run to zero, then reset.

Then mark post #2 is your solution, and we're done. Please mark the topic solved, giving @xfpd the credit.

Thank you guys for your help.

1 Like