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. IR distance sensor with relay and repeater not working

IR distance sensor with relay and repeater not working

Scheduled Pinned Locked Moved Troubleshooting
8 Posts 2 Posters 3.2k Views 1 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.
  • F Offline
    F Offline
    floris
    wrote on last edited by
    #1

    Hi,
    i cant make my IR distance-repeater-relay sensor to work. It doesnt go into the repeatermode and i cant switch the relays. the distancesensor does work.. anyone any idea?

    // Example sketch showing how to control physical relays.
    // This example will remember relay state even after power failure.
    
    #include <MySensor.h>
    #include <SPI.h>
    

    #include <DistanceGP2Y0A21YK.h>

    #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 2 // Total number of attached relays
    #define RELAY_ON 0  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 1 // GPIO value to write to turn off attached relay
    

    #define CHILD_ID_DISTANCE 100
    DistanceGP2Y0A21YK Dist;
    // unsigned long SLEEP_TIME = 5000; // Sleep time between reads (in milliseconds)

    MySensor gw;
    int numSensors=0;
    

    boolean receivedConfig = true;
    boolean metric = true;
    int distance;
    int lastDistance=0;
    int loopCounter=0;
    MyMessage msg(1,V_LIGHT);
    MyMessage msgdist(CHILD_ID_DISTANCE,V_DISTANCE);

    void setup() 
    {   
    

    Dist.begin(0);
    // Initialize library and add callback for incoming messages
    gw.begin(NULL, AUTO, true);
    // Send the sketch version information to the gateway and Controller
    gw.sendSketchInfo("RelayDistanceFloris", "1.0");
    gw.present(CHILD_ID_DISTANCE,S_DISTANCE);
    // Fetch relay status
    for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
    // Register all sensors to gw (they will be created as child devices)
    gw.present(sensor, S_LIGHT);
    // Then set relay pins in output mode
    pinMode(pin, OUTPUT);
    // Set relay to last known state (using eeprom storage)
    boolean savedState = gw.loadState(sensor);
    digitalWrite(pin, savedState?RELAY_ON:RELAY_OFF);
    gw.send(msg.set(savedState? 1 : 0));
    }
    }

    void loop()
    {
      // Alway process incoming messages whenever possible
      gw.process();
    distance = Dist.getDistanceCentimeter();
    

    if (distance != lastDistance) {
    lastDistance = distance;
    gw.send(msgdist.set(distance));
    gw.sleep(50000);
    }
    }
    void incomingMessage(const MyMessage &message) {
    if (message.type==V_LIGHT) {
    boolean relayState = message.getBool();
    digitalWrite(message.sensor-1+RELAY_1, relayState?RELAY_ON:RELAY_OFF);
    gw.saveState(message.sensor, relayState);
    gw.send(msg.set(relayState? 1 : 0));
    }
    }

    BulldogLowellB 1 Reply Last reply
    0
    • F floris

      Hi,
      i cant make my IR distance-repeater-relay sensor to work. It doesnt go into the repeatermode and i cant switch the relays. the distancesensor does work.. anyone any idea?

      // Example sketch showing how to control physical relays.
      // This example will remember relay state even after power failure.
      
      #include <MySensor.h>
      #include <SPI.h>
      

      #include <DistanceGP2Y0A21YK.h>

      #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
      #define NUMBER_OF_RELAYS 2 // Total number of attached relays
      #define RELAY_ON 0  // GPIO value to write to turn on attached relay
      #define RELAY_OFF 1 // GPIO value to write to turn off attached relay
      

      #define CHILD_ID_DISTANCE 100
      DistanceGP2Y0A21YK Dist;
      // unsigned long SLEEP_TIME = 5000; // Sleep time between reads (in milliseconds)

      MySensor gw;
      int numSensors=0;
      

      boolean receivedConfig = true;
      boolean metric = true;
      int distance;
      int lastDistance=0;
      int loopCounter=0;
      MyMessage msg(1,V_LIGHT);
      MyMessage msgdist(CHILD_ID_DISTANCE,V_DISTANCE);

      void setup() 
      {   
      

      Dist.begin(0);
      // Initialize library and add callback for incoming messages
      gw.begin(NULL, AUTO, true);
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("RelayDistanceFloris", "1.0");
      gw.present(CHILD_ID_DISTANCE,S_DISTANCE);
      // Fetch relay status
      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
      // Register all sensors to gw (they will be created as child devices)
      gw.present(sensor, S_LIGHT);
      // Then set relay pins in output mode
      pinMode(pin, OUTPUT);
      // Set relay to last known state (using eeprom storage)
      boolean savedState = gw.loadState(sensor);
      digitalWrite(pin, savedState?RELAY_ON:RELAY_OFF);
      gw.send(msg.set(savedState? 1 : 0));
      }
      }

      void loop()
      {
        // Alway process incoming messages whenever possible
        gw.process();
      distance = Dist.getDistanceCentimeter();
      

      if (distance != lastDistance) {
      lastDistance = distance;
      gw.send(msgdist.set(distance));
      gw.sleep(50000);
      }
      }
      void incomingMessage(const MyMessage &message) {
      if (message.type==V_LIGHT) {
      boolean relayState = message.getBool();
      digitalWrite(message.sensor-1+RELAY_1, relayState?RELAY_ON:RELAY_OFF);
      gw.saveState(message.sensor, relayState);
      gw.send(msg.set(relayState? 1 : 0));
      }
      }

      BulldogLowellB Offline
      BulldogLowellB Offline
      BulldogLowell
      Contest Winner
      wrote on last edited by
      #2

      @floris said:

      gw.sleep(50000);

      it is sleeping, so it won't respond to repeater

      gw.sleep(50000);
      

      you need to keep the sensor awake if you plan on using the sensor as a repeater.

      think about using a millis( ) timer to check the distances every so many seconds....

      1 Reply Last reply
      0
      • F Offline
        F Offline
        floris
        wrote on last edited by
        #3

        Hi, thanks. if i disable that sleep, than i get a flipstorm from the distanceinfo. (and i still cant control my relay) i thought i would let send the distanceinfo each 5 seconds (for testing) and for the rest a no sleep mode for repeating and receiving for the relay...

        BulldogLowellB 1 Reply Last reply
        0
        • F floris

          Hi, thanks. if i disable that sleep, than i get a flipstorm from the distanceinfo. (and i still cant control my relay) i thought i would let send the distanceinfo each 5 seconds (for testing) and for the rest a no sleep mode for repeating and receiving for the relay...

          BulldogLowellB Offline
          BulldogLowellB Offline
          BulldogLowell
          Contest Winner
          wrote on last edited by BulldogLowell
          #4

          @floris

          post your code between three 'backticks' (reverse apostrophe like this:

          <<<< three back ticks here
          code
          code
          etc
          code
          <<<< three more back ticks here

          so it can be more easily read...

          try to edit your loop( ) function to look like this:

          void loop()
          {
            // Alway process incoming messages whenever possible
            gw.process();
            static unsigned long lastTime = millis();
            if (millis() - lastTime >= 5000UL) // update the distance every 5 seconds
            {
              distance = Dist.getDistanceCentimeter();
              if (distance != lastDistance)
              {
                lastDistance = distance;
                gw.send(msgdist.set(distance));
              }
              lastTime = millis();
            }
          }
          
          1 Reply Last reply
          0
          • F Offline
            F Offline
            floris
            wrote on last edited by floris
            #5

            Hi, ok. now i have changed it to this: But my relays doest react...

               // Example sketch showing how to control physical relays.
               // This example will remember relay state even after power failure.
            
               #include <MySensor.h>
               #include <SPI.h>
            #include <DistanceGP2Y0A21YK.h>
            
               #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
               #define NUMBER_OF_RELAYS 2 // Total number of attached relays
               #define RELAY_ON 0  // GPIO value to write to turn on attached relay
               #define RELAY_OFF 1 // GPIO value to write to turn off attached relay
            #define CHILD_ID_DISTANCE 100
            DistanceGP2Y0A21YK Dist;
            //    unsigned long SLEEP_TIME = 5000; // Sleep time between reads (in milliseconds)
            
               
               MySensor gw;
               int numSensors=0;
            boolean receivedConfig = true;
            boolean metric = true;
            int distance;
            int lastDistance=0;
            int loopCounter=0;
               MyMessage msg(1,V_LIGHT);
               MyMessage msgdist(CHILD_ID_DISTANCE,V_DISTANCE);
            
            
               void setup() 
               {   
            
            Dist.begin(0);
                 // Initialize library and add callback for incoming messages
                 gw.begin(NULL, AUTO, true);
                 // Send the sketch version information to the gateway and Controller
                 gw.sendSketchInfo("RelayDistanceFloris", "1.0");
            gw.present(CHILD_ID_DISTANCE,S_DISTANCE);
                 // Fetch relay status
                 for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
                   // Register all sensors to gw (they will be created as child devices)
                   gw.present(sensor, S_LIGHT);
                   // Then set relay pins in output mode
                   pinMode(pin, OUTPUT);   
                   // Set relay to last known state (using eeprom storage)
                   boolean savedState = gw.loadState(sensor);
                   digitalWrite(pin, savedState?RELAY_ON:RELAY_OFF);
                   gw.send(msg.set(savedState? 1 : 0));
                 }
               }
            
            
               void loop()
               {
                 // Alway process incoming messages whenever possible
                 gw.process();
                   static unsigned long lastTime = millis();
             if (millis() - lastTime >= 5000UL) // update the distance every 5 seconds
             {
               distance = Dist.getDistanceCentimeter();
            if (distance != lastDistance)
            {
            lastDistance = distance;
            gw.send(msgdist.set(distance));
            }
               lastTime = millis();
               }
               }
               void incomingMessage(const MyMessage &message) {
                 if (message.type==V_LIGHT) {
                    boolean relayState = message.getBool();
                   digitalWrite(message.sensor-1+RELAY_1, relayState?RELAY_ON:RELAY_OFF);
                    gw.saveState(message.sensor, relayState);
                   gw.send(msg.set(relayState? 1 : 0));
                 }
               }
            
            
            F 1 Reply Last reply
            0
            • F floris

              Hi, ok. now i have changed it to this: But my relays doest react...

                 // Example sketch showing how to control physical relays.
                 // This example will remember relay state even after power failure.
              
                 #include <MySensor.h>
                 #include <SPI.h>
              #include <DistanceGP2Y0A21YK.h>
              
                 #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
                 #define NUMBER_OF_RELAYS 2 // Total number of attached relays
                 #define RELAY_ON 0  // GPIO value to write to turn on attached relay
                 #define RELAY_OFF 1 // GPIO value to write to turn off attached relay
              #define CHILD_ID_DISTANCE 100
              DistanceGP2Y0A21YK Dist;
              //    unsigned long SLEEP_TIME = 5000; // Sleep time between reads (in milliseconds)
              
                 
                 MySensor gw;
                 int numSensors=0;
              boolean receivedConfig = true;
              boolean metric = true;
              int distance;
              int lastDistance=0;
              int loopCounter=0;
                 MyMessage msg(1,V_LIGHT);
                 MyMessage msgdist(CHILD_ID_DISTANCE,V_DISTANCE);
              
              
                 void setup() 
                 {   
              
              Dist.begin(0);
                   // Initialize library and add callback for incoming messages
                   gw.begin(NULL, AUTO, true);
                   // Send the sketch version information to the gateway and Controller
                   gw.sendSketchInfo("RelayDistanceFloris", "1.0");
              gw.present(CHILD_ID_DISTANCE,S_DISTANCE);
                   // Fetch relay status
                   for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
                     // Register all sensors to gw (they will be created as child devices)
                     gw.present(sensor, S_LIGHT);
                     // Then set relay pins in output mode
                     pinMode(pin, OUTPUT);   
                     // Set relay to last known state (using eeprom storage)
                     boolean savedState = gw.loadState(sensor);
                     digitalWrite(pin, savedState?RELAY_ON:RELAY_OFF);
                     gw.send(msg.set(savedState? 1 : 0));
                   }
                 }
              
              
                 void loop()
                 {
                   // Alway process incoming messages whenever possible
                   gw.process();
                     static unsigned long lastTime = millis();
               if (millis() - lastTime >= 5000UL) // update the distance every 5 seconds
               {
                 distance = Dist.getDistanceCentimeter();
              if (distance != lastDistance)
              {
              lastDistance = distance;
              gw.send(msgdist.set(distance));
              }
                 lastTime = millis();
                 }
                 }
                 void incomingMessage(const MyMessage &message) {
                   if (message.type==V_LIGHT) {
                      boolean relayState = message.getBool();
                     digitalWrite(message.sensor-1+RELAY_1, relayState?RELAY_ON:RELAY_OFF);
                      gw.saveState(message.sensor, relayState);
                     gw.send(msg.set(relayState? 1 : 0));
                   }
                 }
              
              
              F Offline
              F Offline
              floris
              wrote on last edited by
              #6

              Yessss. i guesss i have got it working!!!! thanks for the help! Floris

                 // Example sketch showing how to control physical relays.
                 // This example will remember relay state even after power failure.
              
                  #include <MySensor.h>
                  #include <SPI.h>
                  #include <DistanceGP2Y0A21YK.h>
              #define CHILD_ID_DISTANCE 100
              DistanceGP2Y0A21YK Dist;
              
                  #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
                  #define NUMBER_OF_RELAYS 2 // Total number of attached relays
                  #define RELAY_ON 0  // GPIO value to write to turn on attached relay
                  #define RELAY_OFF 1 // GPIO value to write to turn off attached relay
              
                  MySensor gw;
                  int numSensors=0;
              boolean receivedConfig = true;
              boolean metric = true;
              int distance;
              int lastDistance=0;
              int loopCounter=0;
                  MyMessage msg(1,V_LIGHT);
                  MyMessage msgdist(CHILD_ID_DISTANCE,V_DISTANCE);
                  
                  void setup() 
                  {
                 Dist.begin(0);
                    // Initialize library and add callback for incoming messages
                    gw.begin(incomingMessage, AUTO, true);
                    // Send the sketch version information to the gateway and Controller
                    gw.sendSketchInfo("RelayDistanceRepeaterFloris", "1.0");
              
                    // Fetch relay status
                    for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
                      // Register all sensors to gw (they will be created as child devices)
              gw.present(CHILD_ID_DISTANCE,S_DISTANCE);
              gw.present(sensor, S_LIGHT);
                      // Then set relay pins in output mode
                      pinMode(pin, OUTPUT);   
                      // Set relay to last known state (using eeprom storage)
                      boolean savedState = gw.loadState(sensor);
                      digitalWrite(pin, savedState?RELAY_ON:RELAY_OFF);
                      gw.send(msg.set(savedState? 1 : 0));
                    }
                  }
              
              
                  void loop()
                  {
                    // Alway process incoming messages whenever possible
                    gw.process();
                            static unsigned long lastTime = millis();
                if (millis() - lastTime >= 5000UL) // update the distance every 5 seconds
                {
                  distance = Dist.getDistanceCentimeter();
              if (distance != lastDistance)
              {
              lastDistance = distance;
              gw.send(msgdist.set(distance));
              }
                  lastTime = millis();
                  }
                  }
              
                  void incomingMessage(const MyMessage &message) {
                    if (message.type==V_LIGHT) {
                       boolean relayState = message.getBool();
                      digitalWrite(message.sensor-1+RELAY_1, relayState?RELAY_ON:RELAY_OFF);
                       gw.saveState(message.sensor, relayState);
                      gw.send(msg.set(relayState? 1 : 0));
                    }
                  }
              
              BulldogLowellB 1 Reply Last reply
              0
              • F floris

                Yessss. i guesss i have got it working!!!! thanks for the help! Floris

                   // Example sketch showing how to control physical relays.
                   // This example will remember relay state even after power failure.
                
                    #include <MySensor.h>
                    #include <SPI.h>
                    #include <DistanceGP2Y0A21YK.h>
                #define CHILD_ID_DISTANCE 100
                DistanceGP2Y0A21YK Dist;
                
                    #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
                    #define NUMBER_OF_RELAYS 2 // Total number of attached relays
                    #define RELAY_ON 0  // GPIO value to write to turn on attached relay
                    #define RELAY_OFF 1 // GPIO value to write to turn off attached relay
                
                    MySensor gw;
                    int numSensors=0;
                boolean receivedConfig = true;
                boolean metric = true;
                int distance;
                int lastDistance=0;
                int loopCounter=0;
                    MyMessage msg(1,V_LIGHT);
                    MyMessage msgdist(CHILD_ID_DISTANCE,V_DISTANCE);
                    
                    void setup() 
                    {
                   Dist.begin(0);
                      // Initialize library and add callback for incoming messages
                      gw.begin(incomingMessage, AUTO, true);
                      // Send the sketch version information to the gateway and Controller
                      gw.sendSketchInfo("RelayDistanceRepeaterFloris", "1.0");
                
                      // Fetch relay status
                      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
                        // Register all sensors to gw (they will be created as child devices)
                gw.present(CHILD_ID_DISTANCE,S_DISTANCE);
                gw.present(sensor, S_LIGHT);
                        // Then set relay pins in output mode
                        pinMode(pin, OUTPUT);   
                        // Set relay to last known state (using eeprom storage)
                        boolean savedState = gw.loadState(sensor);
                        digitalWrite(pin, savedState?RELAY_ON:RELAY_OFF);
                        gw.send(msg.set(savedState? 1 : 0));
                      }
                    }
                
                
                    void loop()
                    {
                      // Alway process incoming messages whenever possible
                      gw.process();
                              static unsigned long lastTime = millis();
                  if (millis() - lastTime >= 5000UL) // update the distance every 5 seconds
                  {
                    distance = Dist.getDistanceCentimeter();
                if (distance != lastDistance)
                {
                lastDistance = distance;
                gw.send(msgdist.set(distance));
                }
                    lastTime = millis();
                    }
                    }
                
                    void incomingMessage(const MyMessage &message) {
                      if (message.type==V_LIGHT) {
                         boolean relayState = message.getBool();
                        digitalWrite(message.sensor-1+RELAY_1, relayState?RELAY_ON:RELAY_OFF);
                         gw.saveState(message.sensor, relayState);
                        gw.send(msg.set(relayState? 1 : 0));
                      }
                    }
                
                BulldogLowellB Offline
                BulldogLowellB Offline
                BulldogLowell
                Contest Winner
                wrote on last edited by
                #7

                @floris said:

                Yessss. i guesss i have got it working!!!! thanks for the help! Floris

                :+1:

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  floris
                  wrote on last edited by
                  #8

                  well.. i guess not..... It seems that its working for a short while. after that 1 of the relays does not respond anymore, when i set on relay on, both of the relays are going on? any help where my sketch is wrong?

                  Floris

                      // Example sketch showing how to control physical relays.
                      // This example will remember relay state even after power failure.
                  
                      #include <MySensor.h>
                      #include <SPI.h>
                      #include <DistanceGP2Y0A21YK.h>
                  #define CHILD_ID_DISTANCE 100
                  DistanceGP2Y0A21YK Dist;
                  
                      #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
                      #define NUMBER_OF_RELAYS 2 // Total number of attached relays
                      #define RELAY_ON 0  // GPIO value to write to turn on attached relay
                      #define RELAY_OFF 1 // GPIO value to write to turn off attached relay
                  
                      MySensor gw;
                      int numSensors=0;
                  boolean receivedConfig = true;
                  boolean metric = true;
                  int distance;
                  int lastDistance=0;
                  int loopCounter=0;
                      MyMessage msg(1,V_LIGHT);
                      MyMessage msgdist(CHILD_ID_DISTANCE,V_DISTANCE);
                      
                      void setup() 
                      {
                     Dist.begin(0);
                        // Initialize library and add callback for incoming messages
                        gw.begin(incomingMessage, AUTO, true);
                        // Send the sketch version information to the gateway and Controller
                        gw.sendSketchInfo("RelayDistanceRepeaterFloris", "1.0");
                  
                        // Fetch relay status
                        for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
                          // Register all sensors to gw (they will be created as child devices)
                  gw.present(CHILD_ID_DISTANCE,S_DISTANCE);
                  gw.present(sensor, S_LIGHT);
                          // Then set relay pins in output mode
                          pinMode(pin, OUTPUT);   
                          // Set relay to last known state (using eeprom storage)
                          boolean savedState = gw.loadState(sensor);
                          digitalWrite(pin, savedState?RELAY_ON:RELAY_OFF);
                          gw.send(msg.set(savedState? 1 : 0));
                        }
                      }
                  
                  
                      void loop()
                      {
                        // Alway process incoming messages whenever possible
                        gw.process();
                                static unsigned long lastTime = millis();
                    if (millis() - lastTime >= 5000UL) // update the distance every 5 seconds
                    {
                      distance = Dist.getDistanceCentimeter();
                  if (distance != lastDistance)
                  {
                  lastDistance = distance;
                  gw.send(msgdist.set(distance));
                  }
                      lastTime = millis();
                      }
                      }
                  
                      void incomingMessage(const MyMessage &message) {
                        if (message.type==V_LIGHT) {
                           boolean relayState = message.getBool();
                          digitalWrite(message.sensor-1+RELAY_1, relayState?RELAY_ON:RELAY_OFF);
                           gw.saveState(message.sensor, relayState);
                          gw.send(msg.set(relayState? 1 : 0));
                        }
                      }
                  
                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  13

                  Online

                  11.7k

                  Users

                  11.2k

                  Topics

                  113.1k

                  Posts


                  Copyright 2025 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