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. Multi-Binary Switch Sketch

Multi-Binary Switch Sketch

Scheduled Pinned Locked Moved Troubleshooting
3 Posts 2 Posters 3.4k 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.
  • M Offline
    M Offline
    mikeones
    wrote on last edited by
    #1

    Anyone have a working example of a multi-switch sketch? I have started to modify the BinarySwitchSensor.ino sketch but I am not sure how to get the debouce settings per switch or if it is needed. I have attached my sketch and error that results when I try an compile this. Any suggestions?

    BinarySwitchSensor.ino: In function ‘void setup()’:
    BinarySwitchSensor:39: error: request for member ‘attach’ in ‘i’, which is of non-class type ‘int’
    BinarySwitchSensor:40: error: request for member ‘interval’ in ‘i’, which is of non-class type ‘int’
    BinarySwitchSensor.ino: In function ‘void loop()’:
    BinarySwitchSensor:54: error: request for member ‘update’ in ‘i’, which is of non-class type ‘int’
    BinarySwitchSensor:56: error: request for member ‘read’ in ‘i’, which is of non-class type ‘int’
    BinarySwitchSensor:61: error: lvalue required as left operand of assignment
    

    This is the code so far.

    // Simple binary switch example 
    // Connect button or door/window reed switch between 
    // digitial I/O pin 3 (ZONE_1 below) and GND.
    
    #include <Sensor.h>
    #include <SPI.h>
    #include <EEPROM.h>  
    #include <RF24.h>
    #include <Bounce2.h>
    
    #define ZONE_1  14  // Arduino Digital I/O pin for button/reed switch
    #define NUMBER_OF_ZONES 6
    
    Sensor gw;
    Bounce debouncer_1 = Bounce();
    Bounce debouncer_2 = Bounce();
    Bounce debouncer_3 = Bounce();
    Bounce debouncer_4 = Bounce();
    Bounce debouncer_5 = Bounce();
    Bounce debouncer_6 = Bounce();
    
    int oldValue_1 =-1;
    int oldValue_2 =-1;
    int oldValue_3 =-1;
    int oldValue_4 =-1;
    int oldValue_5 =-1;
    int oldValue_6 =-1;
    
    void setup()  
    {  
      gw.begin();
      for (int i=0; i<NUMBER_OF_ZONES;i++) {
       // Setup the button
    	pinMode(ZONE_1+i,INPUT);
    	// Activate internal pull-up
    	digitalWrite(ZONE_1+i,HIGH);
    	
    	// After setting up the button, setup debouncer
    	debouncer_1+i.attach(ZONE_1+i);
    	debouncer_1+i.interval(5);
    	
    	// Register binary input sensor to gw (they will be created as child devices)
    	// You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
    	// If S_LIGHT is used, remember to update variable type you send in below.
    	gw.sendSensorPresentation(ZONE_1+i, S_DOOR);
      }
    }
    
    
    //  Check if digital input has changed and send in new value
    void loop() 
    {
      for (int i=0; i<NUMBER_OF_ZONES;i++) {
    	debouncer_1+i.update();
    	// Get the update value
    	int value = debouncer_1+i.read();
       
    	if (value != oldValue_1+i) {
    	   // Send in the new value
    	   gw.sendVariable(ZONE_1+i, V_TRIPPED, value==HIGH ? "1" : "0");  // Change to V_LIGHT if you use S_LIGHT in presentation above
    	   oldValue_1+i = value;
    	}
      }
    }
    
    B 1 Reply Last reply
    0
    • M mikeones

      Anyone have a working example of a multi-switch sketch? I have started to modify the BinarySwitchSensor.ino sketch but I am not sure how to get the debouce settings per switch or if it is needed. I have attached my sketch and error that results when I try an compile this. Any suggestions?

      BinarySwitchSensor.ino: In function ‘void setup()’:
      BinarySwitchSensor:39: error: request for member ‘attach’ in ‘i’, which is of non-class type ‘int’
      BinarySwitchSensor:40: error: request for member ‘interval’ in ‘i’, which is of non-class type ‘int’
      BinarySwitchSensor.ino: In function ‘void loop()’:
      BinarySwitchSensor:54: error: request for member ‘update’ in ‘i’, which is of non-class type ‘int’
      BinarySwitchSensor:56: error: request for member ‘read’ in ‘i’, which is of non-class type ‘int’
      BinarySwitchSensor:61: error: lvalue required as left operand of assignment
      

      This is the code so far.

      // Simple binary switch example 
      // Connect button or door/window reed switch between 
      // digitial I/O pin 3 (ZONE_1 below) and GND.
      
      #include <Sensor.h>
      #include <SPI.h>
      #include <EEPROM.h>  
      #include <RF24.h>
      #include <Bounce2.h>
      
      #define ZONE_1  14  // Arduino Digital I/O pin for button/reed switch
      #define NUMBER_OF_ZONES 6
      
      Sensor gw;
      Bounce debouncer_1 = Bounce();
      Bounce debouncer_2 = Bounce();
      Bounce debouncer_3 = Bounce();
      Bounce debouncer_4 = Bounce();
      Bounce debouncer_5 = Bounce();
      Bounce debouncer_6 = Bounce();
      
      int oldValue_1 =-1;
      int oldValue_2 =-1;
      int oldValue_3 =-1;
      int oldValue_4 =-1;
      int oldValue_5 =-1;
      int oldValue_6 =-1;
      
      void setup()  
      {  
        gw.begin();
        for (int i=0; i<NUMBER_OF_ZONES;i++) {
         // Setup the button
      	pinMode(ZONE_1+i,INPUT);
      	// Activate internal pull-up
      	digitalWrite(ZONE_1+i,HIGH);
      	
      	// After setting up the button, setup debouncer
      	debouncer_1+i.attach(ZONE_1+i);
      	debouncer_1+i.interval(5);
      	
      	// Register binary input sensor to gw (they will be created as child devices)
      	// You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
      	// If S_LIGHT is used, remember to update variable type you send in below.
      	gw.sendSensorPresentation(ZONE_1+i, S_DOOR);
        }
      }
      
      
      //  Check if digital input has changed and send in new value
      void loop() 
      {
        for (int i=0; i<NUMBER_OF_ZONES;i++) {
      	debouncer_1+i.update();
      	// Get the update value
      	int value = debouncer_1+i.read();
         
      	if (value != oldValue_1+i) {
      	   // Send in the new value
      	   gw.sendVariable(ZONE_1+i, V_TRIPPED, value==HIGH ? "1" : "0");  // Change to V_LIGHT if you use S_LIGHT in presentation above
      	   oldValue_1+i = value;
      	}
        }
      }
      
      B Offline
      B Offline
      BulldogLowell
      Contest Winner
      wrote on last edited by BulldogLowell
      #2

      @mikeones said:

      Anyone have a working example of a multi-switch sketch? I have started to modify the BinarySwitchSensor.ino sketch but I am not sure how to get the debouce settings per switch or if it is needed. I have attached my sketch and error that results when I try an compile this. Any suggestions?

      BinarySwitchSensor.ino: In function ‘void setup()’:
      BinarySwitchSensor:39: error: request for member ‘attach’ in ‘i’, which is of non-class type ‘int’
      BinarySwitchSensor:40: error: request for member ‘interval’ in ‘i’, which is of non-class type ‘int’
      BinarySwitchSensor.ino: In function ‘void loop()’:
      BinarySwitchSensor:54: error: request for member ‘update’ in ‘i’, which is of non-class type ‘int’
      BinarySwitchSensor:56: error: request for member ‘read’ in ‘i’, which is of non-class type ‘int’
      BinarySwitchSensor:61: error: lvalue required as left operand of assignment
      

      This is the code so far.

      // Simple binary switch example
      // Connect button or door/window reed switch between
      // digitial I/O pin 3 (ZONE_1 below) and GND.

      #include <Sensor.h>
      #include <SPI.h>
      #include <EEPROM.h>
      #include <RF24.h>
      #include <Bounce2.h>

      #define ZONE_1 14 // Arduino Digital I/O pin for button/reed switch
      #define NUMBER_OF_ZONES 6

      Sensor gw;
      Bounce debouncer_1 = Bounce();
      Bounce debouncer_2 = Bounce();
      Bounce debouncer_3 = Bounce();
      Bounce debouncer_4 = Bounce();
      Bounce debouncer_5 = Bounce();
      Bounce debouncer_6 = Bounce();

      int oldValue_1 =-1;
      int oldValue_2 =-1;
      int oldValue_3 =-1;
      int oldValue_4 =-1;
      int oldValue_5 =-1;
      int oldValue_6 =-1;

      void setup()
      {
      gw.begin();
      for (int i=0; i<NUMBER_OF_ZONES;i++) {
      // Setup the button
      pinMode(ZONE_1+i,INPUT);
      // Activate internal pull-up
      digitalWrite(ZONE_1+i,HIGH);

        // After setting up the button, setup debouncer
        debouncer_1+i.attach(ZONE_1+i);
        debouncer_1+i.interval(5);
        
        // Register binary input sensor to gw (they will be created as child devices)
        // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
        // If S_LIGHT is used, remember to update variable type you send in below.
        gw.sendSensorPresentation(ZONE_1+i, S_DOOR);
      }
      

      }

      // Check if digital input has changed and send in new value
      void loop()
      {
      for (int i=0; i<NUMBER_OF_ZONES;i++) {
      debouncer_1+i.update();
      // Get the update value
      int value = debouncer_1+i.read();

        if (value != oldValue_1+i) {
           // Send in the new value
           gw.sendVariable(ZONE_1+i, V_TRIPPED, value==HIGH ? "1" : "0");  // Change to V_LIGHT if you use S_LIGHT in presentation above
           oldValue_1+i = value;
        }
      }
      

      }

      debouncer_1+i.attach(ZONE_1+i);
      

      yeah, you are trying to add an integer to a string

      the Bounce2.h library complains about that syntax, I cannot find good examples with multi pushbuttons.

      bounce.h seems a little more friendly to your needs, and a better set of instructions.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mikeones
        wrote on last edited by
        #3

        I did get this code to compile... I am going to test with this over the weekend.

        // Simple binary switch example 
        // Connect button or door/window reed switch between 
        // digitial I/O pin 3 (ZONE_1 below) and GND.
        
        #include <Sensor.h>
        #include <SPI.h>
        #include <EEPROM.h>  
        #include <RF24.h>
        #include <Bounce2.h>
        
        #define ZONE_1  14  // Arduino Digital I/O pin for button/reed switch
        #define NUMBER_OF_ZONES 6
        
        Sensor gw;
        Bounce debouncer_1 = Bounce();
        Bounce debouncer_2 = Bounce();
        Bounce debouncer_3 = Bounce();
        Bounce debouncer_4 = Bounce();
        Bounce debouncer_5 = Bounce();
        Bounce debouncer_6 = Bounce();
        
        int oldValue_1 =-1;
        int oldValue_2 =-1;
        int oldValue_3 =-1;
        int oldValue_4 =-1;
        int oldValue_5 =-1;
        int oldValue_6 =-1;
        
        void setup()  
        {  
          gw.begin();
          for (int i=0; i<NUMBER_OF_ZONES;i++) {
           // Setup the button
        	pinMode(ZONE_1+i,INPUT);
        	// Activate internal pull-up
        	digitalWrite(ZONE_1+i,HIGH);
        	
        	// After setting up the button, setup debouncer
        	switch (1+i) {
        	  case 1:
        		debouncer_1.attach(ZONE_1);
        		debouncer_1.interval(5);
        		break;
        	  case 2:
        		debouncer_2.attach(ZONE_1+i);
        		debouncer_2.interval(5);
        		break;
        	  case 3:
        		debouncer_3.attach(ZONE_1+i);
        		debouncer_3.interval(5);
        		break;
        	  case 4:
        		debouncer_4.attach(ZONE_1+i);
        		debouncer_4.interval(5);
        		break;
        	  case 5:
        		debouncer_5.attach(ZONE_1+i);
        		debouncer_5.interval(5);
        		break;
        	}
        	
        	// Register binary input sensor to gw (they will be created as child devices)
        	// You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
        	// If S_LIGHT is used, remember to update variable type you send in below.
        	gw.sendSensorPresentation(ZONE_1+i, S_DOOR);
        	delay(1000);
          }
        }
        
        
        //  Check if digital input has changed and send in new value
        void loop() 
        {
          for (int i=0; i<NUMBER_OF_ZONES;i++) {
        	int num = 1+i;
        	// Get the update value
        	switch (num) {
        	  case 1:
        		{
        		  debouncer_1.update();
        		  int value_1 = debouncer_1.read();
        		  if (value_1 != oldValue_1) {
        		   // Send in the new value
        		   gw.sendVariable(ZONE_1+i, V_TRIPPED, value_1==HIGH ? "1" : "0");  // Change to V_LIGHT if you use S_LIGHT in presentation above
        		   oldValue_1 = value_1;
        		  }
        		  break;
        		}
        	  case 2:
        		{
        		  debouncer_2.update();
        		  int value_2 = debouncer_2.read();
        		  if (value_2 != oldValue_2) {
        		   // Send in the new value
        		   gw.sendVariable(ZONE_1+i, V_TRIPPED, value_2==HIGH ? "1" : "0");  // Change to V_LIGHT if you use S_LIGHT in presentation above
        		   oldValue_2 = value_2;
        		  }
        		  break;
        		}
        	  case 3:
        		{
        		  debouncer_3.update();
        		  int value_3 = debouncer_3.read();
        		  if (value_3 != oldValue_3) {
        		   // Send in the new value
        		   gw.sendVariable(ZONE_1+i, V_TRIPPED, value_3==HIGH ? "1" : "0");  // Change to V_LIGHT if you use S_LIGHT in presentation above
        		   oldValue_3 = value_3;
        		  }
        		  break;
        		}
        	  case 4:
        		{
        		  debouncer_4.update();
        		  int value_4 = debouncer_4.read();
        		  if (value_4 != oldValue_4) {
        		   // Send in the new value
        		   gw.sendVariable(ZONE_1+i, V_TRIPPED, value_4==HIGH ? "1" : "0");  // Change to V_LIGHT if you use S_LIGHT in presentation above
        		   oldValue_4 = value_4;
        		  }
        		  break;
        		}
        	  case 5:
        		{
        		  debouncer_5.update();
        		  int value_5 = debouncer_5.read();
        		  if (value_5 != oldValue_5) {
        		   // Send in the new value
        		   gw.sendVariable(ZONE_1+i, V_TRIPPED, value_5==HIGH ? "1" : "0");  // Change to V_LIGHT if you use S_LIGHT in presentation above
        		   oldValue_5 = value_5;
        		  }
        		  break;
        		}
        	  case 6:
        		{
        		  debouncer_6.update();
        		  int value_6 = debouncer_6.read();
        		  if (value_6 != oldValue_6) {
        		   // Send in the new value
        		   gw.sendVariable(ZONE_1+i, V_TRIPPED, value_6==HIGH ? "1" : "0");  // Change to V_LIGHT if you use S_LIGHT in presentation above
        		   oldValue_6 = value_6;
        		  }
        		break;
        		}
        	}
          }
        }
        
        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


        12

        Online

        12.0k

        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