A few example sketches are included in the examples directory.
These illustrate how this library can be used.
As this library does not contain any support for hardware CAN tranceivers, all of these examples use the simpler serial CAN transport.
This setup is useful for testing as it does not require any CAN bus equipment such as CANUSB or CANETHER. Instead, simply use a USB cable connected between a computer and the Arduino (same cable as is used for programming the Arduino). You can then communicate with the Arduino through the same serial port as you use for programming the Arduino.
You can then start FCU (or any other configuration utility) and connect through this serial port. The FCU will then communicate with your Arduino module as a CBUS module.
Libraries that implement support for hardware CAN tranceivers contain similar example sketches as these but using those CAN tranceivers instead.
All the example sketches have a structure that defines all the necessary elements in order.
A bare minimum sketch that only contains the necessary services
(MinimumNodeServiceWithDiagnostics and CANServiceWithDiagnostics).
It can be used to demonstrate that the Arduino module can communicate with CAN bus
and can communicate with FCU.
The FCU can assign a node number and query the module parameters.
The services used in this sketch have enabled diagnostics so that all features of VLCB can be demonstrated.
Note that there are no support for node variables or events.
A small module that uses one input for producing events and one output that is controlled by consumed events. For this it uses NodeVariableService and four services for event support.
The sketch defines one moduleSwitch object for an input pin
and one moduleLED object for an output pin.
To manage these two I/O pins there are two functions:
There are two event variables:
| EV# | Function |
|---|---|
| 1 | Input channel number. Set to 1 to send this event when the moduleSwitch is changed |
| 2 | Output function. Set to 1 to turn on the moduleLED. Set to 2 to make the moduleLED blink. |
The sketch supports self-consumed events. Create an event with EV#1 set to 1 to react to the moduleSwitch. Set EV#2 to make moduleLED change. Now the LED will react when the switch is changed.
If the moduleSwitch is changed but there is no stored event with EV#1 == 1 then no event is produced.
This sketch uses services with diagnostics enabled to demonstrate them.
This is a larger sketch that handles 4 input pins and 4 output pins. See full documentation for this sketch in its own README.
This sketch does not enable diagnostics to save memory on small processors and also to demonstrate that diagnostics is optional.
This sketch has a similar functionality to VLCB_4in4out above but
is implemented using “event slots” instead of relying on specific event variables.
Events in slots 1-4 are produced when switch 1-4 respectively is activated. If there is no event in the slot for an activated switch then a default event is created.
Consumed events in slots 10-19 activate LED 1, events in slots 20-29 activate LED 2, etc.
This example sketch has the same functionality as VLCB_1in1out above but uses a serial connection instead of CAN bus. This setup is useful for testing as it does not require any CAN bus equipment such as CANUSB or CANETHER. Instead, simply use a USB cable connected between a computer and the Arduino (same cable as is used for programming the Arduino). You can then communicate with the Arduino through the same serial port as you use for programming the Arduino.
You can then start FCU (or any other configuration utility) and connect through this serial port. The FCU will then communicate with your Arduino module as a CBUS module.
This example demonstrates split serial usage on a single node. The hardware serial port is dedicated to SerialGC for VLCB/GridConnect traffic, while a separate SoftwareSerial port is used for SerialUserInterface commands and status output.
This layout is useful when you want to keep the transport channel and the user console separate. It also shows the intended use of the Stream-based serial refactor for either HardwareSerial or SoftwareSerial.