How to simulate Arduino setup () {} and loop () {}

#1

Hi, thanks for kind help,
I'm not hiding I'm new to Javascript, Nore Red, and I would prefer do everything I need much faster using Arduino code, but I need to adapt to progress and it's time to learn new language/systems.
One of the biggest question I asked myself is, how can I execute code once and before the main code begins? How can I initialize variables, and especially hardware before Node Red start using it on flows? I have a lot on I2C bus, for some there aren't libraries and I have to execute commands on I2C before all,

I'm thinking to create a global.set and don't execute anything until it will be set after initiate hardware, any other idea?
Thank you!

#2

This may be a relevant thread - Node registration order
and this may also help https://flows.nodered.org/node/node-red-contrib-config

1 Like
#3

The short answer is that you make use of the settings.js file. You can put code in there or perhaps in a separate Node.JS "module" (so you might want to look up how to do that, it isn't hard at all) file to keep things tidy.

There is a global variable property in there that lets you pre-define variables. Just be aware than settings.js is only executed when you start Node-RED so if you change anything there, you have to restart Node-RED.

The links that Dave has given give the details.

1 Like
#4

perfect, works! I used [node-red-contrib-config] and I set the desidered variable to 0. Then I made a function driven by an Inject every 0,1 seconds, which send multiple data to hardware (every 0,1 seconds). When the data is sent this variable has already an amout of value, using the global I check it in other flows thre the hardware is used, and ony if that valuse is reached I'll use the hardware