Multi-Binary Switch Sketch



  • 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;
    	}
      }
    }

  • Contest Winner

    @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.



  • 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;
    		}
    	}
      }
    }

Log in to reply
 

Suggested Topics

  • 3
  • 2
  • 6
  • 1

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts