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. Binary switch code written for 1.5.4

Binary switch code written for 1.5.4

Scheduled Pinned Locked Moved Troubleshooting
5 Posts 2 Posters 1.2k Views 4 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.
  • ihtgtwtdI Offline
    ihtgtwtdI Offline
    ihtgtwtd
    wrote on last edited by
    #1

    Hi,
    I found the following sketch somewhere in the forum. It is written for MySensors 1.5.4, I tried to convert it for 2.1 but ended up with code that wont compile.

    What needs to be changed to make it compile?

    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    
    #define NUMBER_OF_SWITCHES 6
    
    MySensor gw;
    Bounce debouncer[NUMBER_OF_SWITCHES];
    
    int oldValue[NUMBER_OF_SWITCHES];
    byte switchPin[NUMBER_OF_SWITCHES] = {3,4,5,6,7,8}; //<<<<<<<<<<< set your switch pins here
    
    MyMessage msg(0,V_STATUS);
    
    void setup()  
    {  
      gw.begin();
    
      for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
      {
        pinMode(switchPin[i],INPUT_PULLUP);
        debouncer[i] = Bounce();
        debouncer[i].attach(switchPin[i]);
        debouncer[i].interval(5);
      }
      for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
      {
        gw.present(i, S_BINARY);
        delay(250);
      }
    }
    //
    void loop() 
    {
      for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
      {
        debouncer[i].update();
        int value = debouncer[i].read();
        if (value != oldValue[i]) 
        {
          gw.send(msg.setSensor(i).set(value == HIGH? true : false), false); 
        }
        oldValue[i] = value;
      }
    } 
    

    this is the original sketch without changes.

    greetings

    mfalkviddM 1 Reply Last reply
    0
    • ihtgtwtdI ihtgtwtd

      Hi,
      I found the following sketch somewhere in the forum. It is written for MySensors 1.5.4, I tried to convert it for 2.1 but ended up with code that wont compile.

      What needs to be changed to make it compile?

      #include <MySensor.h>
      #include <SPI.h>
      #include <Bounce2.h>
      
      #define NUMBER_OF_SWITCHES 6
      
      MySensor gw;
      Bounce debouncer[NUMBER_OF_SWITCHES];
      
      int oldValue[NUMBER_OF_SWITCHES];
      byte switchPin[NUMBER_OF_SWITCHES] = {3,4,5,6,7,8}; //<<<<<<<<<<< set your switch pins here
      
      MyMessage msg(0,V_STATUS);
      
      void setup()  
      {  
        gw.begin();
      
        for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
        {
          pinMode(switchPin[i],INPUT_PULLUP);
          debouncer[i] = Bounce();
          debouncer[i].attach(switchPin[i]);
          debouncer[i].interval(5);
        }
        for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
        {
          gw.present(i, S_BINARY);
          delay(250);
        }
      }
      //
      void loop() 
      {
        for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
        {
          debouncer[i].update();
          int value = debouncer[i].read();
          if (value != oldValue[i]) 
          {
            gw.send(msg.setSensor(i).set(value == HIGH? true : false), false); 
          }
          oldValue[i] = value;
        }
      } 
      

      this is the original sketch without changes.

      greetings

      mfalkviddM Offline
      mfalkviddM Offline
      mfalkvidd
      Mod
      wrote on last edited by
      #2

      @ihtgtwtd https://forum.mysensors.org/post/42715 has instructions on how to convert from 1.x to 2.x.

      1 Reply Last reply
      0
      • ihtgtwtdI Offline
        ihtgtwtdI Offline
        ihtgtwtd
        wrote on last edited by
        #3

        thanks @mfalkvidd
        I already read that post. I tried again and now it worked.

        Thanks again

        mfalkviddM 1 Reply Last reply
        1
        • ihtgtwtdI ihtgtwtd

          thanks @mfalkvidd
          I already read that post. I tried again and now it worked.

          Thanks again

          mfalkviddM Offline
          mfalkviddM Offline
          mfalkvidd
          Mod
          wrote on last edited by
          #4

          @ihtgtwtd great!

          Would you mind posting the converted sketch, in case someone else comes looking for it later?

          1 Reply Last reply
          0
          • ihtgtwtdI Offline
            ihtgtwtdI Offline
            ihtgtwtd
            wrote on last edited by
            #5

            No, of course not. Here you go, have fun everyone :-)

            #define MY_DEBUG
            #define MY_RADIO_NRF24
            
            #include <MySensors.h>
            #include <SPI.h>
            #include <Bounce2.h>
            
            #define SKETCH_NAME "Binary Sensor"
            #define SKETCH_MAJOR_VER "1"
            #define SKETCH_MINOR_VER "0"
            
            #define first_CHILD_ID 0
            #define second_CHILD_ID 1
            #define third_CHILD_ID 2
            #define fourth_CHILD_ID 3
            #define fifth_CHILD_ID 4
            #define sixth_CHILD_ID 5
            
            #define NUMBER_OF_SWITCHES 6
            
            Bounce debouncer[NUMBER_OF_SWITCHES];
            
            int oldValue[NUMBER_OF_SWITCHES];
            byte switchPin[NUMBER_OF_SWITCHES] = {3,4,5,6,7,8}; //<<<<<<<<<<< set your switch pins here
            
            MyMessage msg(0,V_STATUS);
            
            
            void setup()  
            {  
              for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
              {
                pinMode(switchPin[i],INPUT_PULLUP);
                debouncer[i] = Bounce();
                debouncer[i].attach(switchPin[i]);
                debouncer[i].interval(5);
              }
              for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
              {
                present(i, S_BINARY);
                delay(250);
              }
            }
            //
            void loop() 
            {
              for (int i = 0; i < NUMBER_OF_SWITCHES; i++)
              {
                debouncer[i].update();
                int value = debouncer[i].read();
                if (value != oldValue[i]) 
                {
                  send(msg.setSensor(i).set(value == HIGH? true : false), false); 
                }
                oldValue[i] = value;
              }
            } 
            
            1 Reply Last reply
            2
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            9

            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