Help!!! Binary & Relay Compilation Not Working



  • REVISED!!!

    Compiles but not working

    // Simple binary switch example 
    // Connect button or door/window reed switch between 
    // digitial I/O pin 3 (BUTTON_PIN below) and GND.
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    #include <Time.h>        //http://playground.arduino.cc/Code/Time
    #include <TimeAlarms.h>  //http://playground.arduino.cc/Code/Time
    
    MySensor gw;
    
    #define RADIO_ID 12
    #define noReeds 3
    const int BUTTON_PIN[] = {4,5,6};            // Arduino Digital I/O pin for button/reed switch, A0 - A4
    boolean reedState[] = {HIGH, HIGH, HIGH};
    
    #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 1 // Total number of attached relays
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    
    
    Bounce debouncer[noReeds];
    MyMessage msg[noReeds];
    
    void setup(){
    
      gw.begin(NULL, RADIO_ID, true);  //stattc RADIO_ID an enable repeater
      Alarm.delay(250);
      gw.sendSketchInfo("Relay", "1.0");
      gw.sendSketchInfo("Alarm Pannel", "1.0");
      Alarm.delay(250);
      gw.requestTime(receiveTime);
      // initialize Relays with corresponding buttons
      for (int i = 0; i < noReeds; i++){
        msg[i].sensor = i;          // initialize messages
        msg[i].type = V_TRIPPED;
        debouncer[i] = Bounce();        // initialize debouncer
        debouncer[i].attach(BUTTON_PIN[i]);
        debouncer[i].interval(5);
        // Setup the button
        pinMode(BUTTON_PIN[i],INPUT);
        // Activate internal pull-up
        digitalWrite(BUTTON_PIN[i],HIGH);
        gw.present(i, S_DOOR);      // present sensor to gateway
        Serial.print("setup for switch: ");
        Serial.print(BUTTON_PIN[i]);
        Serial.println(" complete" );
        Alarm.timerRepeat(720, updateState); // update relay status every 2 hrs
        Alarm.delay(250);
      }
       // Fetch relay status
      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
        // Register all sensors to gw (they will be created as child devices)
        gw.present(sensor, S_LIGHT);
        // Then set relay pins in output mode
        pinMode(pin, OUTPUT);   
        // Set relay to last known state (using eeprom storage) 
        digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
      }
    }
    
    //  Check if digital input has changed and send in new value
    void loop() 
    {
      gw.process();
      
    }
    
    void incomingMessage(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_LIGHT) {
         // Change relay state
         digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         gw.saveState(message.sensor, message.getBool());
           // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
         
      }  
         
      
      for (int i = 0; i < noReeds; i++){
        debouncer[i].update();
        // Get the update value
        int value = debouncer[i].read();
        if (value != reedState[i]) {
           // Send in the new value
           gw.send(msg[i].set(value==HIGH ? 1 : 0), false);
           reedState[i] = value;
           Serial.print("updating state for swicth: ");
           Serial.print(BUTTON_PIN[i]);
           Serial.print(" state: ");
           Serial.println(reedState[i]);
        }
      }
    }
    
    void updateState(){
      Serial.println("Start state info");
      for (int i = 0; i < noReeds; i++)
      {
          Serial.print("sending update for switch: ");
          Serial.println(BUTTON_PIN[i]);
          gw.present(i, S_DOOR);
          Alarm.delay(250);
          //MyMessage msg(relayPin[pin],V_LIGHT);
          gw.send(msg[i].set(reedState[i]), false); // Send last state from eprom to GW
          Alarm.delay(250);
      }
    }
    
    // This is called when a new time value was received
    void receiveTime(unsigned long time) {
      setTime(time);
    }```

  • Hardware Contributor

    On of the error might be the last line "}```)
    remove so its only }

    Br


  • Contest Winner

    @jeylites

    your example compiles for me.

    Consider removing TimeAlarms.h library and use gw.wait(250); instead of Alarm.delay(250);

    gw.wait() executes gw.process() over and over again during the delay time; so it is essentially non-blocking.



  • My bad. I copied the wrong sketch to this chat. I have revised the sketch and it complies but it's not working... 😞 Please see sketch above.



  • This is the serial message I get

    repeater started, id 12
    send: 12-12-0-0 s=255,c=0,t=18,pt=0,l=5,st=ok:1.4.1
    send: 12-12-0-0 s=255,c=3,t=6,pt=1,l=1,st=ok:0
    read: 0-0-12 s=255,c=3,t=6,pt=0,l=1:M
    send: 12-12-0-0 s=255,c=3,t=11,pt=0,l=5,st=ok:Relay
    send: 12-12-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
    send: 12-12-0-0 s=255,c=3,t=11,pt=0,l=12,st=ok:Alarm Pannel
    send: 12-12-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
    send: 12-12-0-0 s=255,c=3,t=1,pt=0,l=3,st=ok:1.0
    send: 12-12-0-0 s=0,c=0,t=0,pt=0,l=5,st=ok:1.4.1
    setup for switch: 5 complete
    send: 12-12-0-0 s=1,c=0,t=3,pt=0,l=5,st=ok:1.4.1
    read: 0-0-12 s=255,c=3,t=1,pt=0,l=10:1435918408


  • Contest Winner

    @jeylites

    fyi, just pick one name...

    gw.sendSketchInfo("Relay", "1.0");
    gw.sendSketchInfo("Alarm Pannel", "1.0");

    What is this supposed to be doing? it looks like:

    reporting the state of three reed switches
    actuating a relay

    Why are you using the dalays? time?

    hard to really understand what you are trying to do here...



  • @BulldogLowell
    Essentially, I will like to combine Array Binary and Relay Actuator in one sketch.


  • Contest Winner

    three binary switches and a single relay?



  • yes sir !



  • @BulldogLowell

    Any ideas how I can get this to work?


  • Hero Member

    @jeylites it looks like you are overwriting i somewhere inside the loop. can you try to add prints of the value of i at several strategic points to see when it becomes 5? I would suggest to add one (and also print BUTTON_PIN[i]) before "msg[i].sensor = i;", one after "// Setup the button" and print i with the BUTTON_PIN[i].



  • @Moshe-Livne

    I'm going to try fix it as suggested. But the thing is it complies....


  • Hero Member

    @jeylites Alas compiling is not a guarantee against memory overwrites....as button_pin should have shown 4 and not 5 i assume that either the index (i) got corrupt or the button_pin array.


  • Contest Winner

    @jeylites

    I think this is what you want. I cannot test it right now, but it does compile:

    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    
    #define NUMBER_OF_REEDS 3
    
    const int relayPin = 3;
    const int reedPin[NUMBER_OF_REEDS] = {4, 5, 6};
    
    int reedState[NUMBER_OF_REEDS];
    Bounce debouncer[NUMBER_OF_REEDS] = Bounce();
    
    MySensor gw;
    MyMessage msg[NUMBER_OF_REEDS];
    
    void setup()
    {
      gw.begin(incomingMessage, AUTO, true);
      gw.sendSketchInfo("Test", "1.0a");
      gw.present(0, S_LIGHT);
      pinMode(relayPin, OUTPUT);
      digitalWrite(relayPin, gw.loadState(0));
      for (int i = 0; i < NUMBER_OF_REEDS; i++)
      {
        debouncer[i].attach(reedPin[i]);
        debouncer[i].interval(5);
        pinMode(reedPin[i], INPUT_PULLUP);
        gw.present(i, S_DOOR);
        gw.wait(250);
      }
    }
    
    void loop()
    {
      gw.process();
      for (int i = 0; i < NUMBER_OF_REEDS; i++) 
      {
        debouncer[i].update();
        int value = debouncer[i].read();
        if (value != reedState[i]) 
        {
          gw.send(msg[i+1].set(value), false); // device number mismatch here because of the array
          reedState[i] = value;
          Serial.print("updating state for switch: ");
          Serial.print(reedPin[i]);
          Serial.print(" state: ");
          Serial.println(reedState[i]);
        }
      }
    }
    
    void incomingMessage(const MyMessage &message)
    {
      if (message.type == V_LIGHT)
      {
        int newState = message.getBool();
        digitalWrite(relayPin, newState);
        gw.saveState(0, newState);
        Serial.print(F("Incoming change for relay, New state: "));
        Serial.println(newState);
      }
    }
    

    EDIT: Updated to correct debounce class errors


  • Contest Winner

    @jeylites

    to turn relay off automatically after two minutes (and send the updated "off status" to controller... try something like this untested code:

    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    
    #define NUMBER_OF_REEDS 3
    
    const int relayPin = 3;
    const int reedPin[NUMBER_OF_REEDS] = {4, 5, 6};
    
    int reedState[NUMBER_OF_REEDS];
    Bounce debouncer[NUMBER_OF_REEDS] = Bounce();
    
    unsigned long relayTimer;
    
    MySensor gw;
    MyMessage msg[NUMBER_OF_REEDS];
    MyMessage relayMessage(0,V_LIGHT);
    
    void setup()
    {
      gw.begin(incomingMessage, AUTO, true);
      gw.sendSketchInfo("Test", "1.0a");
      gw.present(0, S_LIGHT);
      pinMode(relayPin, OUTPUT);
      digitalWrite(relayPin, gw.loadState(0));
      for (int i = 0; i < NUMBER_OF_REEDS; i++)
      {
        debouncer[i].attach(reedPin[i]);
        debouncer[i].interval(5);
        pinMode(reedPin[i], INPUT_PULLUP);
        gw.present(i, S_DOOR);
        gw.wait(250);
      }
    }
    
    void loop()
    {
      gw.process();
      for (int i = 0; i < NUMBER_OF_REEDS; i++) 
      {
        debouncer[i].update();
        int value = debouncer[i].read();
        if (value != reedState[i]) 
        {
          gw.send(msg[i+1].set(value), false); // device number mismatch here because of the array
          reedState[i] = value;
          Serial.print("updating state for switch: ");
          Serial.print(reedPin[i]);
          Serial.print(" state: ");
          Serial.println(reedState[i]);
        }
      }
      if (millis() - relayTimer > 2 * 60 * 1000UL && digitalRead(relayPin)) // two minute timer
      {
        digitalWrite(relayPin, LOW);
        gw.saveState(0, false);
        gw.send(relayMessage.set(false), true);
      }
    }
    
    void incomingMessage(const MyMessage &message)
    {
      if (message.type == V_LIGHT)
      {
        int newState = message.getBool();
        digitalWrite(relayPin, newState);
        gw.saveState(0, newState);
        if (newState)
        {
          relayTimer = millis();
        }
        Serial.print(F("Incoming change for relay, New state: "));
        Serial.println(newState);
      }
    }
    

Log in to reply
 

Suggested Topics

  • 4
  • 20
  • 15
  • 2
  • 4
  • 2

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts