# Math formula in Function node for Darkroom Timer Program

Hello,

I am a newbie to the forums and Node Red too.

I am using Node Red to control my darkroom equipment using 2 remote control sockets. The user inputs a time that triggers a counter, turns the light on and then when the counter ends switches the light off.

I would like to add to the basic functionality by displaying f/stop calculations on the dashboard for information. I need to apply the formula T x 2^+f to the message payload. The value T is the message payload input and f is the stop increase or decrease which will be specified in each function node. An increase of half a stop of light would be T x 2^+0.5

Can anyone assist in helping me for apply the formula in a way that the function node can understand correctly?

Many thanks!

John.

Hi and welcome to the forum.

You may need to clarify the formula as given.

I can't parse that as valid sorry. `2^+f`? Is that `2^2+f`? (Two squared + f)

Ok, something else. (Not a problem though)

I have written an example flow that shows you how to do this, but you need to understand that the `T` and `f` must be sent at the same time - or: in the same message.

So ok: in my example `T` is defined in the `inject` node. But at some stage you are going to have to set things up.

If you are wanting `T` to be the `msg.payload` it isn't that hard to re-write the code in the `function` node.

``````[{"id":"c9949860.264198","type":"function","z":"703c61ac.837e1","name":"","func":"//  T x 2^+f\n\nvar T = parseInt(msg.t);\nvar f = parseInt(msg.f);\n\nvar answer = T * 2^2 + f;\n\nmsg.payload = answer;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1910,"y":540,"wires":[["9626001.13ad18"]]},{"id":"2f36bac6.3f5cf6","type":"inject","z":"703c61ac.837e1","name":"Set T and f","props":[{"p":"T","v":"3","vt":"num"},{"p":"f","v":"5","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1740,"y":540,"wires":[["c9949860.264198"]]},{"id":"9626001.13ad18","type":"debug","z":"703c61ac.837e1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2080,"y":540,"wires":[]}]
``````

Does that help?

`^` is the bitwise XOR operator. The operator to raise to the power of is `**`

1 Like

That will be something like
`msg.payload = msg.payload * (2**f)`
I am never sure of the precedence of ** so I tend to add extra parentheses just in case.

1 Like

Excellent, thank you for the help. I've got it working as I wanted!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.