Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. Question about code and electrolysis

Question about code and electrolysis

Scheduled Pinned Locked Moved Troubleshooting
7 Posts 4 Posters 1.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    punter9
    wrote on last edited by punter9
    #1

    I am writing code to read 2 analog moisture sensors that are subject to electrolysis. I am using millis as a timer to have it only read on set intervals. I currently have this code outside the millis function.

    unsigned long currentMillis = millis();
      int soilValue1 = analogRead(ANALOG_INPUT_SOIL_SENSOR1);// Read analog soil value
      int soilValue2 = analogRead(ANALOG_INPUT_SOIL_SENSOR2);
    

    my question is will this code still read the soil probes constantly and therefore erode them or will it only read on the intervals I have defined. I know it reports only on the intervals I have outlined but that really defeats the purpose or trying to prevent electrolysis if it is still reading them constantly and just not reporting the values. Thanks for any help!

    #include <SPI.h>
    #include <MySensor.h>
    
    #define ANALOG_INPUT_SOIL_SENSOR1 A0
    #define CHILD_ID1 0 // Id of the sensor child
    #define ANALOG_INPUT_SOIL_SENSOR2 A1
    #define CHILD_ID2 1 // Id of the sensor child
    
    
    long previousMillis = 0;
    long interval = (1000); //1000 is 1 second 
    
    MySensor gw;
    MyMessage msg1(CHILD_ID1, V_HUM),msg2(CHILD_ID2, V_HUM);
    int lastSoilValue1 = -1;
    int lastSoilValue2 = -1;
    
    void setup()
    
    {
      gw.begin();
      gw.sendSketchInfo("Soil Moisture Sensor - analog", "1.0"); // Send the sketch version information to the gateway and Controller
      pinMode(ANALOG_INPUT_SOIL_SENSOR1, INPUT);// sets the soil sensor analog pin as input
      pinMode(ANALOG_INPUT_SOIL_SENSOR2, INPUT);
      gw.present(CHILD_ID1, S_HUM);// Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID2, S_HUM);
    }
    
    void loop()
    
    {
      unsigned long currentMillis = millis();
      int soilValue1 = analogRead(ANALOG_INPUT_SOIL_SENSOR1);// Read analog soil value
      int soilValue2 = analogRead(ANALOG_INPUT_SOIL_SENSOR2);
      
      if (currentMillis - previousMillis > interval) {
        previousMillis = currentMillis;  
        Serial.println(soilValue1);
        Serial.println(soilValue2);
        gw.send(msg1.set(constrain(map(soilValue1, 1023, 250, 0, 100), 0, 100)));
        gw.send(msg2.set(constrain(map(soilValue2, 1023, 250, 0, 100), 0, 100)));
        lastSoilValue1 = soilValue1;
        lastSoilValue2 = soilValue2;
      }
    }```
    1 Reply Last reply
    0
    • P Offline
      P Offline
      punter9
      wrote on last edited by
      #2

      well I just moved it inside the if statement to make sure. If this doesn't do what I am hoping it does please chime in! Thanks.

      #include <SPI.h>
      #include <MySensor.h>
      
      #define ANALOG_INPUT_SOIL_SENSOR1 A0
      #define CHILD_ID1 0 // Id of the sensor child
      #define ANALOG_INPUT_SOIL_SENSOR2 A1
      #define CHILD_ID2 1 // Id of the sensor child
      
      
      long previousMillis = 0;
      long interval = (10000); //1000 is 1 second 
      
      MySensor gw;
      MyMessage msg1(CHILD_ID1, V_HUM),msg2(CHILD_ID2, V_HUM);
      int lastSoilValue1 = -1;
      int lastSoilValue2 = -1;
      
      void setup()
      
      {
        gw.begin();
        gw.sendSketchInfo("Soil Moisture Sensor - analog", "1.0"); // Send the sketch version information to the gateway and Controller
        pinMode(ANALOG_INPUT_SOIL_SENSOR1, INPUT);// sets the soil sensor analog pin as input
        pinMode(ANALOG_INPUT_SOIL_SENSOR2, INPUT);
        gw.present(CHILD_ID1, S_HUM);// Register all sensors to gw (they will be created as child devices)
        gw.present(CHILD_ID2, S_HUM);
      }
      
      void loop()
      
      {
        unsigned long currentMillis = millis();
        
        if (currentMillis - previousMillis > interval) {
          int soilValue1 = analogRead(ANALOG_INPUT_SOIL_SENSOR1);// Read analog soil value
          int soilValue2 = analogRead(ANALOG_INPUT_SOIL_SENSOR2);
          previousMillis = currentMillis;  
          Serial.println(soilValue1);
          Serial.println(soilValue2);
          gw.send(msg1.set(constrain(map(soilValue1, 1023, 250, 0, 100), 0, 100)));
          gw.send(msg2.set(constrain(map(soilValue2, 1023, 250, 0, 100), 0, 100)));
          lastSoilValue1 = soilValue1;
          lastSoilValue2 = soilValue2;
        }
      }
      
      m26872M DwaltD AWIA 3 Replies Last reply
      0
      • P punter9

        well I just moved it inside the if statement to make sure. If this doesn't do what I am hoping it does please chime in! Thanks.

        #include <SPI.h>
        #include <MySensor.h>
        
        #define ANALOG_INPUT_SOIL_SENSOR1 A0
        #define CHILD_ID1 0 // Id of the sensor child
        #define ANALOG_INPUT_SOIL_SENSOR2 A1
        #define CHILD_ID2 1 // Id of the sensor child
        
        
        long previousMillis = 0;
        long interval = (10000); //1000 is 1 second 
        
        MySensor gw;
        MyMessage msg1(CHILD_ID1, V_HUM),msg2(CHILD_ID2, V_HUM);
        int lastSoilValue1 = -1;
        int lastSoilValue2 = -1;
        
        void setup()
        
        {
          gw.begin();
          gw.sendSketchInfo("Soil Moisture Sensor - analog", "1.0"); // Send the sketch version information to the gateway and Controller
          pinMode(ANALOG_INPUT_SOIL_SENSOR1, INPUT);// sets the soil sensor analog pin as input
          pinMode(ANALOG_INPUT_SOIL_SENSOR2, INPUT);
          gw.present(CHILD_ID1, S_HUM);// Register all sensors to gw (they will be created as child devices)
          gw.present(CHILD_ID2, S_HUM);
        }
        
        void loop()
        
        {
          unsigned long currentMillis = millis();
          
          if (currentMillis - previousMillis > interval) {
            int soilValue1 = analogRead(ANALOG_INPUT_SOIL_SENSOR1);// Read analog soil value
            int soilValue2 = analogRead(ANALOG_INPUT_SOIL_SENSOR2);
            previousMillis = currentMillis;  
            Serial.println(soilValue1);
            Serial.println(soilValue2);
            gw.send(msg1.set(constrain(map(soilValue1, 1023, 250, 0, 100), 0, 100)));
            gw.send(msg2.set(constrain(map(soilValue2, 1023, 250, 0, 100), 0, 100)));
            lastSoilValue1 = soilValue1;
            lastSoilValue2 = soilValue2;
          }
        }
        
        m26872M Offline
        m26872M Offline
        m26872
        Hardware Contributor
        wrote on last edited by
        #3

        @punter9
        Nothing in your code seems to control the power to your soil sensor. You'll probably need to implement hw/sw for this.

        1 Reply Last reply
        0
        • P punter9

          well I just moved it inside the if statement to make sure. If this doesn't do what I am hoping it does please chime in! Thanks.

          #include <SPI.h>
          #include <MySensor.h>
          
          #define ANALOG_INPUT_SOIL_SENSOR1 A0
          #define CHILD_ID1 0 // Id of the sensor child
          #define ANALOG_INPUT_SOIL_SENSOR2 A1
          #define CHILD_ID2 1 // Id of the sensor child
          
          
          long previousMillis = 0;
          long interval = (10000); //1000 is 1 second 
          
          MySensor gw;
          MyMessage msg1(CHILD_ID1, V_HUM),msg2(CHILD_ID2, V_HUM);
          int lastSoilValue1 = -1;
          int lastSoilValue2 = -1;
          
          void setup()
          
          {
            gw.begin();
            gw.sendSketchInfo("Soil Moisture Sensor - analog", "1.0"); // Send the sketch version information to the gateway and Controller
            pinMode(ANALOG_INPUT_SOIL_SENSOR1, INPUT);// sets the soil sensor analog pin as input
            pinMode(ANALOG_INPUT_SOIL_SENSOR2, INPUT);
            gw.present(CHILD_ID1, S_HUM);// Register all sensors to gw (they will be created as child devices)
            gw.present(CHILD_ID2, S_HUM);
          }
          
          void loop()
          
          {
            unsigned long currentMillis = millis();
            
            if (currentMillis - previousMillis > interval) {
              int soilValue1 = analogRead(ANALOG_INPUT_SOIL_SENSOR1);// Read analog soil value
              int soilValue2 = analogRead(ANALOG_INPUT_SOIL_SENSOR2);
              previousMillis = currentMillis;  
              Serial.println(soilValue1);
              Serial.println(soilValue2);
              gw.send(msg1.set(constrain(map(soilValue1, 1023, 250, 0, 100), 0, 100)));
              gw.send(msg2.set(constrain(map(soilValue2, 1023, 250, 0, 100), 0, 100)));
              lastSoilValue1 = soilValue1;
              lastSoilValue2 = soilValue2;
            }
          }
          
          DwaltD Offline
          DwaltD Offline
          Dwalt
          wrote on last edited by
          #4

          @punter9
          You could try powering it from a digital pin, turn the pin high, wait 50ms, take analog reading, turn digital pin low.

          Veralite UI5 :: IBoard Ethernet GW :: MyS 1.5

          1 Reply Last reply
          0
          • P punter9

            well I just moved it inside the if statement to make sure. If this doesn't do what I am hoping it does please chime in! Thanks.

            #include <SPI.h>
            #include <MySensor.h>
            
            #define ANALOG_INPUT_SOIL_SENSOR1 A0
            #define CHILD_ID1 0 // Id of the sensor child
            #define ANALOG_INPUT_SOIL_SENSOR2 A1
            #define CHILD_ID2 1 // Id of the sensor child
            
            
            long previousMillis = 0;
            long interval = (10000); //1000 is 1 second 
            
            MySensor gw;
            MyMessage msg1(CHILD_ID1, V_HUM),msg2(CHILD_ID2, V_HUM);
            int lastSoilValue1 = -1;
            int lastSoilValue2 = -1;
            
            void setup()
            
            {
              gw.begin();
              gw.sendSketchInfo("Soil Moisture Sensor - analog", "1.0"); // Send the sketch version information to the gateway and Controller
              pinMode(ANALOG_INPUT_SOIL_SENSOR1, INPUT);// sets the soil sensor analog pin as input
              pinMode(ANALOG_INPUT_SOIL_SENSOR2, INPUT);
              gw.present(CHILD_ID1, S_HUM);// Register all sensors to gw (they will be created as child devices)
              gw.present(CHILD_ID2, S_HUM);
            }
            
            void loop()
            
            {
              unsigned long currentMillis = millis();
              
              if (currentMillis - previousMillis > interval) {
                int soilValue1 = analogRead(ANALOG_INPUT_SOIL_SENSOR1);// Read analog soil value
                int soilValue2 = analogRead(ANALOG_INPUT_SOIL_SENSOR2);
                previousMillis = currentMillis;  
                Serial.println(soilValue1);
                Serial.println(soilValue2);
                gw.send(msg1.set(constrain(map(soilValue1, 1023, 250, 0, 100), 0, 100)));
                gw.send(msg2.set(constrain(map(soilValue2, 1023, 250, 0, 100), 0, 100)));
                lastSoilValue1 = soilValue1;
                lastSoilValue2 = soilValue2;
              }
            }
            
            AWIA Offline
            AWIA Offline
            AWI
            Hero Member
            wrote on last edited by
            #5

            @punter9 An older post which discusses corrosion and how to avoid it

            1 Reply Last reply
            0
            • P Offline
              P Offline
              punter9
              wrote on last edited by punter9
              #6

              ah this makes sense. As long as these are connected to my voltage supply there will be this issue. I need to hook it to a controllable source such as a digital pin to get controlled voltage.

              I am learning here, but this seems to me the steps to the most electrolysis resistance.

              Base - 2 bolts in soil with an analog reading going to arduino, power and ground to power supply. Very high electrolysis

              1.) implement a digital pin as the power source - write high, wait, take analog reading, write low

              2.) implement a voltage dividing circuit. add resistor to one side of circuit, use sensor as other side, take analog reading between the two

              3.) reverse polarity each reading - write digital pin 1 high, digital pin 2 low, take analog reading. Next time do the inverse.

              Does this all seem correct? I really appreciate your help and references on this, I am really learning a ton here.

              1 Reply Last reply
              0
              • P Offline
                P Offline
                punter9
                wrote on last edited by punter9
                #7

                Ok, I decided for my needs all I really need to do is be able to turn power on and off so I have written code to utilize the digital pins as a power source, and continue with the analog read. This should extend the life of my sensors since I only need to get a reading about 4 times a day.

                Would anyone mind looking over this code and make sure it is actually limiting electrolysis as I intend? I am still learning here. Thanks for all the help so far!

                The way I have it set up at the moment is to have the power(vcc) come from the digital pin and then tie the ground to the common ground so as to not tie up more digital pins. Will this work as intended or do I need to also have a dedicated digital pin written low for each sensor?

                #include <SPI.h>
                #include <MySensor.h>
                
                #define ANALOG_INPUT_SOIL_SENSOR1 A0
                #define CHILD_ID1 0 // Id of the sensor child
                #define ANALOG_INPUT_SOIL_SENSOR2 A1
                #define CHILD_ID2 1 // Id of the sensor child
                
                
                long previousMillis = 0;
                long interval = (10000); //delay betweein readings - 1000 is 1 second 
                
                MySensor gw;
                MyMessage msg1(CHILD_ID1, V_HUM),msg2(CHILD_ID2, V_HUM);
                int soilPower1 = 1; //digital pin to get power from
                int soilPower2 = 2; //digital pin to get power from
                
                void setup()
                
                {
                  gw.begin();
                  gw.sendSketchInfo("Soil Moisture Sensor - analog", "1.0"); // Send the sketch version information to the gateway and Controller
                  pinMode(ANALOG_INPUT_SOIL_SENSOR1, INPUT);// sets the soil sensor analog pin as input
                  pinMode(ANALOG_INPUT_SOIL_SENSOR2, INPUT);
                  gw.present(CHILD_ID1, S_HUM);// Register all sensors to gw (they will be created as child devices)
                  gw.present(CHILD_ID2, S_HUM);
                  pinMode(soilPower1,OUTPUT);
                  pinMode(soilPower2,OUTPUT); 
                }
                
                void loop()
                
                {
                  unsigned long currentMillis = millis();
                  digitalWrite(soilPower1,LOW);
                  digitalWrite(soilPower2,LOW);
                  
                  if (currentMillis - previousMillis + 500 > interval) {
                    digitalWrite(soilPower1,HIGH);
                    digitalWrite(soilPower2,HIGH);
                  }
                
                  if (currentMillis - previousMillis > interval) {  
                    int soilValue1 = analogRead(ANALOG_INPUT_SOIL_SENSOR1);// Read analog soil value
                    int soilValue2 = analogRead(ANALOG_INPUT_SOIL_SENSOR2);
                    previousMillis = currentMillis;  
                    Serial.println(soilValue1);
                    Serial.println(soilValue2);
                    gw.send(msg1.set(constrain(map(soilValue1, 1023, 250, 0, 100), 0, 100)));
                    gw.send(msg2.set(constrain(map(soilValue2, 1023, 250, 0, 100), 0, 100)));
                    digitalWrite(soilPower1,LOW);
                    digitalWrite(soilPower2,LOW);
                  }
                }
                
                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                15

                Online

                11.7k

                Users

                11.2k

                Topics

                113.0k

                Posts


                Copyright 2019 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • MySensors
                • OpenHardware.io
                • Categories
                • Recent
                • Tags
                • Popular