Help debouncing input

Hi,
I’m about finished my espresso machine, and am just ironing out some kinks in the fill program. This component takes the signal from a level sensor in the boiler, and then decides whether or not to turn on the fill pump. I would like to debounce the filling to maybe 3 seconds to prevent the pump from just clicking on/off when the water is boiling during steaming. How could I achieve this? Is it as simple as adding a delay in the component?

void fill(){
  if(digitalRead(lowSwitch) == HIGH){
    digitalWrite(inletRelay, LOW);}
  if(digitalRead(lowSwitch)== LOW){
    digitalWrite(inletRelay, HIGH);}
}

Also, when the machine turns on, the pump always switches on for a second or so (even if the boiler is full), is there a way to prevent this?
Ethan

Look at how millis() is used to ensure a suitable interval between button presses in several things at a time. Your application just needs a longer interval.

...R

So, I have a level sensor which reads high/low, the state of this level sensor is to trigger a relay which controls a fill pump in a boiler. However, when the boiler is depressurized (like when steaming), the water begins to roll and if there is just the right (or i guess wrong in this case) amount of water in the boiler, the relay will switch on and off in quick succession which is not only annoying, but will also damage parts. I basically want to delay the turning on of the pump a second or two so that short incoming signals of 'low water level' are ignored. Unfortunately, simply adding a delay will not work in this case.
I tried this but it didn't work(the pump just never turned on):

void fill(){
  unsigned long tt=millis();
  bool fillPress=digitalRead(lowSwitch);
  if ((fillPress==HIGH) && ( (tt-lastFillPress) >500 ) ){
    lastFillPress=tt;
    digitalWrite(inletRelay, LOW);}
  if (digitalRead(lowSwitch==LOW)){
    digitalWrite(inletRelay, HIGH);}
}

attached is the rest of my code
Ethan

It would be helpful if you put every { and every } on their own lines and used Tools + Auto Format to fix that horrid indenting. Then, maybe I'd be able to read your code.

Well gosh, I am really so terribly sorry for having you folks see that horribly formatted bit of code. Hope this is good enough.

void fill(){
  unsigned long tt=millis();
  bool fillPress=digitalRead(lowSwitch);
  if ((fillPress==HIGH) && ( (tt-lastFillPress) >500 ) ){
    lastFillPress=tt;
    digitalWrite(inletRelay, LOW);
  }
  if (digitalRead(lowSwitch==LOW)){
    digitalWrite(inletRelay, HIGH);
  }
}

workinprogress6_with_new_fill.ino (14.2 KB)

Well gosh,

PaulS was total correct. And as you can’t bebothered to read the posting rules for this forum I won’t look at you snippet of code.

Mark

This looks like a near-duplicate of a previous thread: Help with debouncing!!!

christop:
This looks like a near-duplicate of a previous thread: Help with debouncing!!!

It is, yes. I couldn't find it when I searched so I created a new one. If a moderator would merge them, that would be great.

Merged.

Hope this is good enough.

No. Every { goes on its own line.

You are debouncing the conversion to HIGH but not the conversion to LOW. Why not?

void fill(){
 unsigned long tt=millis();
 bool fillPress=digitalRead(lowSwitch);
 if ((fillPress==HIGH) && ( (tt-lastFillPress) >500 ) ){
   lastFillPress=tt;
   digitalWrite(inletRelay, LOW);
 }
 if (digitalRead(lowSwitch==LOW)){
   digitalWrite(inletRelay, HIGH);
 }
}

I have been thinking about this and I feel there is something missing but I don't know enough about your system to put my finger on it.

Can you describe step by step in plain language what this piece of code is supposed to do?

...R

fancycoconut:

  if (digitalRead(lowSwitch==LOW)){

digitalWrite(inletRelay, HIGH);
  }

It's this surely?

  if (digitalRead(lowSwitch)==LOW){
    digitalWrite(inletRelay, HIGH);
  }

"digitalRead(lowSwitch==LOW)" is just saying "digitalRead(boolean state)"

Infact you don't need the second read, so:

  if (lowSwitch==LOW){
    digitalWrite(inletRelay, HIGH);
  }

fancycoconut:
Hi,
I'm about finished my espresso machine, and am just ironing out some kinks in the fill program. This component takes the signal from a level sensor in the boiler, and then decides whether or not to turn on the fill pump. I would like to debounce the filling to maybe 3 seconds to prevent the pump from just clicking on/off when the water is boiling during steaming. How could I achieve this? Is it as simple as adding a delay in the component?

void fill(){

if(digitalRead(lowSwitch) == HIGH){
   digitalWrite(inletRelay, LOW);}
 if(digitalRead(lowSwitch)== LOW){
   digitalWrite(inletRelay, HIGH);}
}




Also, when the machine turns on, the pump always switches on for a second or so (even if the boiler is full), is there a way to prevent this?
Ethan

What processor are you using? AVR (Uno) or ARM (Due)?

Robin2:
I have been thinking about this and I feel there is something missing but I don't know enough about your system to put my finger on it.

Can you describe step by step in plain language what this piece of code is supposed to do?

...R

This basically checks my water level in the boiler. When this level is below a certain pin, it will trigger the fill pump.

Vaclav:
What processor are you using? AVR (Uno) or ARM (Due)?

I have uno.

fancycoconut:
This basically checks my water level in the boiler. When this level is below a certain pin, it will trigger the fill pump.
I have uno.

I understand it at that overview level.
What I would like you to do is talk me step by step through the program and the process it is controlling so that I have the same view of it as you have.

...R

Robin2:
I understand it at that overview level.
What I would like you to do is talk me step by step through the program and the process it is controlling so that I have the same view of it as you have.

...R

By the program do you mean the entire program? Or just the fill program?
The overall program is controlling my espresso machine, so there is a boiler, fill pump, brew pump, and some other solenoids and mechanical parts.
The fill circuit (in more depth) is basically the boiler with a contact attached to the bottom of the boiler and then another contact which is submerged at a certain level. I have a simple circuit which (if i remember correctly) outputs HIGH to the arduino when water is absent. At this point, the arduino should output HIGH to the relay, which will complete the circuit to the fill pump. I would like to debounce the turning on of the pump so that if the water is at a rolling boil, the pump won't spaz out.
Hope this helps

fancycoconut:
I have a simple circuit which (if i remember correctly) outputs HIGH to the arduino when water is absent. At this point, the arduino should output HIGH to the relay, which will complete the circuit to the fill pump. I would like to debounce the turning on of the pump so that if the water is at a rolling boil, the pump won’t spaz out.

OK, let me try to write the detail as I think it is - correct me where I am wrong (I am drawing a lot from Reply #2)

There is enough water in the boiler
The boiler is boiling
The boiler is depressurized and the water starts bubbling
The bubbling water incorrectly triggers the low water level
…I don’t think that makes sense - let’s try again

There is enough water in the boiler
The boiler is boiling
The boiler is depressurized and the water level starts falling as the water is consumed
The falling water level triggers the low water signal
Because the boiling water is bubbling it can incorrectly turn off and on the low water signal

…I’m not sure that is correct either

Because I would expect the pump to stay running until the high water signal was triggered and I can’t see how the bubbling water could trigger that.

I hope this makes sense.

…R

Robin2:
OK, let me try to write the detail as I think it is - correct me where I am wrong (I am drawing a lot from Reply #2)

There is enough water in the boiler
The boiler is boiling
The boiler is depressurized and the water starts bubbling
The bubbling water incorrectly triggers the low water level
....I don't think that makes sense - let's try again

There is enough water in the boiler
The boiler is boiling
The boiler is depressurized and the water level starts falling as the water is consumed
The falling water level triggers the low water signal
Because the boiling water is bubbling it can incorrectly turn off and on the low water signal

...I'm not sure that is correct either

Because I would expect the pump to stay running until the high water signal was triggered and I can't see how the bubbling water could trigger that.

I hope this makes sense.

...R

You are sort-of right. False triggering can happen in a couple ways. Most common is a bubble rising where the pin is. The other large issue is after depressurization of the boiler (like after steaming milk), the water continues to roll and bubble for a while, and that clicks the pump on and off constantly.

OH I KNOW
What needs to be done is make the pump only turn on if the pin reads low water for more than a second. So i've been thinking about this in the wrong way. I still don't know how to have this happen though. So basically, to rephrase, I need to have the pump turn on if the water is low for more than a second or two.