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. General Discussion
  3. Help!!! Binary & Relay Compilation Not Working

Help!!! Binary & Relay Compilation Not Working

Scheduled Pinned Locked Moved General Discussion
15 Posts 4 Posters 5.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.
  • S Offline
    S Offline
    sundberg84
    Hardware Contributor
    wrote on last edited by
    #2

    On of the error might be the last line "}```)
    remove so its only }

    Br

    Controller: Proxmox VM - Home Assistant
    MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
    MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
    RFLink GW - Arduino Mega + RFLink Shield, 433mhz

    1 Reply Last reply
    0
    • jeylitesJ jeylites

      REVISED!!!

      Compiles but not working

      // Simple binary switch example 
      // Connect button or door/window reed switch between 
      // digitial I/O pin 3 (BUTTON_PIN below) and GND.
      
      #include <MySensor.h>
      #include <SPI.h>
      #include <Bounce2.h>
      #include <Time.h>        //http://playground.arduino.cc/Code/Time
      #include <TimeAlarms.h>  //http://playground.arduino.cc/Code/Time
      
      MySensor gw;
      
      #define RADIO_ID 12
      #define noReeds 3
      const int BUTTON_PIN[] = {4,5,6};            // Arduino Digital I/O pin for button/reed switch, A0 - A4
      boolean reedState[] = {HIGH, HIGH, HIGH};
      
      #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
      #define NUMBER_OF_RELAYS 1 // Total number of attached relays
      #define RELAY_ON 1  // GPIO value to write to turn on attached relay
      #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
      
      
      Bounce debouncer[noReeds];
      MyMessage msg[noReeds];
      
      void setup(){
      
        gw.begin(NULL, RADIO_ID, true);  //stattc RADIO_ID an enable repeater
        Alarm.delay(250);
        gw.sendSketchInfo("Relay", "1.0");
        gw.sendSketchInfo("Alarm Pannel", "1.0");
        Alarm.delay(250);
        gw.requestTime(receiveTime);
        // initialize Relays with corresponding buttons
        for (int i = 0; i < noReeds; i++){
          msg[i].sensor = i;          // initialize messages
          msg[i].type = V_TRIPPED;
          debouncer[i] = Bounce();        // initialize debouncer
          debouncer[i].attach(BUTTON_PIN[i]);
          debouncer[i].interval(5);
          // Setup the button
          pinMode(BUTTON_PIN[i],INPUT);
          // Activate internal pull-up
          digitalWrite(BUTTON_PIN[i],HIGH);
          gw.present(i, S_DOOR);      // present sensor to gateway
          Serial.print("setup for switch: ");
          Serial.print(BUTTON_PIN[i]);
          Serial.println(" complete" );
          Alarm.timerRepeat(720, updateState); // update relay status every 2 hrs
          Alarm.delay(250);
        }
         // 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) 
          digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
        }
      }
      
      //  Check if digital input has changed and send in new value
      void loop() 
      {
        gw.process();
        
      }
      
      void incomingMessage(const MyMessage &message) {
        // We only expect one type of message from controller. But we better check anyway.
        if (message.type==V_LIGHT) {
           // Change relay state
           digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
           // Store state in eeprom
           gw.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());
           
        }  
           
        
        for (int i = 0; i < noReeds; i++){
          debouncer[i].update();
          // Get the update value
          int value = debouncer[i].read();
          if (value != reedState[i]) {
             // Send in the new value
             gw.send(msg[i].set(value==HIGH ? 1 : 0), false);
             reedState[i] = value;
             Serial.print("updating state for swicth: ");
             Serial.print(BUTTON_PIN[i]);
             Serial.print(" state: ");
             Serial.println(reedState[i]);
          }
        }
      }
      
      void updateState(){
        Serial.println("Start state info");
        for (int i = 0; i < noReeds; i++)
        {
            Serial.print("sending update for switch: ");
            Serial.println(BUTTON_PIN[i]);
            gw.present(i, S_DOOR);
            Alarm.delay(250);
            //MyMessage msg(relayPin[pin],V_LIGHT);
            gw.send(msg[i].set(reedState[i]), false); // Send last state from eprom to GW
            Alarm.delay(250);
        }
      }
      
      // This is called when a new time value was received
      void receiveTime(unsigned long time) {
        setTime(time);
      }```
      B Offline
      B Offline
      BulldogLowell
      Contest Winner
      wrote on last edited by
      #3

      @jeylites

      your example compiles for me.

      Consider removing TimeAlarms.h library and use gw.wait(250); instead of Alarm.delay(250);

      gw.wait() executes gw.process() over and over again during the delay time; so it is essentially non-blocking.

      1 Reply Last reply
      0
      • jeylitesJ Offline
        jeylitesJ Offline
        jeylites
        wrote on last edited by jeylites
        #4

        My bad. I copied the wrong sketch to this chat. I have revised the sketch and it complies but it's not working... :( Please see sketch above.

        1 Reply Last reply
        0
        • jeylitesJ Offline
          jeylitesJ Offline
          jeylites
          wrote on last edited by
          #5

          This is the serial message I get

          repeater started, id 12
          send: 12-12-0-0 s=255,c=0,t=18,pt=0,l=5,st=ok:1.4.1
          send: 12-12-0-0 s=255,c=3,t=6,pt=1,l=1,st=ok:0
          read: 0-0-12 s=255,c=3,t=6,pt=0,l=1:M
          send: 12-12-0-0 s=255,c=3,t=11,pt=0,l=5,st=ok:Relay
          send: 12-12-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
          send: 12-12-0-0 s=255,c=3,t=11,pt=0,l=12,st=ok:Alarm Pannel
          send: 12-12-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
          send: 12-12-0-0 s=255,c=3,t=1,pt=0,l=3,st=ok:1.0
          send: 12-12-0-0 s=0,c=0,t=0,pt=0,l=5,st=ok:1.4.1
          setup for switch: 5 complete
          send: 12-12-0-0 s=1,c=0,t=3,pt=0,l=5,st=ok:1.4.1
          read: 0-0-12 s=255,c=3,t=1,pt=0,l=10:1435918408

          1 Reply Last reply
          0
          • B Offline
            B Offline
            BulldogLowell
            Contest Winner
            wrote on last edited by
            #6

            @jeylites

            fyi, just pick one name...

            gw.sendSketchInfo("Relay", "1.0");
            gw.sendSketchInfo("Alarm Pannel", "1.0");

            What is this supposed to be doing? it looks like:

            reporting the state of three reed switches
            actuating a relay

            Why are you using the dalays? time?

            hard to really understand what you are trying to do here...

            1 Reply Last reply
            0
            • jeylitesJ Offline
              jeylitesJ Offline
              jeylites
              wrote on last edited by
              #7

              @BulldogLowell
              Essentially, I will like to combine Array Binary and Relay Actuator in one sketch.

              1 Reply Last reply
              0
              • B Offline
                B Offline
                BulldogLowell
                Contest Winner
                wrote on last edited by
                #8

                three binary switches and a single relay?

                1 Reply Last reply
                0
                • jeylitesJ Offline
                  jeylitesJ Offline
                  jeylites
                  wrote on last edited by
                  #9

                  yes sir !

                  1 Reply Last reply
                  0
                  • jeylitesJ Offline
                    jeylitesJ Offline
                    jeylites
                    wrote on last edited by jeylites
                    #10

                    @BulldogLowell

                    Any ideas how I can get this to work?

                    Moshe LivneM B 2 Replies Last reply
                    0
                    • jeylitesJ jeylites

                      @BulldogLowell

                      Any ideas how I can get this to work?

                      Moshe LivneM Offline
                      Moshe LivneM Offline
                      Moshe Livne
                      Hero Member
                      wrote on last edited by
                      #11

                      @jeylites it looks like you are overwriting i somewhere inside the loop. can you try to add prints of the value of i at several strategic points to see when it becomes 5? I would suggest to add one (and also print BUTTON_PIN[i]) before "msg[i].sensor = i;", one after "// Setup the button" and print i with the BUTTON_PIN[i].

                      1 Reply Last reply
                      0
                      • jeylitesJ Offline
                        jeylitesJ Offline
                        jeylites
                        wrote on last edited by
                        #12

                        @Moshe-Livne

                        I'm going to try fix it as suggested. But the thing is it complies....

                        Moshe LivneM 1 Reply Last reply
                        0
                        • jeylitesJ jeylites

                          @Moshe-Livne

                          I'm going to try fix it as suggested. But the thing is it complies....

                          Moshe LivneM Offline
                          Moshe LivneM Offline
                          Moshe Livne
                          Hero Member
                          wrote on last edited by
                          #13

                          @jeylites Alas compiling is not a guarantee against memory overwrites....as button_pin should have shown 4 and not 5 i assume that either the index (i) got corrupt or the button_pin array.

                          1 Reply Last reply
                          0
                          • jeylitesJ jeylites

                            @BulldogLowell

                            Any ideas how I can get this to work?

                            B Offline
                            B Offline
                            BulldogLowell
                            Contest Winner
                            wrote on last edited by BulldogLowell
                            #14

                            @jeylites

                            I think this is what you want. I cannot test it right now, but it does compile:

                            #include <MySensor.h>
                            #include <SPI.h>
                            #include <Bounce2.h>
                            
                            #define NUMBER_OF_REEDS 3
                            
                            const int relayPin = 3;
                            const int reedPin[NUMBER_OF_REEDS] = {4, 5, 6};
                            
                            int reedState[NUMBER_OF_REEDS];
                            Bounce debouncer[NUMBER_OF_REEDS] = Bounce();
                            
                            MySensor gw;
                            MyMessage msg[NUMBER_OF_REEDS];
                            
                            void setup()
                            {
                              gw.begin(incomingMessage, AUTO, true);
                              gw.sendSketchInfo("Test", "1.0a");
                              gw.present(0, S_LIGHT);
                              pinMode(relayPin, OUTPUT);
                              digitalWrite(relayPin, gw.loadState(0));
                              for (int i = 0; i < NUMBER_OF_REEDS; i++)
                              {
                                debouncer[i].attach(reedPin[i]);
                                debouncer[i].interval(5);
                                pinMode(reedPin[i], INPUT_PULLUP);
                                gw.present(i, S_DOOR);
                                gw.wait(250);
                              }
                            }
                            
                            void loop()
                            {
                              gw.process();
                              for (int i = 0; i < NUMBER_OF_REEDS; i++) 
                              {
                                debouncer[i].update();
                                int value = debouncer[i].read();
                                if (value != reedState[i]) 
                                {
                                  gw.send(msg[i+1].set(value), false); // device number mismatch here because of the array
                                  reedState[i] = value;
                                  Serial.print("updating state for switch: ");
                                  Serial.print(reedPin[i]);
                                  Serial.print(" state: ");
                                  Serial.println(reedState[i]);
                                }
                              }
                            }
                            
                            void incomingMessage(const MyMessage &message)
                            {
                              if (message.type == V_LIGHT)
                              {
                                int newState = message.getBool();
                                digitalWrite(relayPin, newState);
                                gw.saveState(0, newState);
                                Serial.print(F("Incoming change for relay, New state: "));
                                Serial.println(newState);
                              }
                            }
                            

                            EDIT: Updated to correct debounce class errors

                            1 Reply Last reply
                            0
                            • B Offline
                              B Offline
                              BulldogLowell
                              Contest Winner
                              wrote on last edited by BulldogLowell
                              #15

                              @jeylites

                              to turn relay off automatically after two minutes (and send the updated "off status" to controller... try something like this untested code:

                              #include <MySensor.h>
                              #include <SPI.h>
                              #include <Bounce2.h>
                              
                              #define NUMBER_OF_REEDS 3
                              
                              const int relayPin = 3;
                              const int reedPin[NUMBER_OF_REEDS] = {4, 5, 6};
                              
                              int reedState[NUMBER_OF_REEDS];
                              Bounce debouncer[NUMBER_OF_REEDS] = Bounce();
                              
                              unsigned long relayTimer;
                              
                              MySensor gw;
                              MyMessage msg[NUMBER_OF_REEDS];
                              MyMessage relayMessage(0,V_LIGHT);
                              
                              void setup()
                              {
                                gw.begin(incomingMessage, AUTO, true);
                                gw.sendSketchInfo("Test", "1.0a");
                                gw.present(0, S_LIGHT);
                                pinMode(relayPin, OUTPUT);
                                digitalWrite(relayPin, gw.loadState(0));
                                for (int i = 0; i < NUMBER_OF_REEDS; i++)
                                {
                                  debouncer[i].attach(reedPin[i]);
                                  debouncer[i].interval(5);
                                  pinMode(reedPin[i], INPUT_PULLUP);
                                  gw.present(i, S_DOOR);
                                  gw.wait(250);
                                }
                              }
                              
                              void loop()
                              {
                                gw.process();
                                for (int i = 0; i < NUMBER_OF_REEDS; i++) 
                                {
                                  debouncer[i].update();
                                  int value = debouncer[i].read();
                                  if (value != reedState[i]) 
                                  {
                                    gw.send(msg[i+1].set(value), false); // device number mismatch here because of the array
                                    reedState[i] = value;
                                    Serial.print("updating state for switch: ");
                                    Serial.print(reedPin[i]);
                                    Serial.print(" state: ");
                                    Serial.println(reedState[i]);
                                  }
                                }
                                if (millis() - relayTimer > 2 * 60 * 1000UL && digitalRead(relayPin)) // two minute timer
                                {
                                  digitalWrite(relayPin, LOW);
                                  gw.saveState(0, false);
                                  gw.send(relayMessage.set(false), true);
                                }
                              }
                              
                              void incomingMessage(const MyMessage &message)
                              {
                                if (message.type == V_LIGHT)
                                {
                                  int newState = message.getBool();
                                  digitalWrite(relayPin, newState);
                                  gw.saveState(0, newState);
                                  if (newState)
                                  {
                                    relayTimer = millis();
                                  }
                                  Serial.print(F("Incoming change for relay, New state: "));
                                  Serial.println(newState);
                                }
                              }
                              
                              1 Reply Last reply
                              0

                              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                              With your input, this post could be even better 💗

                              Register Login
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              15

                              Online

                              12.1k

                              Users

                              11.2k

                              Topics

                              113.4k

                              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