Binary button conflicting Relais with button



  • I use a sensor with 3 relais with button and 4 binary buttons. All 3 relais are working and 3 of the 4 buttons are working to but as soon as I hit the button on pin A1, the relay on pin A5 is switching. I have already tried to change the A1 pin in another but same result. Any idea how to solve this? included the code:```
    // 2x binary switch + dallas temp example
    // Connect button or door/window reed switch between
    // digitial I/O pin 4,5 (BUTTON_PIN_4/BUTTON_PIN_5 below) and GND.
    // Connect dallas temp sensor to digital pin 3

    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    #define DIGITAL_MOTION_SENSOR 3
    #define CHILD_ID_MOTION 6
    #define BUTTON1_PIN 2
    #define CHILD_ID_BUTTON1 4
    #define BUTTON2_PIN A0
    #define CHILD_ID_BUTTON2 3
    #define BUTTON3_PIN 8
    #define CHILD_ID_BUTTON3 5
    #define BUTTON4_PIN A1
    #define CHILD_ID_BUTTON4 1
    #define RELAY_ON 0 // switch around for realy HIGH/LOW state
    #define RELAY_OFF 1
    #define RADIO_ID 43

    Bounce motionsDebouncer = Bounce();
    Bounce button1debouncer = Bounce();
    Bounce button2debouncer = Bounce();
    Bounce button3debouncer = Bounce();
    Bounce button4debouncer = Bounce();
    int oldValueButt1=-1;
    int oldValueButt2=-1;
    int oldValueButt3=-1;
    int oldValueButt4=-1;

    int lastMOTION;
    unsigned long SLEEP_TIME = 10000;
    unsigned long blockMotionTimer = 0;

    MySensor gw;

    #define noRelays 3
    const int relayPin[] = {A3, A7, A6}; // switch around pins to your desire
    const int buttonPin[] = {4, A5, 7}; // 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
    byte oldValue; // last Values for key (debounce)
    boolean relayState; // relay status (also stored in EEPROM)
    };

    Relay Relays[noRelays];
    Bounce debouncerRELAY[noRelays];
    MyMessage msgRELAY[noRelays], msgMOTION(CHILD_ID_MOTION, V_TRIPPED), msgBUTTON1(CHILD_ID_BUTTON1,V_TRIPPED), msgBUTTON2(CHILD_ID_BUTTON2,V_TRIPPED), msgBUTTON3(CHILD_ID_BUTTON3,V_TRIPPED), msgBUTTON4(CHILD_ID_BUTTON4,V_TRIPPED);

    void setup()
    {
    //sensors.begin();
    //gw.begin();
    gw.begin(incomingMessage, AUTO, true);
    gw.sendSketchInfo("Motion/Button/relay", "1.0");

    // Setup the button
    pinMode(BUTTON1_PIN,INPUT);
    pinMode(BUTTON2_PIN,INPUT);
    pinMode(BUTTON3_PIN,INPUT);
    pinMode(BUTTON4_PIN,INPUT);
    // Activate internal pull-up
    digitalWrite(BUTTON1_PIN,HIGH);// Activate internal pull-up
    digitalWrite(BUTTON2_PIN,HIGH);
    digitalWrite(BUTTON3_PIN,HIGH);
    digitalWrite(BUTTON4_PIN,HIGH);

    gw.present(CHILD_ID_BUTTON1, S_DOOR);
    gw.present(CHILD_ID_BUTTON2, S_DOOR);
    gw.present(CHILD_ID_BUTTON3, S_DOOR);
    gw.present(CHILD_ID_BUTTON4, S_DOOR);

    // After setting up the button, setup debouncer
    

    button1debouncer.attach(BUTTON1_PIN);
    button1debouncer.interval(5);
    button2debouncer.attach(BUTTON2_PIN);
    button2debouncer.interval (5);
    button3debouncer.attach(BUTTON3_PIN);
    button3debouncer.interval(5);
    button4debouncer.attach(BUTTON4_PIN);
    button4debouncer.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. See "msg" above.
    gw.present(CHILD_ID_BUTTON1, S_DOOR);
    gw.present(CHILD_ID_BUTTON2, S_DOOR);
    gw.present(CHILD_ID_BUTTON3, S_DOOR);
    gw.present(CHILD_ID_BUTTON4, S_DOOR);

    pinMode(DIGITAL_MOTION_SENSOR, INPUT_PULLUP); // sets the motion sensor digital pin as input
    motionsDebouncer.attach(DIGITAL_MOTION_SENSOR);
    motionsDebouncer.interval(400);
    // Register all sensors to gw (they will be created as child devices)
    gw.present(CHILD_ID_MOTION, S_MOTION, "Motion needs presentation", true);

    for (int i = 0; i < noRelays; i++)
    {
    Relays[i].buttonPin = buttonPin[i]; // assign physical pins
    Relays[i].relayPin = relayPin[i];
    msgRELAY[i].sensor = i; // initialize messages
    msgRELAY[i].type = V_LIGHT;
    debouncerRELAY[i] = Bounce(); // initialize debouncer
    debouncerRELAY[i].attach(buttonPin[i]);
    debouncerRELAY[i].interval(5);
    pinMode(Relays[i].buttonPin, INPUT_PULLUP);
    pinMode(Relays[i].relayPin, OUTPUT);
    Relays[i].relayState = gw.loadState(i); // retrieve last values from EEPROM
    digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
    gw.send(msgRELAY[i].set(Relays[i].relayState ? true : false)); // make controller aware of last status
    gw.present(i, S_LIGHT); // present sensor to gateway
    delay(250);

    }

    }
    void loop()
    {
    gw.process();

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

    if (value != oldValueButt1) {
    // Send in the new value
    gw.send(msgBUTTON1.set(value==HIGH ? 1 : 0));
    oldValueButt1 = value;
    }
    button2debouncer.update();
    // Get the update value
    value = button2debouncer.read();

    if (value != oldValueButt2) {
    // Send in the new value
    gw.send(msgBUTTON2.set(value==HIGH ? 1 : 0));
    oldValueButt2 = value;
    }
    button3debouncer.update();
    // Get the update value
    value = button3debouncer.read();

    if (value != oldValueButt3) {
    // Send in the new value
    gw.send(msgBUTTON3.set(value==HIGH ? 1 : 0));
    oldValueButt3 = value;
    }
    button4debouncer.update();
    // Get the update value
    value = button4debouncer.read();

    if (value != oldValueButt4) {
    // Send in the new value
    gw.send(msgBUTTON4.set(value==HIGH ? 1 : 0));
    oldValueButt4 = value;
    }

    for (byte i = 0; i < noRelays; i++)
    {
    debouncerRELAY[i].update();
    byte value = debouncerRELAY[i].read();
    if (value != Relays[i].oldValue && value == 0)
    {
    Relays[i].relayState = !Relays[i].relayState;
    digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF);
    gw.send(msgRELAY[i].set(Relays[i].relayState ? true : false));
    gw.saveState( i, Relays[i].relayState );
    } // save sensor state in EEPROM (location == sensor number)

    Relays[i].oldValue = value;
    

    }

    if (blockMotionTimer == 0) {
    if (motionsDebouncer.update()) {
    int value = motionsDebouncer.read();
    Serial.println( "PIR " + (String)value );
    gw.send( msgMOTION.set( value ), true ); // Also it might need inverted value
    blockMotionTimer = (millis() + SLEEP_TIME);
    }
    } else {
    motionsDebouncer.update(); // dummy update to prevent false trigger after timer expires
    if (blockMotionTimer < millis()) {
    blockMotionTimer = 0;
    }
    }

    }

    void incomingMessage(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
    gw.saveState( message.sensor, Relays[message.sensor].relayState ); // save sensor state in EEPROM (location == sensor number)
    }
    }
    //gw.wait(50);

    }


  • Mod

    @Dick what you said in the introduction

    I use a sensor with 3 relais with button and 4 binary buttons. All 3 relais are working and 3 of the 4 buttons are working to but as soon as I hit the button on pin A1, the relay on pin A5 is switching.

    and the code you posted:

    const int relayPin[] = {A3, A7, A6}; // switch around pins to your desire
    const int buttonPin[] = {4, A5, 7}; // switch around pins to your desire

    don't match up.

    • You're saying you have 3 relays and 4 buttons, but you have only 3 elements in the buttonPin array?
    • You're saying you have a button on pin A1 when A1 isn't used at all?
    • You're saying the relay on pin A5 is switching, when A5 is used for a button?

    Could you clarify?
    Could you please use the code button to make the code section easier to read?



  • @mfalkvidd i thought is was ok because I defined 4 binary buttons 2, 8, A0 and A1 and they only work like a button. Beside these Buttons there is a class in use for the 3x relais with extra buttons to control the relais or do I make a mistake?



  • This post is deleted!

Log in to reply
 

Suggested Topics

  • 4
  • 3
  • 933
  • 9
  • 274
  • 14

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts