Using debounce twice in the same sketch



  • Hi there, i'm struggling to combine a couple sketches and it seems the only issue now is using debouncer.

    The binaryswitchsensor sketch uses Debouncer, but my relay actuator with buttons sketch does as well.

    These are the errors i get compiling:
    ChookHouseAutomationV21.ino:89:26: error: conflicting declaration 'Bounce debouncer [7]'
    ChookHouseAutomationV21.ino:71:8: error: 'debouncer' has a previous declaration as 'Bounce debouncer'
    ChookHouseAutomationV21.ino: In function 'void setup()':
    ChookHouseAutomationV21.ino:151:14: error: no match for 'operator[]' (operand types are 'Bounce' and 'int')
    ChookHouseAutomationV21.ino:152:14: error: no match for 'operator[]' (operand types are 'Bounce' and 'int')
    ChookHouseAutomationV21.ino:153:14: error: no match for 'operator[]' (operand types are 'Bounce' and 'int')
    ChookHouseAutomationV21.ino: In function 'void loop()':
    ChookHouseAutomationV21.ino:219:18: error: no match for 'operator[]' (operand types are 'Bounce' and 'byte {aka unsigned char}')
    ChookHouseAutomationV21.ino:221:28: error: no match for 'operator[]' (operand types are 'Bounce' and 'byte {aka unsigned char}')
    Error compiling.

    Can anyone help please?? This is for my chook house automation system.


  • Admin

    @breimann It is usually helpful if you post the entire sketch within code blocks. You can create a code block by entering ``` before and after the code.



  • said in Using debounce twice in the same sketch:

    'Bounce debouncer [7]

    Agree with @blacey, however, the problem is that one portion of the code uses

    Bounce debouncer [7]
    

    Whereas the other uses

    Bounce debouncer
    

    It might be enough to remove this declaration and replace

    debouncer
    

    with

    debouncer[0]
    

    in the code using the non-array debouncer class. I do not know your hardware setup, so the index (the zero) might be different.



  • Thankyou @blacey I can try and thanks for telling me how to post code properly!



  • @pansen, Thankyou for your suggestion. I just don't understand the bounce/debounce function properly.

    I'll try posting my whole code.

    Thankyou to both of you for taking the time to help.



  • @breimann Here is my code. It's not pretty.

    #define MY_DEBUG
    
    // Enable and select radio type attached 
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    //#define MY_RS485
     
    #include <SPI.h>
    #include <MySensors.h>  
    #include <DHT.h>
    #include <Bounce2.h>
    
    // Set this to the pin you connected the DHT's data pin to
    #define DHT_DATA_PIN 14
    
    // Set this offset if the sensor has a permanent small offset to the real temperatures
    #define SENSOR_TEMP_OFFSET 0
    
    // Sleep time between sensor updates (in milliseconds)
    // Must be >1000ms for DHT22 and >2000ms for DHT11
    static const uint64_t UPDATE_INTERVAL = 60000;
    
    // Force sending an update of the temperature after n sensor reads, so a controller showing the
    // timestamp of the last update doesn't show something like 3 hours in the unlikely case, that
    // the value didn't change since;
    // i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms]
    static const uint8_t FORCE_UPDATE_N_READS = 10;
    
    #define CHILD_ID_HUM 10
    #define CHILD_ID_TEMP 11
    #define CHILD_ID_DOOR 12
    
    #define RELAY_ON 0                      // switch around for ACTIVE LOW / ACTIVE HIGH relay
    #define RELAY_OFF 1
    #define DOOR_SENSOR 41                  // pin for door reed switch
    
    Bounce debouncer = Bounce(); 
    int oldValue=-1;
    
    //
    
    #define noRelays 7                     //2-4
    const int relayPin[] = {42, 43, 44, 50, 51, 52, 53};       //  switch around pins to your desire
    const int buttonPin[] = {2, 3, 4, 5, 6, 7, 8};   //  switch around pins to your desire
    
    class Relay             // relay class, store all relevant data (equivalent to struct)
    {
      public:
        int buttonPin;                   // physical pin number of button
        int relayPin;             // physical pin number of relay
        boolean relayState;               // relay status (also stored in EEPROM)
    };
    
    Relay Relays[noRelays];
    Bounce debouncer[noRelays];
    MyMessage msg[noRelays];
    
    
    float lastTemp;
    float lastHum;
    uint8_t nNoUpdatesTemp;
    uint8_t nNoUpdatesHum;
    bool metric = true;
    
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    MyMessage msgDoor(CHILD_ID_DOOR, V_TRIPPED);
    DHT dht;
    
    
    void presentation()  
    { 
      // Send the sketch version information to the gateway
      sendSketchInfo("The Chook Hotel", "2.0");
      
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID_HUM, S_HUM);
      present(CHILD_ID_TEMP, S_TEMP);
      present(CHILD_ID_DOOR, S_DOOR);  
    
      metric = getConfig().isMetric;
      
      wait(100);
    
      for (int i = 0; i < noRelays; i++)
        present(i, S_LIGHT);                               // present sensor to gateway
    
      wait(100);
    
    }
    
    
    void setup()
    {
      dht.setup(DHT_DATA_PIN); // set data pin of DHT sensor
      if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
        Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
      }
      // Sleep for the time of the minimum sampling period to give the sensor time to power up
      // (otherwise, timeout errors might occure for the first reading)
      sleep(dht.getMinimumSamplingPeriod());
      
      wait(100);
      // Initialize Relays with corresponding buttons
      for (int i = 0; i < noRelays; i++) {
        Relays[i].buttonPin = buttonPin[i];              // assign physical pins
        Relays[i].relayPin = relayPin[i];
        msg[i].sensor = i;                                   // initialize messages
        msg[i].type = V_LIGHT;
        pinMode(Relays[i].buttonPin, INPUT_PULLUP);
        wait(100);
        pinMode(Relays[i].relayPin, OUTPUT);
        Relays[i].relayState = loadState(i);                               // retrieve last values from EEPROM
        digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
        send(msg[i].set(Relays[i].relayState ? true : false));                 // make controller aware of last status
        wait(50);
        debouncer[i] = Bounce();                        // initialize debouncer
        debouncer[i].attach(buttonPin[i]);
        debouncer[i].interval(30);
        wait(50);
      }
    
      // Setup the button
      pinMode(DOOR_SENSOR,INPUT);
      // Activate internal pull-up
      digitalWrite(DOOR_SENSOR,HIGH);
      
      // After setting up the button, setup debouncer
      debouncer.attach(DOOR_SENSOR);
      debouncer.interval(5);
    
    }
    
    
    void loop()      
    {  
      // Force reading sensor, so it works also after sleep()
      dht.readSensor(true);
      
      // Get temperature from DHT library
      float temperature = dht.getTemperature();
      if (isnan(temperature)) {
        Serial.println("Failed reading temperature from DHT!");
      } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) {
        // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
        lastTemp = temperature;
        if (!metric) {
          temperature = dht.toFahrenheit(temperature);
        }
        // Reset no updates counter
        nNoUpdatesTemp = 0;
        temperature += SENSOR_TEMP_OFFSET;
        send(msgTemp.set(temperature, 1));
    
        #ifdef MY_DEBUG
        Serial.print("T: ");
        Serial.println(temperature);
        #endif
      } else {
        // Increase no update counter if the temperature stayed the same
        nNoUpdatesTemp++;
      }
    
      // Get humidity from DHT library
      float humidity = dht.getHumidity();
      if (isnan(humidity)) {
        Serial.println("Failed reading humidity from DHT");
      } else if (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS) {
        // Only send humidity if it changed since the last measurement or if we didn't send an update for n times
        lastHum = humidity;
        // Reset no updates counter
        nNoUpdatesHum = 0;
        send(msgHum.set(humidity, 1));
        
        #ifdef MY_DEBUG
        Serial.print("H: ");
        Serial.println(humidity);
        #endif
      } else {
        // Increase no update counter if the humidity stayed the same
        nNoUpdatesHum++;
      }
    
      for (byte i = 0; i < noRelays; i++) {
        if (debouncer[i].update()) {
          
          int value = debouncer[i].read();
          
          if ( value == LOW) {
            Relays[i].relayState = !Relays[i].relayState;
            digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF);
            send(msg[i].set(Relays[i].relayState ? true : false));
            // save sensor state in EEPROM (location == sensor number)
            saveState( i, Relays[i].relayState );
    
          }
    
        }
      }
    
    debouncer.update();
      // Get the update value
      int value = debouncer.read();
     
      if (value != oldValue) {
         // Send in the new value
         send(msgDoor.set(value==HIGH ? 1 : 0));
         oldValue = value;
      }
      //wait(20);
    }
    
    void receive(const MyMessage &message) {
      if (message.type == V_LIGHT) {
        if (message.sensor < noRelays) {          // check if message is valid for relays..... previous line  [[[ if (message.sensor <=noRelays){ ]]]
          Relays[message.sensor].relayState = message.getBool();
          digitalWrite(Relays[message.sensor].relayPin, Relays[message.sensor].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
          saveState( message.sensor, Relays[message.sensor].relayState ); // save sensor state in EEPROM (location == sensor number)
        }
      }
      wait(20);
    }


  • No worries. Can you upload a schematic? Like, what is connected where?

    As a start:

    Change 1

    Bounce debouncer = Bounce();  
    

    to

    Bounce debouncer_door_sensor = Bounce();
    

    Change 2

     // After setting up the button, setup debouncer
     debouncer.attach(DOOR_SENSOR);
     debouncer.interval(5);
    

    to

     // After setting up the button, setup debouncer
     debouncer_door_sensor.attach(DOOR_SENSOR);
     debouncer_door_sensor.interval(5);
    

    Change 3

    debouncer.update();
    // Get the update value
     int value = debouncer.read();
    

    to

    debouncer_door_sensor.update();
    // Get the update value
     int value = debouncer_door_sensor.read();
    

    Good luck



  • @pansen Thankyou!!! I made those 3 changes and tried compiling and it compiled all good. Now i just need to try uploading but that's a bit complex as my Mega is in the chookhouse! I'll try it tomorrow and let you know.



  • @pansen Sorry i haven't gotten to uploading it. Too much else on. Will let you know when i do.



  • @pansen I uploaded it last week, and it all works great!!! Thankyou Heaps!!


Log in to reply
 

Suggested Topics

  • 1
  • 1
  • 1
  • 10
  • 2
  • 5

24
Online

11.2k
Users

11.1k
Topics

112.5k
Posts