NodeRedSimpleSerial

Arduino Sketches

Analog Read Serial 01

sketch file

Initial test sketch, based on Analog Read Serial Tutorial example. Adjusted to read data from 2 analog pins, and to handle single character commands received from the serial line.

Raw measurement values and calculated voltages are sent as human readable csv to the serial port. Here, human readable means a space after each comma.

A received “C” character turns the LED on pin 3 on, “c” turns it off. CR and LF characters are ignored. Any other character turns the built in LED on. Receiving a valid command character turns the built in LED off again.

This provides enough of an interface to create a Node-RED flow that receives and displays data from the Arduino, and to send commands that control what the Arduino is doing.

serial flow

Analog Read Serial 02

sketch file

An adjusted sketch that more aggressively follows some coding conventions. Constant values are stored in const variables, with all upper case names. Literal values are moved to named constants. Several signed variables have been changed to unsigned. The common measurement code has been refactored into a function. A global variable has been moved local to the single function that needs to reference it. The command processing code has been moved to a separate function. Constant values and string literals have been moved to program memory. Changed the csv output to remove the space after each comma, which allows the csv parser node to process it more cleanly.

Analog Read Serial 03

sketch file

Prepare to restructure using c++ classes for handling the separate functions of the sketch. Or at least simulate the way class methods are called and work with encapsulated data.

Make the final non-const global variable local and static in the only function that references it. Refactor the measurement functionality into a function that manages the interval internally. The loop function no longer needs any conditional logic. Instead it passes the current micros() time stamp to each called function, when lets them use elapsed time to control their internal state. For functions that do not actually use the time marker in their logic, add an unused attribute tag, to prevent the compiler from complaining about unused arguments.

Analog Read Serial 04

sketch file

Refactor and expand the command code repertoire processed, to simulated a larger event loop handling independent, lossly coupled, functionality. Split the command reading from the command processing, and turn the processing of each command into a separate function. Turn the main loop function calls into an iterated array of function pointers, to simulated an environment where methods could be dynamically added to and removed from the main event processing loop.

The (from serial port) command reading method also handles error indication. It does not know anything about what commands are actually valid, but it detects commands that were not processed by any of the existing functions. Every iteration, every method gets a chance to see a received command, and respond to it. Even if processing the command will delayed until later, an interested party must save the command internally, and set a flag to show they have accepted the command.

With this structure, potentially multiple methods can initiate processing based on the same received command. It has not been implemented here, but something like a global (multiple methods) reset is possible.