Need a little help to alter my sketch



  • Hey everyone,

    I have a few on the Mini Rboards working very well for control of a few light switches, using Vera 3 as my controller. What is driving me and the family a little crazy is how the manual light switches have to work so that they operate the Mini Rboard.

    Here is my sketch:

    // Example sketch fรถr a "light switch" where you can control light or something 
    // else from both vera and a local physical button (connected between digital
    // pin 14 and GND).
    // This node also works as a repeader for other nodes
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    
    #define RELAY_PIN  4  // Arduino Digital I/O pin number for relay 
    #define BUTTON_PIN  14  // Arduino Digital I/O pin number for button -A0 which is D14
    #define CHILD_ID 1   // Id of the sensor child
    #define RELAY_ON 1
    #define RELAY_OFF 0
    
    Bounce debouncer = Bounce(); 
    int oldValue=0;
    bool state;
    MySensor gw;
    MyMessage msg(CHILD_ID,V_LIGHT);
    
    void setup()  
    {  
      gw.begin(incomingMessage, AUTO, true);
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Relay & Button", "1.0");
    
     // Setup the button
      pinMode(BUTTON_PIN,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN,HIGH);
      
      // After setting up the button, setup debouncer
      debouncer.attach(BUTTON_PIN);
      debouncer.interval(5);
    
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID, S_LIGHT);
    
      // Make sure relays are off when starting up
      digitalWrite(RELAY_PIN, RELAY_OFF);
      // Then set relay pins in output mode
      pinMode(RELAY_PIN, OUTPUT);   
          
      // Set relay to last known state (using eeprom storage) 
      state = gw.loadState(CHILD_ID);
      digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
    }
    
    
    /*
    *  Example on how to asynchronously check for new messages from gw
    */
    void loop() 
    {
      gw.process();
      debouncer.update();
      // Get the update value
      int value = debouncer.read();
      if (value != oldValue && value==0) {
          gw.send(msg.set(state?false:true), true); // Send new state and request ack back
      }
      oldValue = value;
    } 
     
    void incomingMessage(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.isAck()) {
         Serial.println("This is an ack from gateway");
      }
    
      if (message.type == V_LIGHT) {
         // Change relay state
         state = message.getBool();
         digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         gw.saveState(CHILD_ID, state);
        
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       } 
    }
    
    

    So to use mu current light switches, I have to turn them on and then off, for the light to operate. If I only turn the switch on, the light will operate, but to operate it again I have to turn the switch off and then on again.

    I have looked into a momentary switch, but to get the ones to fit are quite expensive...

    Is there a way to alter the sketch to make it work better? IE can I have the sketch open the relay when the binary switch is connected, and then close the relay when it is disconnected?

    thanks for your help ๐Ÿ™‚


  • Hero Member

    This post is deleted!

  • Hero Member

    @Dean, try to change this line:

         if (value != oldValue && value==0)
    

    by removing the '&& value==0'

         if (value != oldValue)


  • @rvendrame thanks for that mate! I will give that a go.

    So that I am clear on the understanding, I have it so that it is waiting for a change, and it needs to equal zero. So by removing the zero bit, it should just be waiting for a change. I suppose the reason for it to be waiting for both is to reduce the chance of it missing a change in value, which I could see happening, but it may not be a big deal, so I'll change the code and try it.

    Thanks again for your help ๐Ÿ™‚


  • Contest Winner

    @Dean said:

    ving the zero bit, it should just be waiting for a change. I suppose the reason for it to be waiting for both is to reduce the chance of it missing a change in value, which I could see happening, but it may not be a big deal, so I'll change the code and try it.

    The original code is written for a Momentary switch. Press it and it closes the circuit driving a change of state, release it and again it changes back to an open circuit and the pin state changes again.

    You have a toggle switch which when 'flipped' remains in its new state.


Log in to reply
 

Suggested Topics

  • 3
  • 6
  • 2
  • 6
  • 5

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts