Re: [node-red] FLIP-FLOP

1,906 views
Skip to first unread message

Colin Law

unread,
May 1, 2017, 3:01:43 AM5/1/17
to node...@googlegroups.com
You could write a simple function node that uses a node context
variable to remember the current state and flip it each time a message
is received.

Colin

On 1 May 2017 at 01:39, David Caparrós <davi...@gmail.com> wrote:
> can someone help me? I need a flip-flop funcion to use an input to tiger 0
> or 1 each time the input is received, a simple flip flop function that i'm
> so estupid that i'm not able to find.
>
> Thanks in advance
>
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/4a14d1af-1bed-4652-86ac-645430320ed8%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

steve rickus

unread,
May 1, 2017, 9:41:52 AM5/1/17
to Node-RED
Sounds like a job for 'modulo' -- in javascript the percent sign is used to return the remainder of dividing a number by another number:

var odd = 5 % 2; // returns 1
var even = 6 % 2; // returns 0
var flipflop = counter % 2; // assuming you are keeping a count of msg received

There are many ways to keep track of the number of messages received -- search the flows library for the word "count", for example. Many people write their own little function node that gets the counter from the node's context (or uses 0 if not defined), then increment the value and save it back to context:

var count = context.get("counter") || 0;
count
++;
context
.set("counter", count);
msg
.payload = count % 2;
return msg;

I usually keep a browser open to the Mozilla Developer Network and the W3 Schools sites as my javascript reference...

Colin Law

unread,
May 1, 2017, 10:39:02 AM5/1/17
to node...@googlegroups.com
I would prefer to do

var count = context.get("counter") || 0;
count = (count+1) % 2;
context.set("counter", count);
msg.payload = count;
return msg;

That stops it failing when the value of count gets too large after a
few billion years.

Colin
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/f91178bc-fce6-437d-9a54-185b7089003f%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages