Problem with reed sensor



  • Hello
    I would like to build a water level sensor.
    I have four reed contacts wired and connected.
    it works so far.
    but unfortunately it happens very often that he switches though.
    but not back.
    I think it's the software.
    Unfortunately, my programming arts are not that great.
    my english also not .... 🙂
    can someone help me solve this problem?

    I use the following sketch.

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     * 
     * DESCRIPTION
     * Example sketch for a "light switch" where you can control light or something 
     * else from both HA controller and a local physical button 
     * (connected between digital pin 3 and GND).
     * This node also works as a repeader for other nodes
     * http://www.mysensors.org/build/relay
     */ 
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #define MY_NODE_ID 50
     
    // Enabled repeater feature for this node
    #define MY_REPEATER_FEATURE
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>
    
    #define RELAY_PIN1  8  // Arduino Digital I/O pin number for relay
    #define RELAY_PIN2  6  // Arduino Digital I/O pin number for relay 
    #define BUTTON_PIN1  2  // Arduino Digital I/O pin number for button
    #define BUTTON_PIN2  3  // Arduino Digital I/O pin number for button
    #define BUTTON_PIN3  4  // Arduino Digital I/O pin number for button
    #define BUTTON_PIN4  5  // Arduino Digital I/O pin number for button 
    #define BUTTON_PIN5  A3  // Arduino Digital I/O pin number for button 
    #define CHILD_ID 1   // Id of the sensor child
    #define RELAY_ON 1
    #define RELAY_OFF 0
    
    Bounce debouncer1 = Bounce();
    Bounce debouncer2 = Bounce();
    Bounce debouncer3 = Bounce();
    Bounce debouncer4 = Bounce();
    Bounce debouncer5 = Bounce();
    int oldValue1=0;
    int oldValue2=0;
    int oldValue3=0;
    int oldValue4=0;
    int oldValue5=0;
    bool state1;
    bool state2;
    bool state3;
    bool state4;
    bool state5;
    MyMessage msg1(1,V_LIGHT);
    MyMessage msg2(2,V_LIGHT);
    MyMessage msg3(3,V_LIGHT);
    MyMessage msg4(4,V_LIGHT);
    MyMessage msg5(5,V_LIGHT);
    
    void setup()  
    {  
      // Setup the button
      pinMode(BUTTON_PIN1,INPUT);
      pinMode(BUTTON_PIN2,INPUT);
      pinMode(BUTTON_PIN3,INPUT);
      pinMode(BUTTON_PIN4,INPUT);
      pinMode(BUTTON_PIN5,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN1,HIGH);
      digitalWrite(BUTTON_PIN2,HIGH);
      digitalWrite(BUTTON_PIN3,HIGH);
      digitalWrite(BUTTON_PIN4,HIGH);
      digitalWrite(BUTTON_PIN5,HIGH);
      
      // After setting up the button, setup debouncer
      debouncer1.attach(BUTTON_PIN1);
      debouncer2.attach(BUTTON_PIN2);
      debouncer3.attach(BUTTON_PIN3);
      debouncer4.attach(BUTTON_PIN4);
      debouncer5.attach(BUTTON_PIN5);
      debouncer1.interval(30);
      debouncer2.interval(30);
      debouncer3.interval(30);
      debouncer4.interval(30);
      debouncer5.interval(30);
    
      // Make sure relays are off when starting up
      digitalWrite(RELAY_PIN1, RELAY_OFF);
      digitalWrite(RELAY_PIN2, RELAY_OFF);
      // Then set relay pins in output mode
      pinMode(RELAY_PIN1, OUTPUT);
      pinMode(RELAY_PIN2, OUTPUT);   
          
      // Set relay to last known state (using eeprom storage) 
      state1 = loadState(1);
      digitalWrite(RELAY_PIN1, state1?RELAY_ON:RELAY_OFF);
      state5 = loadState(5);
      digitalWrite(RELAY_PIN2, state5?RELAY_ON:RELAY_OFF);
    }
    
    void presentation()  {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Relay & Button", "1.0");
    
      // Register all sensors to gw (they will be created as child devices)
      present(1, S_LIGHT);
      present(2, S_LIGHT);
      present(3, S_LIGHT);
      present(4, S_LIGHT);
      present(5, S_LIGHT);
    }
    
    /*
    *  Example on how to asynchronously check for new messages from gw
    */
    void loop()
    {
      debouncer1.update();
      debouncer2.update();
      debouncer3.update();
      debouncer4.update();
      debouncer5.update();
    
      
      // Get the update value
      int value1 = debouncer1.read();
      if (value1 != oldValue1 && value1==0) {
          send(msg1.set(state1?false:true), true); // Send new state and request ack back
      }
      oldValue1 = value1;
     
      // Get the update value
      int value2 = debouncer2.read();
      if (value2 != oldValue2 && value2==0) {
          send(msg2.set(state2?false:true), true); // Send new state and request ack back
      }
      oldValue2 = value2;
    
      // Get the update value
      int value3 = debouncer3.read();
      if (value3 != oldValue3 && value3==0) {
          send(msg3.set(state3?false:true), true); // Send new state and request ack back
      }
      oldValue3 = value3;
    
      // Get the update value
      int value4 = debouncer4.read();
      if (value4 != oldValue4 && value4==0) {
          send(msg4.set(state4?false:true), true); // Send new state and request ack back
      }
      oldValue4 = value4;
    
      // Get the update value
      int value5 = debouncer5.read();
      if (value5 != oldValue5 && value5==0) {
          send(msg5.set(state5?false:true), true); // Send new state and request ack back
      }
      oldValue5 = value5;
    } 
     
    void receive(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.isAck()) {
         Serial.println("This is an ack from gateway");
      }
    
      if (message.type == V_LIGHT) {
         // Change relay state
         if(message.sensor==1){
      digitalWrite(RELAY_PIN1, message.getBool()?RELAY_ON:RELAY_OFF);
             state1=message.getBool();
      }
         // Change relay state
         if(message.sensor==2){
      digitalWrite(RELAY_PIN1, message.getBool()?RELAY_ON:RELAY_OFF);
             state2=message.getBool();
      }
         // Change relay state
         if(message.sensor==3){
      digitalWrite(RELAY_PIN1, message.getBool()?RELAY_ON:RELAY_OFF);
             state3=message.getBool();
      }
         // Change relay state
         if(message.sensor==4){
      digitalWrite(RELAY_PIN1, message.getBool()?RELAY_ON:RELAY_OFF);
             state4=message.getBool();
      }
      if(message.sensor==5){
      digitalWrite(RELAY_PIN2, message.getBool()?RELAY_ON:RELAY_OFF);
            state5=message.getBool();
      }
    
    // Store state in eeprom
          saveState(message.sensor, message.getBool());
      
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       } 
    }
    
    

    Greetings
    Michael



  • @micmuec Hello Michael and welcome. I am a little confused on your setup and what you are trying to accomplish with all of your sensors? Can you explain better what you are trying to do and how your sensor setup is supposed to work. Then I can better understand what your code is doing.



  • Hello

    the individual sensors should show me the status of the water.
    I have a float with magnet that triggers the individual reed contacts.
    sensor (reed) 1 = 25%, sensor 2 = 50%, 3 = 75%, 4 = 100%, sensor 5 is intended for a relay.
    I would like to see the whole in FHEM.
    and at 100% a pump should start up and pump up to 25%, then switch off again. I want to control the pump with FHEM.
    but there are my problems. because the reeds do not switch clean, the pump would go on and off when it wants.
    I think man could also write the code differently, so that all the sensor takes over.
    but unfortunately I miss the programming knowledges.
    Do you manage my plan?

    greetings
    Michael



  • @micmuec It almost sounds like some of the problem is your float switches. What is relay 1 and relay 2?



  • Hello

    the relays have no function in this case.
    I took over the sketch and changed just a little bit.
    I do not really need them.

    greetings
    Michael



  • @micmuec wasn't one of them to start the pump?



  • @micmuec If I read your post correctly you are trying to use 1940's pump controls via a processor... I do not understand why.
    Incidentally, you would normally express tank capacity as % full, you are doing the inverse, % empty.

    The reed switches will function correctly if the magnetic field closes it, that is a physical issue, nothing to do with your program, so check the proximity of magnet and reed.
    The debounce function is for the computer to avoid false positives on a processor running in millisecond cycles, hence registering a single event rather than every bounce of the contacts (typically 10-60ms). How long does it take before the level moves sufficiently to avoid the reed closing? I suggest you can ignore repeat signals as if the magnet is THAT close it does not matter how many times it registers.
    I suggest that you are looking at ANY contact on the reed is a signal to start or stop the pump, KISS...

    Were it me I would use your latching relay tied to supply via the reeds, one to switch on and one to switch off your relay. I would additionally recommend placing a safety contact above the highest to stop the pump in the event the high contact does not trigger, that is how it was done in the 1940's too, as it saves overflowing and free showers for the neighbourhood.
    The computer is only getting in the way in this case I suggest, use the relay and reeds to control the pump, an ultrasonic or pressure sensor to monitor height, perhaps with an intervention relay from the computer should both the HIGH and the HIGH-HIGH reeds fail to register.
    Old fashioned technology maybe, but...



  • Hello

    Thank all for the answers.
    I've done a couple of tests and got new insights.
    @ zboblamont: yes, I know that it is very old technology, but usually old works better as new ...
    and yes you were partly right, as far as the interaction of the reeds and the magnets is concerned.
    It always happened two switching operations during a movement.
    After a bit of tweaking, I have now found the right position.

    but the bigger problem is actually that I may have too little idea .....
    The sketch is written in such a way, which is switched with each key. reed Closes = "on". reed closes the second time = "off".
    and i thought that when the reed is closed it is "on" and when the reed opens again it is "off".

    that means I have to rebuild my Floater, and rewrite the sketch, then it should actually work as I imagine.
    does anyone have a tip on how to change the sketch?

    Thank you very much!

    greetings
    Michael

    @ dbemowsk: not at the moment 😉



  • @micmuec You are not updating the technology, you are introducing technology to a solution to control something which needs no control, then trying to make the technology work with a program I do not think you understand, rather than write one you DO understand. No offence intended, I am still learning too...
    You would be far better leaving the 'old' technology in place doing the control, and using the 'new' technology to monitor it, that was my point.

    Assuming the reeds and magnet (external mounting I guess) function correctly, the reeds will be normally open, closing only when the magnet is near.
    One dual coil latching relay can be switched by the reeds to command the pump on or off by connecting the reeds via supply to the respective relay coils. Such a relay does not care how many "on" or "off" signals it gets, each will cease when the magnet moves away from the reed, and you do not have the switching current limitations of the processor to contend with when activating a relay.

    You can monitor what is going on by looking at the reed outputs, although it would be much simpler using the second contact of a DPDT latching relay to signal the pump command condition to your processor. - The reeds will change state as the magnet moves away, the second DPDT contact will remain closed until changed by the activation of the second reed at the end of the cycle.
    You can then pass that condition to your Gateway as your current state, Pump running or Pump off, Tank 75% or Tank empty, whichever your choice. The Gateway records the signalled condition, the Node has no need to retrieve anything from Gateway or EEPROM, simplicity itself.

    Does that make sense?



  • Hi

    Yes it is! I want to merge old and new technology.
    I am completely aware that I could control my pump only with the reeds and a relay, but that would be too easy.
    Of course I'm not offended, I know that I do not understand it correctly.
    I enjoy working with my arduino, even if it does not always work.
    free according to the motto. learning by doing!
    I already had a sensor with ultrasound.
    but this one was very prone to error. was probably due to the cheap product.
    I do not know yet if my new craft will be better, but I will see that.
    I want to check the reeds and let me show in FHEM what condition they have. and in this, I would have just connected a relay to my arduino and this can control the pump.
    It's way too complicated, but it would work.
    I think over my system again, maybe I'll build it as you suggested.

    Thank you!

    greetings
    Michael



  • @micmuec
    Again, I would suggest a HIGH-HIGH trigger just in case something goes wrong, to prevent overflow.


Log in to reply
 

Suggested Topics

  • 3
  • 6
  • 2
  • 5
  • 1

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts