How To: Make a Simple/Cheap Scene Controller (with video)


  • Admin

    Hi Everyone,

    I recently decided to make a very simple/cheap scene controller with a 1x4 membrane keypad. I normally try to automate as many things as I can but sometimes it's just nice to have some physical buttons. I have this in my bedroom so I can turn on/off lights, fans etc without having to open my eyes. Hopefully this will be of use to someone...

    Fritzing Scene Controller Wiring_bb.png

    $4 Arduino Home Automation Control with MySensors – 08:19
    — Pete B

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - PeteWill
     * 
     * DESCRIPTION
     * A simple scene controller for use with MySensors.  8 scenes can be executed.
     * 4 with short button presses and 4 with long button presses (held .5 seconds or more).
     * Watch a how to video here: https://youtu.be/KMGj5Bi7vL0 
     */
    
    #include <Keypad.h>
    #include <SPI.h>
    #include <MySensor.h>
    
    #define NODE_ID 14 // or set to AUTO if you want gw to assign a NODE_ID for you.
    #define SN "Scene Controller"
    #define SV "1.0"
    
    
    #define KEYPAD_CHILD_ID 0
    
    MySensor gw;
    MyMessage scene(KEYPAD_CHILD_ID, V_SCENE_ON);
    
    const byte ROWS = 4; //four rows
    const byte COLS = 1; //three columns
    char keys[ROWS][COLS] = {
      {'1'},
      {'2'},
      {'3'},
      {'4'}
    };
    
    byte rowPins[ROWS] = {6, 7, 4, 5}; //connect to the row pinouts of the keypad
    byte colPins[COLS] = {8}; //connect to the column pinouts of the keypad
    
    Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
    byte lastState;
    
    
    void setup() {
      gw.begin(NULL, NODE_ID);
      gw.sendSketchInfo(SN, SV);
      gw.present(KEYPAD_CHILD_ID, S_SCENE_CONTROLLER);
      keypad.addEventListener(keypadEvent); // Add an event listener for this keypad
    }
    
    void loop() {
      char key = keypad.getKey();
    }
    
    void keypadEvent(KeypadEvent key) {
      switch (keypad.getState()) {
    
        case PRESSED:
          lastState = 1;
          break;
    
        case HOLD:
          lastState = 2;
          break;
    
        case RELEASED:
          int keyInt = key - '0'; //Quick way to convert Char to Int so it can be sent to controller
          if (lastState == 2) {
            keyInt = keyInt + 4; //If button is held, add 4.  If using more than 4 buttons this number will need to be changed
          }
          gw.send(scene.set(keyInt));
          break;
      }
    }
    

    Here is a link for a keypad: http://www.ebay.com/itm/171505110526?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT

    Here is another option for a scene controller keypad contributed by @AWI. It has capacitive touch buttons and an interrupt to battery power can be used. Thanks @AWI!
    http://forum.mysensors.org/topic/2001/how-to-make-a-simple-cheap-scene-controller-with-video/14


  • Admin

    Great work @petewill!


  • Hero Member

    Great & simple.. for a "touch" version (similar code/connection, price is for 10 pcs. 😉 )


  • Admin

    I have this on the lab-bench also... But I haven't had time to try it out yet.

    http://www.ebay.com/itm/251631847404?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT


  • Admin

    @AWI & @hek thanks, yeah, I have been wanting to play with capacitive touch too! Someday soon hopefully 🙂



  • Another stellar video @petewill ! Thanks! Now time to 3D print a box and add batteries for your night stand.



  • @petewill said:

    @AWI & @hek thanks, yeah, I have been wanting to play with capacitive touch too! Someday soon hopefully 🙂

    Cool! What do you think about a battery version of this? (short battery life?)



  • @petewill

    Where did you get your touchpad?


  • Hero Member

    @petewill To get you started with capacitive: the ones with the TTPxxx chip in 4/8/16 button versions like have pins with leds for every button. Can also generate an interrupt at keypress (so no need to poll the keyboard in low-power applications). And can can also be used with a simple serial protocol (only 2/3 pins needed). I would be happy to supply the code (but don't want to mess-up your great topic any further 😉 )


  • Admin

    @DrJeff Yes, that would be great! Someday...

    @msebbe Yes, this version would have short battery life because it's not sleeping. But, if @AWI posts his code a battery version sounds possible.

    @Hoffan you can get them here: http://www.ebay.com/itm/171505110526?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT

    @AWI That would be great if you could supply the code for a capacitive sleeping node! If you could supply the wiring diagram and parts list also I will add it to the first post as another option (giving credit to you of course). Thanks!



  • @petewill really nice thing which can also be a nice solution for "old" people leaving with us.
    I was thinking to use matrix keypad which I own.
    Any help from community? probably analog input should be used as digital input because of missing inputs on nano...
    link


  • Hero Member

    @Tomasz-Pazio The keyboard you mention is a standard 4 x 4 matrix keyboard. You can wire it with 8 pins for which you can also use the analog pins. These can be addressed by A0, A1, etc. The library @petewill used in the sketch can handle this keyboard.


  • Admin

    @Tomasz-Pazio You will also need to adjust the code as mine is specifically designed to use 4 buttons. It shouldn't be too difficult to modify though.


  • Hero Member

    Not even close to @petewill 's great build descriptions but here my version the "touch" version which can be battery operated. The used keyboard uses around 150uA. The touch panel generates an interrupt which wakes the arduino, reads the key and swiches a scene.

    upload-10b4b56c-4f8e-4f43-ac06-69217e48adcb

    The code below toggles between 'Scene ON' and 'Scene OFF' and stores the last state in EEPROM.

    All credits to @petewill

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - PeteWill
     *   	   1.1 - AWI
     * 
     * DESCRIPTION
     * A simple scene controller for use with MySensors.  8 scenes can be executed
     *  with an 8 key keypad, make sure to attach the pins and fill keyPins[] resp.
     * Watch a how to video here: https://youtu.be/KMGj5Bi7vL0 
     */
    #include <SPI.h>
    #include <MySensor.h>
    
    #define SN "Scene Controller"
    #define SV "1.0"
    
    #define NODE_ID 14 // or set to AUTO if you want gw to assign a NODE_ID for you.
    const byte KEYPAD_CHILD_ID = 0 ;
    
    MySensor gw;
    MyMessage scene_on(KEYPAD_CHILD_ID, V_SCENE_ON);
    MyMessage scene_off(KEYPAD_CHILD_ID, V_SCENE_OFF);
    
    const int DV = 2;									//DataValid (=key pressed) pin goes there
    const int DV_int = DV-2;							//DataValid interrupt on pin 3 => 1
    const unsigned long SLEEP_TIME = 3600000 ;			// sleep for an hour (or more if you want to)
    volatile boolean DVint = false ; 					// interrupt flag, set by interrupt
    const byte keyPins[] = {A0, A1, A2, A3, 3, 4, 5, 6};// keypad pins, (8 pin keyboard)
    byte keyState[8]  ;									// hold current keystate (copy of EEPROM)
    
    void setup(){
    	gw.begin(NULL, NODE_ID);
    	gw.sendSketchInfo(SN, SV);
    	gw.present(KEYPAD_CHILD_ID, S_SCENE_CONTROLLER);
    	pinMode(DV,INPUT);          					// Data Valid (interrupt)
    	for (int i=0 ; i < sizeof(keyPins); i++){
    		keyState[i] = gw.loadState(i) ; 			// load last Scenestates from EEPROM
    		}
    	}
    
    // loop only if interrupt, else sleep
    void loop(){
        // if so get a key value and send to MySensosrs network
    	byte key = fetchData();    						// if so fetch key
        Serial.println(key);							// serial print as binary
    	if (key > 0){									// (key-1) is used as index
    		boolean keyVal = !gw.loadState(key-1);		// use lastState from EEPROM and toggle
    		gw.saveState(key-1, keyVal);				// save new state to EEPROM
    		if (keyVal) gw.send(scene_on.set(key-1));	// set the Scene On or Off
    		else gw.send(scene_off.set(key-1));
    		}
        DVint=false;									// reset interrupt flag
    	gw.sleep(DV_int, RISING, SLEEP_TIME);			// node wakes up on key interrupt or time
    	Serial.println(" key pressed or time trigger ");
    }
    
    // interrupt routine, only sets flag
    void intrp(){DVint = true;};
    
    // fetch serial data, only highest number key is returned
    byte fetchData(){
    	int Key=0;                        				// default key = 0 (nothing pressed)
    	for (byte i = 0 ; i < sizeof(keyPins) ; i++){	// check each key, 
    		if(digitalRead(keyPins[i]) == HIGH) Key=i+1;// set key if pressed (now only highest key)
    		}
    	return Key;										// return Key (0 if no key pressed)
    }
    
    
    


  • ok easy.... two changes and 32 scenes can be managed 🙂

    const byte ROWS = 4;
    const byte COLS = 4;
    byte rowPins[ROWS] = {3, 4, 5, 6};
    byte colPins[COLS] = {A0, A1, A2, A3};
    char keys[ROWS][COLS] = {{'1','2','3','A'},
                             {'4','5','6','B'},
                             {'7','8','9','C'},
                             {'*','0','#','D'}};
    
    
    -----------------------------------------
    
     keyInt = keyInt + 100;
    
    

  • Admin

    @AWI Excellent, thanks for adding the code for the capacitive touch, battery operated sensor! I added a link to your post in my initial post.



  • Hi Petewill
    I 'd like to build your Scene Controller but I am missing the keypad.h library. Where can I get it?

    greetings
    Brom_Snor


  • Admin

    @brom_snor
    Sorry about that. I thought it was part of the default libraries. I must have downloaded it a while ago. This one should work: http://playground.arduino.cc/Code/Keypad



  • Hi all - Another capacitive version with an "out of the bottle" keypad - 4 buttons. Like AWI's version, also toggles between 'Scene ON' and 'Scene OFF', but states are kept in a variable (keyState) - LEDs added for better toogle visibility. Capacitive routine found at http://www.arduino.cc/playground/Code/CapacitiveSensor

    Great post from @petewill and nice version from AWI. Thanks!

    Imagen.jpg

    #include <SPI.h>
    #include <MySensor.h>
    #define SN "Scene Controller"
    #define SV "1.2"
    #define NODE_ID 20 // or set to AUTO if you want gw to assign a NODE_ID for you.
    const byte KEYPAD_CHILD_ID = 1 ;
    MySensor gw;
    MyMessage scene_on(KEYPAD_CHILD_ID, V_SCENE_ON);
    MyMessage scene_off(KEYPAD_CHILD_ID, V_SCENE_OFF);

    long time = 0;
    long debounce = 800;
    const byte keyLeds[] = {14, 15, 16, 17}; //LEDs to turn on/off
    const byte keyPins[] = {2, 3, 4, 5}; //capacitive keys
    byte keyState = B1111; //save LEDs states

    void setup() {
    Serial.begin(9600);
    for (byte i = 0 ; i < sizeof(keyLeds) ; i++)
    { pinMode(keyLeds[i], OUTPUT);
    }
    gw.begin(NULL, NODE_ID);
    gw.sendSketchInfo(SN, SV);
    gw.present(KEYPAD_CHILD_ID, S_SCENE_CONTROLLER);
    }

    void loop() {
    uint8_t pinRead;
    for (byte i = 0 ; i < sizeof(keyPins) ; i++){
    pinRead = readCapacitivePin(keyPins[i]);
    if (pinRead > 2 && millis() - time > debounce) {
    digitalWrite(keyLeds[i], bitRead(keyState,i));
    if (bitRead(keyState,i) == 1){
    gw.send(scene_on.set(keyPins[i]));
    bitWrite(keyState, i, 0);} else
    {gw.send(scene_off.set(keyPins[i]));
    bitWrite(keyState, i, 1);}
    time = millis();
    }
    }
    }

    // — readCapPin found at http://www.arduino.cc/playground/Code/CapacitiveSensor
    uint8_t readCapacitivePin(int pinToMeasure) {

    // Variables used to translate from Arduino to AVR pin naming
    volatile uint8_t* port;
    volatile uint8_t* ddr;
    volatile uint8_t* pin;
    // Here we translate the input pin number from
    // Arduino pin number to the AVR PORT, PIN, DDR,
    // and which bit of those registers we care about.
    byte bitmask;
    port = portOutputRegister(digitalPinToPort(pinToMeasure));
    ddr = portModeRegister(digitalPinToPort(pinToMeasure));
    bitmask = digitalPinToBitMask(pinToMeasure);
    pin = portInputRegister(digitalPinToPort(pinToMeasure));
    // Discharge the pin first by setting it low and output
    *port &= ~(bitmask);
    *ddr |= bitmask;
    delay(1);
    // Make the pin an input with the internal pull-up on
    *ddr &= ~(bitmask);
    *port |= bitmask;

    // Now see how long the pin to get pulled up. This manual unrolling of the loop
    // decreases the number of hardware cycles between each read of the pin,
    // thus increasing sensitivity.
    uint8_t cycles = 17;
    if (*pin & bitmask) { cycles = 0;}
    else if (*pin & bitmask) { cycles = 1;}
    else if (*pin & bitmask) { cycles = 2;}
    else if (*pin & bitmask) { cycles = 3;}
    else if (*pin & bitmask) { cycles = 4;}
    else if (*pin & bitmask) { cycles = 5;}
    else if (*pin & bitmask) { cycles = 6;}
    else if (*pin & bitmask) { cycles = 7;}
    else if (*pin & bitmask) { cycles = 8;}
    else if (*pin & bitmask) { cycles = 9;}
    else if (*pin & bitmask) { cycles = 10;}
    else if (*pin & bitmask) { cycles = 11;}
    else if (*pin & bitmask) { cycles = 12;}
    else if (*pin & bitmask) { cycles = 13;}
    else if (*pin & bitmask) { cycles = 14;}
    else if (*pin & bitmask) { cycles = 15;}
    else if (*pin & bitmask) { cycles = 16;}

    // Discharge the pin again by setting it low and output
    // It’s important to leave the pins low if you want to
    // be able to touch more than 1 sensor at a time – if
    // the sensor is left pulled high, when you touch
    // two sensors, your body will transfer the charge between
    // sensors.
    *port &= ~(bitmask);
    *ddr |= bitmask;

    return cycles;
    }



  • @Fred-LaR WOW!
    You just perked up my intrest got to try this one!



  • Anybody made some Nice enclosure for this?


  • Admin

    @Fred-LaR That's a great idea. Nice!



  • My domoticz found S_SCENE_CONTROLLER sensor but there is not any devices for such a hardware. Do you know how can I control scene with it?


  • Mod

    @Michal-Mormon which version of Domoticz are you using?



  • Domoticz v.2.4633



  • lua script?


  • Admin

    @Michel---It said:

    lua script?

    Sorry, not sure what you're asking.



  • @Michal-Mormon
    S_SCENE_CONTROLLER can send messages V_SCENE_ON and V_SCENE_OFF. If you send those messages for the first time, domoticz will create new switch with the name 'scene'.



  • @Maciej-Kulawik
    I found it as 'Scene' switch but this example send only V_SCENE_ON the first time (the switch is still ON later)



  • @Michal-Mormon
    Yes, exactly! It looks like without scene_off sent from scene controller in domoticz it will be one-time action (since if this scene switch is switched on you cannot switch it on again). You can try to setup automatic switching it off in domoticz.



  • I added 2 lines and works perfect with Domoticz:

    MyMessage scene(KEYPAD_CHILD_ID, V_SCENE_ON);
    MyMessage scene2(KEYPAD_CHILD_ID, V_SCENE_OFF); //added line

    gw.send(scene.set(keyInt));
    gw.send(scene2.set(keyInt)); //added line



  • @Michal-Mormon awesome!



  • @petewill Have you updated this code to 2.0 yet? What is different?


  • Admin



  • @hek
    Yes I read but was soooo lazy! I just wanted it already done 😬

    I just update one of my old sensors it was a simple single relay. My others are way more complicated! I will give this a try.


  • Admin

    @DrJeff said:

    @petewill Have you updated this code to 2.0 yet? What is different?

    Not yet, but I'm slowly working on it. 🙂 The link hek gave is very helpful if you want to try it yourself. It's a good way to learn about the new features in 2.0.



  • @petewill Any news about the 2.0 code? I am in the process of making scene controllers in the different rooms but can't get it to work.


  • Admin

    @dvr123 No, not yet 😬
    My thermostat project has taken priority over everything because I need one in my house. I have a prototype working but I need to finish it. It is taking much longer than expected and I also haven't had much time lately. Sorry.



  • Hi Guys,

    I've been trying to compile this sketch without any success. I keep getting the following error when compiling

    #error No forward link or gateway feature activated. This means nowhere to send messages! Pretty pointless.
    

    Can anyone help? thanks


  • Admin

    @Matt-Pitts I still haven't had time to convert/test the sketch for version 2.0. If you want to take a shot at it there is info here: https://forum.mysensors.org/topic/4276/converting-a-sketch-from-1-5-x-to-2-0-x

    I will get to it eventually I just have other projects that are consuming my time right now. Sorry.



  • This post is deleted!


  • Why is Lukas' post deleted? I have got it in the mail and it seems to compile correctly.
    The only thing is that Domoticz only recognizes the button on the first click, after that Domoticz isn't receiving anything.


  • Admin

    Not sure why @Łukasz-Kostrzewa deleted it. As an administrator, I can still see the post.



  • Hi
    I delated my post because it pasted wrong. I try to paste it correctly but don't know how:( (hek - maybe You as an administrator can paste it corectly?)

    dvr123 have right that this code recognize the buton only on the first click:(
    Mayby someone knows how to modify the code to work like normal buton (not the scene controller)?

    Best regards



  • I am okey with the scene button, but it has to work more than one time 😄


  • Admin

    @Łukasz-Kostrzewa, paste code within 3 backtick characters, like this:

    ```
    Insert Code Here
    ```



  • You could do that but it is not a great solution.
    Add to Domoticz all 8 switches (4 when pressed and 4 when hold).
    In first switch (pressed switch) add Slave device (first hold switch). Do it for all 4 switches.
    To 5,6,7,8 switches add Slave Device (1,2,3,4 switch).
    It works like that:
    When You press First number on keyboard it change state to ON and 5 to OFF
    When You Hold first number on keybord it change state of 5 switch to ON and first to OFF.
    Next You have to make some events to trigger some scenes or lights
    I have hope I managed to explain what I mean 🙂
    Best regards



  • The 2.0 version code:

    #define MY_DEBUG
    #define MY_RADIO_NRF24
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <Keypad.h>
    
    #define NODE_ID 14 // or set to AUTO if you want gw to assign a NODE_ID for you.
    #define SN "Scene Controller"
    #define SV "1.0"
    #define KEYPAD_CHILD_ID 95
    
    MyMessage scene(KEYPAD_CHILD_ID, V_SCENE_ON); 
    MyMessage scene2(KEYPAD_CHILD_ID, V_SCENE_OFF); 
    
    const byte ROWS = 4; //four rows
    const byte COLS = 1; //three columns
    char keys[ROWS][COLS] = {
      {'1'},
      {'2'},
      {'3'},
      {'4'}
    };
    
    byte rowPins[ROWS] = {6, 7, 4, 5}; //connect to the row pinouts of the keypad
    byte colPins[COLS] = {8}; //connect to the column pinouts of the keypad
    
    Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
    byte lastState;
    
    
    void setup() {
      
      sendSketchInfo(SN, SV);
      present(KEYPAD_CHILD_ID, S_SCENE_CONTROLLER);
      keypad.addEventListener(keypadEvent);
    }
    
    void loop() {
      char key = keypad.getKey();
    }
    
    void keypadEvent(KeypadEvent key) {
      switch (keypad.getState()) {
    
        case PRESSED:
          lastState = 1;
          break;
    
        case HOLD:
          lastState = 2;
          break;
    
        case RELEASED:
          int keyInt = key - '0'; //Quick way to convert Char to Int so it can be sent to controller
          if (lastState == 2) {
            keyInt = keyInt + 4; //If button is held, add 4.  If using more than 4 buttons this number will need to be changed
          }
          send(scene.set(keyInt));
          break;
     }
    }       
    


  • That's actually much easier! But how do we manage to get it working more than one time? Because I can add the button but after that I can't control it. I can't see the button presses in the Domoticz log either.



  • @Łukasz-Kostrzewa said:

    send(scene.set(keyInt));

    Hi
    After
    send(scene.set(keyInt));
    Add
    send(scene2.set(keyInt));
    Write this code to arduino (now You can OFF all the switches) Next delete code You added and write it to Arduino once again but now in Domoticz make modifications I writed couple post higher. When You do that You will be able to control 4 scenes and another 4 will bo halpefull to OFF the first4 (when You hold the buttons)



  • Complete code version 2 with short press ON long press OFF.

    #define MY_DEBUG
    #define MY_RADIO_NRF24
    
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <Keypad.h>
    
    #define NODE_ID AUTO // or set to AUTO if you want gw to assign a NODE_ID for you.
    #define SN "Scene Controller"
    #define SV "2.0"
    #define KEYPAD_CHILD_ID 95
    
    MyMessage scene(KEYPAD_CHILD_ID, V_SCENE_ON); //scene ON
    MyMessage scene2(KEYPAD_CHILD_ID, V_SCENE_OFF); //scene OFF
    
    const byte ROWS = 4; //four rows
    const byte COLS = 1; //one column
    char keys[ROWS][COLS] = {
      {'1'},
      {'2'},
      {'3'},
      {'4'}
    };
    
    byte rowPins[ROWS] = {6, 7, 4, 5}; //connect to the row pinouts of the keypad
    byte colPins[COLS] = {8}; //connect to the column pinouts of the keypad
    
    Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
    byte lastState;
    
    
    void setup() {
      
      sendSketchInfo(SN, SV);
      present(KEYPAD_CHILD_ID, S_SCENE_CONTROLLER);
      keypad.addEventListener(keypadEvent);
    }
    
    void loop() {
      char key = keypad.getKey();
    }
    
    void keypadEvent(KeypadEvent key) {
      switch (keypad.getState()) {
    
        case PRESSED:
          lastState = 1;
          break;
    
        case HOLD:
          lastState = 2;
          break;
    
        case RELEASED:
          int keyInt = key - '0'; //Quick way to convert Char to Int so it can be sent to controller
          if (lastState == 2) {
            //keyInt = keyInt + 4; //If button is held, add 4.  If using more than 4 buttons this number will need to be changed
            send(scene2.set(keyInt));
          } else {
          send(scene.set(keyInt));
          }
          //break;
     }
    }```


  • Hi rechin304
    Thank You for Your code...It's working great!!!
    Best regards



  • @Łukasz-Kostrzewa you are welcome, all thanks goes to @petewill for initial code.



  • Hi Guys,

    I'm so glad that have updated the sketch which i have now been able to compile and get running 🙂

    But this is the message I get in the serial monitor and I only get these messages when I press button 3?? I am using a single row of four buttons as in the original sketch.

    Am I missing something here? I was assuming the message with either be a 1 for a short press and 0 for a long press? I would be really grateful if someone who knows more about this than me is able to help me figure out what's going on?

    TSP:MSG:SEND 11-11-0-0 s=95,c=1,t=19,pt=2,l=2,sg=0,ft=0,st=ok:3
    TSP:MSG:SEND 11-11-0-0 s=95,c=1,t=19,pt=2,l=2,sg=0,ft=0,st=ok:7
    TSP:MSG:SEND 11-11-0-0 s=95,c=1,t=19,pt=2,l=2,sg=0,ft=0,st=ok:7
    TSP:MSG:SEND 11-11-0-0 s=95,c=1,t=19,pt=2,l=2,sg=0,ft=0,st=ok:3
    TSP:MSG:SEND 11-11-0-0 s=95,c=1,t=19,pt=2,l=2,sg=0,ft=0,st=ok:7
    TSP:MSG:SEND 11-11-0-0 s=95,c=1,t=19,pt=2,l=2,sg=0,ft=0,st=ok:7
    TSP:MSG:SEND 11-11-0-0 s=95,c=1,t=19,pt=2,l=2,sg=0,ft=0,st=ok:7
    TSP:MSG:SEND 11-11-0-0 s=95,c=1,t=19,pt=2,l=2,sg=0,ft=0,st=ok:3
    TSP:MSG:SEND 11-11-0-0 s=95,c=1,t=19,pt=2,l=2,sg=0,ft=0,st=ok:3
    TSP:MSG:SEND 11-11-0-0 s=95,c=1,t=19,pt=2,l=2,sg=0,ft=0,st=ok:3
    TSP:MSG:SEND 11-11-0-0 s=95,c=1,t=19,pt=2,l=2,sg=0,ft=0,st=ok:7
    TSP:MSG:SEND 11-11-0-0 s=95,c=1,t=19,pt=2,l=2,sg=0,ft=0,st=ok:7
    TSP:MSG:SEND 11-11-0-0 s=95,c=1,t=19,pt=2,l=2,sg=0,ft=0,st=ok:7
    TSP:MSG:SEND 11-11-0-0 s=95,c=1,t=19,pt=2,l=2,sg=0,ft=0,st=ok:7
    TSP:MSG:SEND 11-11-0-0 s=95,c=1,t=19,pt=2,l=2,sg=0,ft=0,st=ok:3
    TSP:MSG:SEND 11-11-0-0 s=95,c=1,t=19,pt=2,l=2,sg=0,ft=0,st=ok:3
    TSP:MSG:SEND 11-11-0-0 s=95,c=1,t=19,pt=2,l=2,sg=0,ft=0,st=ok:3
    
    


  • @Matt-Pitts on 3 is short press and in code is scene on and on long press is 7 scene off

    MyMessage scene(KEYPAD_CHILD_ID, V_SCENE_ON); //scene ON
    MyMessage scene2(KEYPAD_CHILD_ID, V_SCENE_OFF); //scene OFF
    
    
            send(scene2.set(keyInt));
          } else {
          send(scene.set(keyInt));```


  • @rechin304 said:

    @Matt-Pitts on 3 is short press and in code is scene on and on long press is 7 scene off

    MyMessage scene(KEYPAD_CHILD_ID, V_SCENE_ON); //scene ON
    MyMessage scene2(KEYPAD_CHILD_ID, V_SCENE_OFF); //scene OFF
    
    
            send(scene2.set(keyInt));
          } else {
          send(scene.set(keyInt));```
    

    Hi Not sure if that helps me, I'm struggling to understand how this actually works? as I said the only button doing anything is 3, do I need to modify the sketch to get the other buttons working?



  • @Matt-Pitts then you need to check your conenction to all wires and if are correctly. Check first post as there is a picture.



  • @rechin304

    Not getting the Hold on buttons working, everything else works. Any ideas why the long press doesn't work?



  • Never mind part of the code was commented out. I wasn't sure because I just started to use OTA. But it's all good now its working fine. Here it is:

    #define MY_DEBUG
    #define MY_RADIO_NRF24
    #define MY_OTA_FIRMWARE_FEATURE
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <Keypad.h>
    
    #define NODE_ID AUTO // or set to AUTO if you want gw to assign a NODE_ID for you.
    #define SN "Scene Controller"
    #define SV "2.0"
    #define KEYPAD_CHILD_ID 95
    
    MyMessage scene(KEYPAD_CHILD_ID, V_SCENE_ON); //scene ON
    MyMessage scene2(KEYPAD_CHILD_ID, V_SCENE_OFF); //scene OFF
    
    const byte ROWS = 4; //four rows
    const byte COLS = 1; //one column
    char keys[ROWS][COLS] = {
      {'1'},
      {'2'},
      {'3'},
      {'4'}
    };
    
    byte rowPins[ROWS] = {6, 7, 4, 5}; //connect to the row pinouts of the keypad
    byte colPins[COLS] = {8}; //connect to the column pinouts of the keypad
    
    Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
    byte lastState;
    
    
    void setup() {
      
      sendSketchInfo(SN, SV);
      present(KEYPAD_CHILD_ID, S_SCENE_CONTROLLER);
      keypad.addEventListener(keypadEvent);
    }
    
    void loop() {
      char key = keypad.getKey();
    }
    
    void keypadEvent(KeypadEvent key) {
      switch (keypad.getState()) {
    
        case PRESSED:
          lastState = 1;
          break;
    
        case HOLD:
          lastState = 2;
          break;
    
        case RELEASED:
          int keyInt = key - '0'; //Quick way to convert Char to Int so it can be sent to controller
          if (lastState == 2) {
            keyInt = keyInt + 4; //If button is held, add 4.  If using more than 4 buttons this number will need to be changed
            send(scene2.set(keyInt));
          } else {
          send(scene.set(keyInt));
          }
          break;
     }
    }
    


  • @DrJeff ok, glad to hear you solved.



  • Hello,

    Here an update of the original sketch with a small modif to add a buzzer for validating the keypress.

    It will beep once for short key press and twice for the long one.

    The buzzer is connected on D3 and the beep length can be adjusted in the function.

    Your feedback and improvement ideas are welcome.

    Best regards,

    Jef

    #define MY_DEBUG
    #define MY_RADIO_NRF24
    
    // Set LOW transmit power level as default, if you have an amplified NRF-module and
    // power your radio separately with a good regulator you can turn up PA level.
    /* - RF24_PA_MIN = -18dBm
     * - RF24_PA_LOW = -12dBm
     * - RF24_PA_HIGH = -6dBm
     * - RF24_PA_MAX = 0dBm
     */
    #define MY_RF24_PA_LEVEL RF24_PA_HIGH
    
    // Enabled repeater feature for this node
    #define MY_REPEATER_FEATURE
    
    // activate signal report
    // #define MY_SIGNAL_REPORT_ENABLED
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <Keypad.h>
    
    #define NODE_ID AUTO // or set to AUTO if you want gw to assign a node id for you.
    #define SN "Keypad Scene Controller"
    #define SV "2.1"
    #define KEYPAD_CHILD_ID 95
    
    MyMessage scene(KEYPAD_CHILD_ID, V_SCENE_ON); //scene ON
    MyMessage scene2(KEYPAD_CHILD_ID, V_SCENE_OFF); //scene OFF
    
    int buzzer = 3; // buzzer pin
    
    
    const byte ROWS = 4; //four rows
    const byte COLS = 1; //one column
    char keys[ROWS][COLS] = {
      {'1'},
      {'2'},
      {'3'},
      {'4'}
    };
    byte rowPins[ROWS] = {6, 7, 4, 5}; //connect to the row pinouts of the keypad
    byte colPins[COLS] = {8}; //connect to the column pinouts of the keypad
    
    Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
    byte lastState;
    
    
    void setup() {
      
      sendSketchInfo(SN, SV);
      present(KEYPAD_CHILD_ID, S_SCENE_CONTROLLER);
      keypad.addEventListener(keypadEvent);
      pinMode(buzzer, OUTPUT);
    }
    /*
     * Declare the buzzer function.
     * Adjust the delay to fit your preferences
     */
    void beep()
    {
      digitalWrite(buzzer, HIGH);
      delay(50);
      digitalWrite(buzzer, LOW);
    }
    
    void loop() {
      char key = keypad.getKey();
    }
    
    void keypadEvent(KeypadEvent key) {
      switch (keypad.getState()) {
    
        case PRESSED:
          lastState = 1;
          beep();
          break;
    
        case HOLD:
          lastState = 2;
          beep();
          break;
    
        case RELEASED:
          int keyInt = key - '0'; //Quick way to convert Char to Int so it can be sent to controller
          if (lastState == 2) {
            //keyInt = keyInt + 4; //If button is held, add 4.  If using more than 4 buttons this number will need to be changed
            send(scene2.set(keyInt));
          } else {
          send(scene.set(keyInt));
          }
          //break;
     }
    }
    


  • I want to use this mysensors touch sensor to adjust a selector switch in domoticz, so if I select scene 1 the selector switch in Domoticz goes to 10%, and scene 2 to 20% and so on. Is that possible and how do I do that?


Log in to reply
 

Suggested Topics

  • 8
  • 3
  • 44
  • 90
  • 2
  • 1

2
Online

11.4k
Users

11.1k
Topics

112.7k
Posts