Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. More relay and push botton

More relay and push botton

Scheduled Pinned Locked Moved Development
relay remotexy
16 Posts 2 Posters 2.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Gabriele Cirillo

    sorry for tag! :)

    this is pratically the code of your link... with some adjustement of my pin configuration... but the output remain LOW...

    i know that is not simply :P

    rejoe2R Offline
    rejoe2R Offline
    rejoe2
    wrote on last edited by
    #6

    @gabriele-cirillo Sorry, but the one you posted may be the sketch of the first post in the linked thread, but my link (at least on my computer here ;-) ) leads to a very different sketch using arrays for assinging PINs and buttons. See post #33 by @korttoma in the linked thread.

    Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

    1 Reply Last reply
    0
    • G Offline
      G Offline
      Gabriele Cirillo
      wrote on last edited by
      #7

      uh ok, sorry, i use the first, your right!

      now... i have tried this, but always unsuccesfully (i just change the pin)

      // Enable debug prints to serial monitor
      #define MY_DEBUG
      
      // Enable and select radio type attached
      #define MY_RADIO_NRF24
      
      #define SN "RelayButtonArray"
      #define SV "1.0"
      
      #include <MySensors.h>
      #include <SPI.h>
      #include <Bounce2.h>
      #define RELAY_ON 0                      // switch around for ACTIVE LOW / ACTIVE HIGH relay
      #define RELAY_OFF 1
      //
      
      #define noRelays 4                     //2-4
      const int relayPin[] = {30, 31, 32, 33};       //  switch around pins to your desire
      const int buttonPin[] = {28, 29, 27, 26};   //  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];
      
      /*
        void before() {
        for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
          // Then set relay pins in output mode
          pinMode(pin, OUTPUT);
          // Set relay to last known state (using eeprom storage)
          digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
        }
        }*/
      
      void setup() {
        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);
        }
      }
      
      void presentation()
      {
        // Send the sketch version information to the gateway and Controller
        sendSketchInfo(SN, SV);
      
        wait(100);
      
        for (int i = 0; i < noRelays; i++)
          present(i, S_LIGHT);                               // present sensor to gateway
      
        wait(100);
      }
      
      
      void loop()
      {
        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 );
      
            }
      
          }
        }
        //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);
      }```
      rejoe2R 1 Reply Last reply
      0
      • G Gabriele Cirillo

        uh ok, sorry, i use the first, your right!

        now... i have tried this, but always unsuccesfully (i just change the pin)

        // Enable debug prints to serial monitor
        #define MY_DEBUG
        
        // Enable and select radio type attached
        #define MY_RADIO_NRF24
        
        #define SN "RelayButtonArray"
        #define SV "1.0"
        
        #include <MySensors.h>
        #include <SPI.h>
        #include <Bounce2.h>
        #define RELAY_ON 0                      // switch around for ACTIVE LOW / ACTIVE HIGH relay
        #define RELAY_OFF 1
        //
        
        #define noRelays 4                     //2-4
        const int relayPin[] = {30, 31, 32, 33};       //  switch around pins to your desire
        const int buttonPin[] = {28, 29, 27, 26};   //  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];
        
        /*
          void before() {
          for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
            // Then set relay pins in output mode
            pinMode(pin, OUTPUT);
            // Set relay to last known state (using eeprom storage)
            digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
          }
          }*/
        
        void setup() {
          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);
          }
        }
        
        void presentation()
        {
          // Send the sketch version information to the gateway and Controller
          sendSketchInfo(SN, SV);
        
          wait(100);
        
          for (int i = 0; i < noRelays; i++)
            present(i, S_LIGHT);                               // present sensor to gateway
        
          wait(100);
        }
        
        
        void loop()
        {
          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 );
        
              }
        
            }
          }
          //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);
        }```
        rejoe2R Offline
        rejoe2R Offline
        rejoe2
        wrote on last edited by
        #8

        @gabriele-cirillo Can you please be a little more specific: What exactly is not working? What's the serial output?
        I have to admit, I never directly used @korttoma's sketch directly, but used the principles he applied to make own things happen. So if you want to dig into this a little more in depth, you may have a look at these here and here (the later not yet finisched, but button+relay-wise working).

        Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

        1 Reply Last reply
        0
        • G Offline
          G Offline
          Gabriele Cirillo
          wrote on last edited by
          #9

          If i trigger the input pin, the output still to LOW

          this is the serial code

          TDI:TSL
          T NODE,CP=RNNNA---,VER=2.2.0
          25 TSM:INIT
          26 TSF:WUR:MS=0
          33 !TSM:INIT:TSP FAIL
          35 TSM:FAIL:CNT=1
          37 TSM:FAIL:DIS
          38 TSF:TDI:TSL
          &"⸮q_⸮⸮!⸮D⸮⸮⸮s⸮ ⸮⸮ED⸮⸮A
          Et⸮EE⸮⸮AEň⸮1⸮⸮⸮
          1 Reply Last reply
          0
          • rejoe2R Offline
            rejoe2R Offline
            rejoe2
            wrote on last edited by
            #10

            OK, this is a different topic: as long as there is no uplink to the controller, your sketch will never enter loop() and so on.
            Sounds like there is no radio module attached? Please see hints in troubleshooting, especially: log parser.
            As a workaround you may get node starting without radio by using

            #define MY_TRANSPORT_WAIT_READY_MS 3000
            

            But some remark wrt applying fixes like that: It's kind of frustrating to discuss basic things when beeing asked for help on really advanced features. Imo, you might get some more basic examples to work first before starting projects like the one here.

            Just my2ct...

            Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

            G 1 Reply Last reply
            0
            • G Offline
              G Offline
              Gabriele Cirillo
              wrote on last edited by
              #11

              you are right and sorry

              But I search for a simply code for x input and for x relay asking the best method for the code...

              now i start from 0 and try simply code! ty

              1 Reply Last reply
              0
              • rejoe2R rejoe2

                OK, this is a different topic: as long as there is no uplink to the controller, your sketch will never enter loop() and so on.
                Sounds like there is no radio module attached? Please see hints in troubleshooting, especially: log parser.
                As a workaround you may get node starting without radio by using

                #define MY_TRANSPORT_WAIT_READY_MS 3000
                

                But some remark wrt applying fixes like that: It's kind of frustrating to discuss basic things when beeing asked for help on really advanced features. Imo, you might get some more basic examples to work first before starting projects like the one here.

                Just my2ct...

                G Offline
                G Offline
                Gabriele Cirillo
                wrote on last edited by
                #12

                @rejoe2 ok, i try with succesfully to make a sketch like my initial requests with array function :)

                for you is so simply, but for me no! :p

                all is ok, only a problem: at boot, at the first pression of one button (pin 27, 28 or 29) put high all 3 output... with another pression of button, all go ok... you see something that i can't see? :) ty

                int buttPin[3] = {27,28,29};// you do not want to use digital pins 0 or 1
                int relayPin[3] = {30,31,32};
                int lastButtState[3];
                int buttState[3];
                int debounceDelay = 50;
                int ledState[3];
                int reading[3];
                unsigned long lastDebounceTime[3];
                    
                void setup() 
                {
                  Serial.begin(115200);
                  for (int i = 0; i < 3; i++)
                  {
                    pinMode(relayPin[i], OUTPUT);
                    pinMode(buttPin[i], INPUT_PULLUP); 
                  }
                }
                
                void loop() 
                {
                  for (int i = 0; i < 3; i++)
                  {
                    reading[i] = digitalRead(buttPin[i]);
                    if (reading[i] != lastButtState[i]) 
                    {
                      lastDebounceTime[i] = millis();
                      lastButtState[i] = reading[i];
                        
                    }
                    if ((millis() - lastDebounceTime[i]) > debounceDelay) {
                       if (buttState[i] != lastButtState[i]) {
                           buttState[i] = lastButtState[i];
                           if (buttState[i] == HIGH) {
                                 ledState[i] = !ledState[i];
                                 digitalWrite(relayPin[i], ledState[i]);
                  }
                  }
                 }
                }
                }```
                1 Reply Last reply
                0
                • rejoe2R Offline
                  rejoe2R Offline
                  rejoe2
                  wrote on last edited by
                  #13

                  To me, these things are also quite close to magic, don't worry :smile: .
                  I don't know, what is going on at bootup, but my assumption wold be the PINs are somehow floating, as you didn't do some kind of initialisation in setup() or comparable.

                  Add. remark:
                  You also deleted the original debouncing function - that's possible, but as you seem to be quite good in adopting things (imo you made a big step forward). So you may review your code once more wrt that; could be the debouncer-function does a little more than just wait some time ;-) .

                  Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

                  G 1 Reply Last reply
                  0
                  • rejoe2R rejoe2

                    To me, these things are also quite close to magic, don't worry :smile: .
                    I don't know, what is going on at bootup, but my assumption wold be the PINs are somehow floating, as you didn't do some kind of initialisation in setup() or comparable.

                    Add. remark:
                    You also deleted the original debouncing function - that's possible, but as you seem to be quite good in adopting things (imo you made a big step forward). So you may review your code once more wrt that; could be the debouncer-function does a little more than just wait some time ;-) .

                    G Offline
                    G Offline
                    Gabriele Cirillo
                    wrote on last edited by
                    #14

                    @rejoe2 i use the pullup input and i try in setup to write relayPin to LOW, to put LOW the variable but with no success...

                    ps: i deleted what??!?!? :P

                    rejoe2R 1 Reply Last reply
                    0
                    • G Gabriele Cirillo

                      @rejoe2 i use the pullup input and i try in setup to write relayPin to LOW, to put LOW the variable but with no success...

                      ps: i deleted what??!?!? :P

                      rejoe2R Offline
                      rejoe2R Offline
                      rejoe2
                      wrote on last edited by
                      #15

                      @gabriele-cirillo said in More relay and push botton:

                      ps: i deleted what??!?!? :P

                      Your code was to some extend very close to @korttoma's. So I thougt you started with a "cleaner" version ot this base and let out some parts - especially things related to

                      #include <Bounce2.h>
                      

                      Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        Gabriele Cirillo
                        wrote on last edited by
                        #16

                        ahhhhh! no, I start from 0 with an example found on the web, the @korttoma sketch is too complicated for me :P

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        20

                        Online

                        11.7k

                        Users

                        11.2k

                        Topics

                        113.1k

                        Posts


                        Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • MySensors
                        • OpenHardware.io
                        • Categories
                        • Recent
                        • Tags
                        • Popular