4 x 4 Keypad array

397 views
Skip to first unread message

David Caparrós

unread,
Aug 14, 2017, 9:47:07 PM8/14/17
to Node-RED

Hello Guys

I'm trying to get working a 4 x 4 keypad through one analog input from an arduino mega board flashed with standard firmdata to be able to handle it with node-red.

To make it easier I put some resistors on the keypad matrix on the way that I get a difference resistance depending on which key is pressed what makes that I can directly read the value on the analog channel and supposed I should convert the value to identify which key had being pressed.

So for instance pressing Key "4" I get a value on the analog channel of 880 (number).

First of all I'm trying to write a function that return me the real pressed key by comparing the analog value with a tolerance, so as example if value is between 775 and 885 should return me that pressed key was "4".

This is just my first test and I don't know why my written function does not return nothing however I see that value delivered at the inlet of the function is correct:


15/8/2017 3:29:55node: 3ab06a3d.528c56A0 : msg.payload : number
880

My flow is:




Function I wrote:

var originalval = msg.payload;

if (960<originalval<970){
msg.payload = "1";
}

else  if (975<originalval<986){
msg.payload = "2";
 }

else if (995<<originalval<1006){
msg.payload = "3";
}

else if (875<originalval<885){
msg.payload = "4";
}

else if (890<originalval<901){
msg.payload = "5";
}

else if (905<originalval<915){
msg.payload = "6";
}

else if (805<originalval<815){
msg.payload = "7";
}

else if (818<originalval<828){
msg.payload = "8";
}

else if (832<originalval<842){
msg.payload = "9";
}

else if (755<originalval<765){
msg.payload = "0";
}

else if (745<originalval<755){
msg.payload = "*";
}

else if (768<originalval<778){
msg.payload = "#";
}

else if (1015<originalval<1025){
msg.payload = "A";
}

else if (923<originalval<932){
msg.payload = "B";
}

else if (846<originalval<856){
msg.payload = "C";
}

else if (780<originalval<790){
msg.payload = "D";
return msg;
}

else
  return null;

[{"id":"a18b36fa.650848","type":"function","z":"c32149c9.68d7d8","name":"","func":"var originalval = msg.payload;\n\nif (960<originalval<970){\nmsg.payload = \"1\";\n}\n\nelse  if (975<originalval<986){\nmsg.payload = \"2\";\n }\n\nelse if (995<<originalval<1006){\nmsg.payload = \"3\";\n}\n\nelse if (875<originalval<885){\nmsg.payload = \"4\";\n}\n\nelse if (890<originalval<901){\nmsg.payload = \"5\";\n}\n\nelse if (905<originalval<915){\nmsg.payload = \"6\";\n}\n\nelse if (805<originalval<815){\nmsg.payload = \"7\";\n}\n\nelse if (818<originalval<828){\nmsg.payload = \"8\";\n}\n\nelse if (832<originalval<842){\nmsg.payload = \"9\";\n}\n\nelse if (755<originalval<765){\nmsg.payload = \"0\";\n}\n\nelse if (745<originalval<755){\nmsg.payload = \"*\";\n}\n\nelse if (768<originalval<778){\nmsg.payload = \"#\";\n}\n\nelse if (1015<originalval<1025){\nmsg.payload = \"A\";\n}\n\nelse if (923<originalval<932){\nmsg.payload = \"B\";\n}\n\nelse if (846<originalval<856){\nmsg.payload = \"C\";\n}\n\nelse if (780<originalval<790){\nmsg.payload = \"D\";\nreturn msg;\n}\n\nelse\n  return null;","outputs":1,"noerr":0,"x":610,"y":500,"wires":[["6f0a1438.ff182c","3ab06a3d.528c56"]]}]


Once this is working (not as for now....) I wanted to see how to handle to be able to read 4 digits to get a passcode,  so for instance be able to identify "123#"  and then get a msg otherwise return nothing or incorrect passw, any advice is really welcome as for now I have no idea how I can manage this, maybe store first message on msg1, second on msg2, third on msg3 and forth on msg4, compare this as an array to the predefined passcode and then return correct or incorrect message and be able to get ready to handle a new combination afterwards.

Thanks in advance for your time, I'm trying to learn a bit from you.

Regards 

David Caparrós

unread,
Aug 14, 2017, 10:22:35 PM8/14/17
to Node-RED
With a rudimentary function I make work the return of the key pressed as follows:




Flow:

[{"id":"44074231.bcd15c","type":"switch","z":"c32149c9.68d7d8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"btwn","v":"960","vt":"num","v2":"970","v2t":"num"},{"t":"btwn","v":"975","vt":"num","v2":"985","v2t":"num"},{"t":"btwn","v":"995","vt":"num","v2":"1006","v2t":"num"},{"t":"btwn","v":"875","vt":"num","v2":"885","v2t":"num"},{"t":"btwn","v":"890","vt":"num","v2":"900","v2t":"num"},{"t":"btwn","v":"905","vt":"num","v2":"915","v2t":"num"},{"t":"btwn","v":"805","vt":"num","v2":"815","v2t":"num"},{"t":"btwn","v":"816","vt":"num","v2":"828","v2t":"num"},{"t":"btwn","v":"832","vt":"num","v2":"842","v2t":"num"},{"t":"btwn","v":"756","vt":"num","v2":"765","v2t":"num"},{"t":"btwn","v":"745","vt":"num","v2":"755","v2t":"num"},{"t":"btwn","v":"768","vt":"num","v2":"778","v2t":"num"},{"t":"btwn","v":"1015","vt":"num","v2":"1025","v2t":"num"},{"t":"btwn","v":"923","vt":"num","v2":"933","v2t":"num"},{"t":"btwn","v":"846","vt":"num","v2":"856","v2t":"num"},{"t":"btwn","v":"780","vt":"num","v2":"790","v2t":"num"},{"t":"else"}],"checkall":"true","outputs":17,"x":350,"y":1000,"wires":[["3ce27f8a.e574a"],["4619b4d3.2100ec"],["3d1348b2.2a51d8"],["d9985705.6c2f18"],["d69939c.05279c8"],["437121a3.66f67"],["d92e654a.72c858"],["7733d9c5.e03a98"],["395878b9.294958"],["f51d469e.624d08"],["6ea5a08b.b646f"],["ad92bb61.5018a8"],["ba832b56.779de8"],["72539223.ffb7dc"],["d8c2166c.c65018"],["3847b55f.55d59a"],["bf41963e.161038"]]},{"id":"4619b4d3.2100ec","type":"change","z":"c32149c9.68d7d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":700,"wires":[["64178689.4b6aa8"]]},{"id":"3d1348b2.2a51d8","type":"change","z":"c32149c9.68d7d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"3","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":740,"wires":[["64178689.4b6aa8"]]},{"id":"d9985705.6c2f18","type":"change","z":"c32149c9.68d7d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"4","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":780,"wires":[["64178689.4b6aa8"]]},{"id":"d69939c.05279c8","type":"change","z":"c32149c9.68d7d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"5","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":820,"wires":[["64178689.4b6aa8"]]},{"id":"437121a3.66f67","type":"change","z":"c32149c9.68d7d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"6","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":860,"wires":[["64178689.4b6aa8"]]},{"id":"d92e654a.72c858","type":"change","z":"c32149c9.68d7d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"7","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":900,"wires":[["64178689.4b6aa8"]]},{"id":"7733d9c5.e03a98","type":"change","z":"c32149c9.68d7d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"8","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":940,"wires":[["64178689.4b6aa8"]]},{"id":"395878b9.294958","type":"change","z":"c32149c9.68d7d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"9","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":980,"wires":[["64178689.4b6aa8"]]},{"id":"f51d469e.624d08","type":"change","z":"c32149c9.68d7d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":1020,"wires":[["64178689.4b6aa8"]]},{"id":"6ea5a08b.b646f","type":"change","z":"c32149c9.68d7d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"*","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":1060,"wires":[["64178689.4b6aa8"]]},{"id":"ad92bb61.5018a8","type":"change","z":"c32149c9.68d7d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"#","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":1100,"wires":[["64178689.4b6aa8"]]},{"id":"ba832b56.779de8","type":"change","z":"c32149c9.68d7d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"A","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":1140,"wires":[["64178689.4b6aa8"]]},{"id":"72539223.ffb7dc","type":"change","z":"c32149c9.68d7d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"B","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":1180,"wires":[["64178689.4b6aa8"]]},{"id":"d8c2166c.c65018","type":"change","z":"c32149c9.68d7d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"C","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":1220,"wires":[["64178689.4b6aa8"]]},{"id":"3847b55f.55d59a","type":"change","z":"c32149c9.68d7d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"D","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":1260,"wires":[["64178689.4b6aa8"]]},{"id":"bf41963e.161038","type":"change","z":"c32149c9.68d7d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"incorrect","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":1300,"wires":[["64178689.4b6aa8"]]},{"id":"3ce27f8a.e574a","type":"change","z":"c32149c9.68d7d8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":660,"wires":[["64178689.4b6aa8"]]},{"id":"64178689.4b6aa8","type":"function","z":"c32149c9.68d7d8","name":"","func":"\nreturn msg;","outputs":1,"noerr":0,"x":750,"y":780,"wires":[["6f0a1438.ff182c"]]}]


However if I can understand what I made wrong on the function I wrote originally will be great!!!!!!!

Now the big question is.... how I can make a function that collect 4 messages in a row and compare it with the stored code and return if is right or wrong code....


Thanks in advance

fluri...@gmail.com

unread,
Aug 15, 2017, 5:46:53 AM8/15/17
to Node-RED
There are several "errors" in your function node:
Try this:

[{"id":"652ae9e5.d121b8","type":"function","z":"8c2006e5.bb1398","name":"","func":"var originalval = msg.payload;\n\n//if (960<originalval<970){\nif (originalval>960 && originalval<970){\n    msg.payload = \"1\";\n}\n\n//else  if (975<originalval<986){\nelse if (originalval>975 && originalval<986){\n    msg.payload = \"2\";\n}\n\n//else if (995<<originalval<1006){\nelse if (originalval>995 && originalval<1006){\n    msg.payload = \"3\";\n}\n\n//else if (875<originalval<885){\nelse if (originalval>875 && originalval<885){\n    msg.payload = \"4\";\n}\n\n/* etc. to modify !!\nelse if (890<originalval<901){\nmsg.payload = \"5\";\n}\n\nelse if (905<originalval<915){\nmsg.payload = \"6\";\n}\n\nelse if (805<originalval<815){\nmsg.payload = \"7\";\n}\n\nelse if (818<originalval<828){\nmsg.payload = \"8\";\n}\n\nelse if (832<originalval<842){\nmsg.payload = \"9\";\n}\n\nelse if (755<originalval<765){\nmsg.payload = \"0\";\n}\n\nelse if (745<originalval<755){\nmsg.payload = \"*\";\n}\n\nelse if (768<originalval<778){\nmsg.payload = \"#\";\n}\n\nelse if (1015<originalval<1025){\nmsg.payload = \"A\";\n}\n\nelse if (923<originalval<932){\nmsg.payload = \"B\";\n}\n\nelse if (846<originalval<856){\nmsg.payload = \"C\";\n}\n\nelse if (780<originalval<790){\nmsg.payload = \"D\";\n// return msg;\n}\n*/\n\nelse {\n  msg = null;\n}\n\nreturn msg;\n","outputs":1,"noerr":0,"x":290,"y":1560,"wires":[["cae0ecbe.7c95c"]]}]

David Caparrós

unread,
Aug 15, 2017, 6:16:10 AM8/15/17
to Node-RED

Thanks Fluri so I understand that need to make split separations and can not compare a value in booth directions at one.

I get my project now running well tacking the easy way....

4 x 4 keypad to enter a code to enable the alarm, each key pressed you get a blue led blink, if completed code is ok then return a green blink led and 20 seconds blue led blinking to allow to leave the apartment prior to the alarm system is definitely enabled.

When alarm is enabled fix red led is on.

IF door opened & PIR motion detected then Blue/red blinking for 30 seconds, time to allow to enter code for disabling the alarm otherwise siren is activated until alarm system is disabled + SMS + Emai notification send.

In parallel this can be enabled or disabled via node-red-dashboard switch that requires a password first to be able to change its status.

A log shown on dashboard saves when alarm is enabled or disabled, is alarm is triggered and if door is opened or closed.

I have only pending to make some safety to avoid the siren to be ringing full day (just in case......) and make definitive wiring and installation as all now is provisionally wired.

It's really amazing what can be achieved in node-red with my low knowledge......

If someone is thinking to make something similar and is interested I can share what I did, just let me know, don't expect amazing programming but all works perfect whatever conditions you try.

Best regards

fluri...@gmail.com

unread,
Aug 15, 2017, 7:02:21 AM8/15/17
to Node-RED
Glad it's working.
Generally I prefer to use the switch statement.

[{"id":"79127543.83b8fc","type":"function","z":"8c2006e5.bb1398","name":"","func":"var originalval = msg.payload;\n\nswitch (true) {\n    case (originalval>960 && originalval<970): msg.payload = \"1\"; break;\n    case (originalval>975 && originalval<986): msg.payload = \"2\"; break;\n    case (originalval>995 && originalval<1006): msg.payload = \"3\"; break;\n    case (originalval>875 && originalval<885): msg.payload = \"4\"; break;\n    // etc\n    default: msg = null;\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":290,"y":1640,"wires":[["cae0ecbe.7c95c"]]}]

Julian Knight

unread,
Aug 16, 2017, 3:31:07 AM8/16/17
to Node-RED
Please do share David. You can use the flows site to share stuff or put something up on GitHub & a link into flows. Just remember to make sure you haven't left any sensitive information in.

Hopefully you've also thought about the security of the web interface and websockets. Have a look at the WIKI for the cookbook for some thoughts on that.
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

Adam S

unread,
Aug 23, 2017, 8:18:12 AM8/23/17
to Node-RED
This is definitely a good case to use a node.  I have a node if you want to try it out.  It was the first node I ever made so may or may not still work.  It is attached if you would like to test it.  Follow instructions here to install https://nodered.org/docs/creating-nodes/first-node#testing-your-node-in-node-red If it works for you please let me know and I'll push it to NPM so it shows up in the manage palette to install

Adam S

unread,
Aug 23, 2017, 8:19:51 AM8/23/17
to Node-RED
Ok, google groups apparently doesn't like .zip files.  I'll upload it to GitHub and post back when I have a link.

Adam S

unread,
Aug 23, 2017, 9:33:37 AM8/23/17
to Node-RED

David Caparrós

unread,
Aug 23, 2017, 11:39:31 AM8/23/17
to Node-RED
Thanks for your help Adam,

At the end what I did to read the 4x4 keyboard matrix to reduce resources consumption is to add some resistors in the way that measuring the analog value I can determine wich key is pressed directly, this reduce from an original 8 digital channels required to handle to a single analog input channel.


I can show some more info if someone is interested.

Regards

El miércoles, 23 de agosto de 2017, 15:33:37 (UTC+2), Adam S escribió:

Adam S

unread,
Aug 23, 2017, 2:21:05 PM8/23/17
to Node-RED
Yes, I would like to see.  Also, I just now noticed you are using an Arduino so my node will not work anyway as it was made for a Pi.  If anyone else wants to try it and give some feedback I'll update it and put it on npm.

David Caparrós

unread,
Aug 23, 2017, 3:26:45 PM8/23/17
to Node-RED



Hi Adam

I'm using an Arduino mega connected trough USB because I had not enough input/output pins on the Raspberry pi, also no analog inputs, I'm using some ESP12-E modules in paralel but on where is located the main unit (raspberry) I was requiring more channels, also wanted to make some isolation between soft and hardware what drive to spend 10$ on an arduino Mega what gives you 54 I/O pins (15 of them PWM) + 15 Analog channels.

For the 4x4 Matrix keyboard I made something like this with resistors:


With this I can determine which of the keys is pressed just looking at the input voltage, in advance I take a reading value as target afterwards for each or the keys what I can work afterwards very simple on node red with a function as follows:



The link on the left is directly coming from the Analog input, I just limit the number of input messages and only ready if value is greater than 500 to assume some key was pressed at this stage.


If value is between X and XX I drive the message to a specific output on the switch and then I change the message to the proper key, for instance I know when pressing 1 Key give me a value of 928, I give 5 tolerance in booth directions so if value is between 923 and 933 then I direct this to first out and change payload to "1!.



Afterwards I use the join module function from palette to put all inputs in a single string and compare it to see if is correct or not:




Hope this helps somehow to someone else.


Regards


Reply all
Reply to author
Forward
0 new messages