NodeSwitch™ part 5: code structure - control drapery motors, LED lights and relays with the same remote

NodeSwitch™ part 5: code structure - control drapery motors, LED lights and relays with the same remote

NodeSwitch™ LED and DC Motor Controller: Background

Please Note: This page is here for legacy purposes only. This equipment has been replaced by our new educational ESP32 Micropython kit, with a programmable low voltage relay module that has almost unlimited capabilities.

The NodeSwitch™ RN-12v-10w-0 unit is an Arduino compatible RF remote control LED companion controller for our radio equipped drapery, shade and blind motors. The optional MiniMight™ reversing motor controller can be used to operate other devices that have a reversible 12v DC motor, such as our 12v window and skylight openers.

As previously discussed (see links at bottom), the NodeSwitch™ controller can be re-programmed to behave in different ways, according to your needs. Reprogramming the microprocessor with the Arduino editor program is a relatively simple procedure that essentially sends a text file through the USB port of your PC to the Arduino microrocessor onboard the NodeSwitch™ unit.

In Part 4 we covered the uploading of the Blink example program that comes with the Arduino development environment (the editor) to the NodeSwitch™ unit via the USB port of your PC. In this article, we will examine the Arduino code structure to see how it works, and modify the program to make the blink rate of the test LED on the Nano microprocessor board change from 1 second intervals to half second intervals.

The code itself is a simplified version of the C/C++ programming language.

The NodeSwitch™ controller (with on-board Arduino) is available in our online store

Step 1: Open the Arduino IDE Program Editor and Load the 'Blink' Program

Here is a screen capture of the Arduino editor showing where to make the correct selection:

loading the Arduino blink example program for NodeSwitch™ programming

Once it is loaded into the Arduino IDE editor, the Blink program should look like this in the code display area:

load the Arduino blink example program as a NodeSwitch™ test

Step 2: Understanding Comments and Readability of Arduino Code

The description of the Blink program at the top of the above screen capture is called a 'block comment' and it is preceded by the 'star slash' ( /* ) characters and ends with the same characters reversed ( */ ). Any text within the block comment is ignored by the compiler when the program is prepared for the upload to the Arduino microprocessor (all comments are discarded).

understanding comments and readability in the Arduino Blink example program The purpose of this mechanism is simply to provide a way to describe the purpose of the various code segments that make up the overall program. It is very important to be as verbose as possible in order to write 'maintainable' code that you can quickly understand when you come back and look at it 6 months later (or someone else is trying to make sense of it).

Line comments are handled in a similar fashion, except they are preceded with a double slash ( // ), with no trailing characters. Also, white space with extra lines are ignored by the compiler; these elements are used simply to make the code more readable.

In the screen capture on the left, I removed the comments and most of the extra line spacing. These changes will not affect the program execution, it would work just the same as the previous screen with all the comments and formatting in place.

The code in the condensed version could actually be reduced further by eliminating the line feeds. As long as each statement is followed by a semi-colon or curly brackets, the compiler will still operate and the program will work just the same, even though it may become somewhat unreadable by the person viewing the code.

It is always a good idea to comment and format your code as much as possible for maximum readability; it does not make the program operate any slower. Note that comments and formatting are handled in exactly the same way in PHP, Javascript and some other programming languages - it is a very common convention these days.

Step 3: Identifying the Key Sections and Structure of the Arduino Code

structure of the Arduino blink example program Some new comments have been added to the bare code in the picture at left, indicating the 4 principal sections that make up the Arduino code structure. When power is applied and the Arduino boots up, the code is executed in sequence until it hits the continuous loop area, at which point it runs forever, unless there is a forced exit to terminate the routine (or it loses power).


The first section (INITIALIZATION) is where you can declare some variables that are to be used by the program at a later point. In this case, we are creating a new integer variable 'led' and assigning it to the test LED on pin 13. This gives a little more meaning to the code to help make it more maintainable.


The program then advances to the next section (SETUP) which is the first place you can actually execute commands or instructions. The commands in this section are only executed once during startup.

In this case, we are telling the microprocessor that the LED pin 13 is going to be used as an OUTPUT. This means it will output 5 volts to provide power to something when it is turned on (the LED in this case).

If it had been configured as an INPUT, the microprocessor would be expecting to see a voltage appear on that pin as an input signal that could be used to trigger something else. The input signal would be used as a digital 'boolean' variable value (i.e. on or off) because pin 13 is a digital GPIO port (General Purpose Input/Output). There are also ports on the Arduino that can be used as varying voltage analog inputs for measuring sensor outputs, such as a temperature sensor.


After it executes the SETUP commands, program execution moves to the LOOP area, where most of the work is usually done. As per the name, it just keeps executing this code (as long as power is applied) in a continuous loop until it is interrupted by a program instruction (usually the result of a variable that tests an INPUT for a trigger signal voltage, or perhaps a loop counter variable that has reached a certain value).


The functions area is used to create re-usable code that can be called many times during program execution. If there is something you want to do repeatedly, it can save a lot of extra code writing, as it is a 'write once, call often' structure.

The Blink program is very simple and does not use any functions, but you will see some function examples in the NodeSwitch™ default factory code. Here is an example of the impulse toggle code for our RSS (Really Smart Switch) version of the NodeSwitch controller; it uses a function:

RSS 12v impulse toggle program (opens in new tab)

Functions also have the ability to have variable parameters passed to them. In the above example, we use that capability to flash the status LED at different rates (pause) and a different number of flashes (blinks) to give more meaning to the status indication.

Step 4: Modifying the Arduino LED Blink Rate

Here is an image showing just the LOOP portion of the Arduino code used by the Blink program, with the original comments:

Arduino code modification of LED blink rate

As you can see, it is quite self-explanatory, just a continuous loop turning the LED on and off at 1 second intervals. The ON time is set by the second line in the loop - delay(1000), the same as the OFF time in the fourth line of the loop. The Arduino system measures time in milliseconds (1,000 milliseconds = 1 second), so half a second would be 500 milliseconds.

At this point, you can change the delay values, then press the upload icon to send the modified program to the Arduino and watch how the behavior changes. You do not have to save the modified program, but if you do, give it a different name so that you don't over-write the original Blink example code.

You can experiment with all kinds of different values for both ON and OFF delay values - go ahead, have some fun!

In the next article, we will cover a more complex example, using the restored factory default program.

The NodeSwitch™ controller is available in our online store

NodeSwitch™ Instructions Index:

Part 1: LED/Motor Controller: Introduction

Part 2: LED/Motor/Relay Controller: Basic Operation

Part 3: LED/Motor/Relay Controller: Advanced Operation: Arduino IDE Editor

Part 4: LED/Motor/Relay Controller: Advanced Operation: Arduino Programming System

Part 5: LED/Motor/Relay Controller: Advanced Operation: Program Code Structure (you are here)

Part 6: LED/Motor/Relay Controller: Advanced Operation: Jumper Locations

Part 7: LED/Motor/Relay Controller: Advanced Operation: Control Program Flow

Additional Resources:

Window Opener & Skylight Opener: Operating and Connection Info for our Chain Actuators

We sincerely hope you enjoy using these advanced motors; if you have any questions, please feel free to contact us at any time!