Water sensing Christmas tree program - adding features

Hey guys! I am VERY new to using arduino’s and this is my first project but I think I have done pretty well for myself. I started out with this guide here but I wanted to add a few features, and change a few things. Basically it is a capacitive sensor in the water and when the capacitance drops below normal a relay will blink the lights once every 10 seconds and if below a fair level will blink the lights on the tree to say “Water, Water, Water” in morse code!!! Awesome!

FYI: tldr warning. You can skip down to the bold text if you don’t want to read this all

Firstly I made my sensor a little bit different. I used copper tape used for snail deterrent on a piece of clear plastic (just the DIY laminate stuff without anything inside it). I made two strips of tape about 1/4" wide about 1/4" apart. I soldered a lead to each and then just attached to the leads. Then I covered this all with packing tape as if it was just in the water the capacitance wouldn’t change. This should also allow me to make new sensors cheaply and quickly if needed.

Secondly, I have NO idea what I am doing when it comes to electrical diagrams (my father-in-law helped do most of the design and implementation) so when I went to the local electronics store and handed the guy the diagram at the post and asked for the components I just bought what he gave me. I guess he sold me the wrong/different relay. I’m pretty sure he warned me it was wrong but said I shouldn’t have any problems. With the original code the lights would be off when there was water, and blink on and off when it was low. I was able to switch the code out and make it work the way I want. I figured out this is what he was probably talking about.

Thirdly, I have frustrating pets that love to drink what my wife and I have named “tree tea”. I have to cover up the bowl at the bottom of the tree to keep them from drinking from it. I bought a plastic oil pan that I plan on cutting a hole in and flipping upside down on the bowl and strapping it down. This makes it difficult for me to add water, and determine what the level the water is at. I bought a small length of black tubing at the hardware store and a funnel. The tubing is ran along the trunk of the tree. I can pull it out when needed and funnel water down it into the bowl.

Since I can’t see what the level is at without taking all of this apart I tried to add logic to have the lights blink back at me in morse code. Naturally I chose for it to blink “thanks” back at me once it reaches my desired level.

This is where my problem is. I created a value called count. Every time the loop starts it increases this value by 1. Once the count reaches 30 it resets. What I am trying to do is determine if water is being added, to check if the desired “full” value has been met. This is difficult, not only because I am most definitely not a programmer, but because the capacitance is CONSTANTLY changing… It varies slightly each time the loop goes around. What I ended up trying is to record the capacitance in loop 1 as olevel. Then I compare the current capacitance, level, to olevel. If level is LARGER than olevel by more than 75 (this may be my problem here, value may need to go up or down) then check to see if the water level has reached full. If filling, but not full I have it output to serial “filling but not full” and keep checking. I know that when the water goes from normal, to fair I get the blink every 10 seconds, and when it goes from fair to low I get the morse “water” sequence. The problem is I sometimes get “thanks” even though I’m not filling the bowl. For simplicity I added a few boolean’s for “blink if full” and “output to serial” because I got sick of commenting lines out during testing. Right now I have both set to off but I am hoping to get the code working so I don’t flood my living room floor!

Any input would be helpful, I may just need to adjust the difference in capacitance in which I determine its being filled vs the normal variances. My other thought is that I my “count” is either too far apart or too close together. Thirdly, and in my opinion the most likely, is my code is just wrong.

I have my sketch attached, as well as some of the serial outputs during a time when it was over my “full” value, I was not filling with water and it was just sitting there outputting to serial.

Thanks guys, sorry for such a long and boring post!

Blinky_Tree_arduino.ino (4.01 KB)

Blinky_Tree_arduino.java (2.46 KB)

output.log (23.1 KB)

There is no need for a count variable and, in any case it will only take a few microseconds for loop() to execute so count will rapidly increase.

Have a look at the debounce example in the IDE. It is used for switches but you can use it to ensure that any input has stayed above or below a value for a defined period of time, thus avoiding false triggering caused by the readings jittering.

Thanks for such a quick response! I looked through the example and I guess I just don't understand what debounce is doing. Would I use it to say "the water level has been greater than "full" for a specified delay time to trigger the response morse code?

Will this filter out the random jumps in the capacitance? How do I use debounce to only trigger if the value used to be low and is now full? I don't want it to trigger that it is full unless it can detect that it was low, and now that I'm filling it has reached full.

Debounce is normally used to stop a switch or button causing multiple triggers as the contacts bounce between on and off as contact is made when the switch is thrown or button is pressed. The bounce period is usually quite short, in the order of milliseconds but if you are counting button presses such things matter. In your case the readings 'bounce' between below a trigger level and above it and the 'bounce' period could be several seconds or more until the readings settle above or below the trigger level even though they may still be varying.

Your description of how the debounce technique works is correct. It ensures that a contact or reading has been in the same state for a user define period of time, albeit longer than a button debounce in your case.

As to whether the reading used to be low and is now full I don't see a problem. If it used to be low and stayed like that for a while you would start to water the tree until it was full then stop when it was full for a while. You need to choose the trigger point and the 'debounce' period to suit your own project of course.

So I have been thinking about how to do this all day today and I can't figure it out. How do I determine that it was low, it's being filled, and its now full? I don't want it to blink "thanks" but the once it climbs from low to a good value then drop back down again.

This is why I dropped out of the programming in college! Programming is WAAAY to logical!!!!

I have not gone back over the details of your code but here is a skeleton of how you might determine when to water and to issue a 'thanks' message when it is done.

start of loop
  read humidity level with 'debounce' to stabilise the value 
  if level is low
    add water
    set thanked to false
  end if
  if level is high
    stop watering
    if thanked is false
      blink thanks
      set thanked to true
    end if
  end if
end of loop