**4.2 Arduino code**

*LabVIEW - A Flexible Environment for Modeling and Daily Laboratory Use*

spond 0 (zero) Volt and at value 1023 will correspond 5 Volt.

combination of the 10 bits, so there will be 2n

D0\_ON\n, D0\_OFF\n, D1\_ON\n, D1\_OFF\n.

high pulse and the entire period (duty cycle).

this occurs the Master message will be read (**Figure 12**).

been considered.

port. The message will contain as many strings as there are lines configured. In the syntax #Ai (i = 0…5) the value of Ai corresponds to the decimal decoding of the

WE suggest to the reader to test own system velocity before to set 500 μsec of sample rate. Usually, for my experience, it is very rare to follow with a LabView (not real time) Loop code that velocity. In case the system is not fast enough, one way of not losing data could be the following: change the Arduino's code to collect the msg (measured value) in a vector of 100 elements and send it to LabView each 50 msec. You can choose different size of vector but you have avoid to saturate the Arduino memory. The op-code (operation code) we have written does not belong to any standard communication protocol. We have invented a sequence of simple ASCIII strings to be sent over serial. So the Master will have at his disposal a set of instructions, which can be extended by the reader, to change the status of a digital output:

In order to avoid a slowdown loop() for sensors reading, due at continuous polling on the receipt of messages from the Master, an event-driven solution has

The reception on the serial line of a request from the Master is triggered by the event generated by the chip that manages the USB communication. When a byte arrives on RX an event is generated and triggered by a software procedure. When

In the end we can send a message to set a Analog output by pin3 in PWM mode. The **Pulse Width Modulation** [4], or PWM, is a powerful technique to control

analogic circuits (applied to a load) using a digital signal. It is a type of digital modulation, in particular we speech of pulse width modulation which allows to obtain a variable average voltage depending on the ratio between the duration of the

In electronics it is used to change the voltage, and therefore the power, on a generic load. For example, to change the speed of a direct current electric motor, to vary the brightness of light bulbs, especially LEDs. A useful duty cycle of 0% indicates a pulse of zero duration, in practice no signal (Vout = 0 volts), while a value of 100% indicates that the pulse ends when the next one begins (Vout = Vcc). To use

combinations. At value 0 will corre-

**90**

**Figure 13.** *PWM example.*

In the following boxes (**Figure 15**) we'll show some code that you can use to create a communication Master–Slave from LabView and Arduino [5].

In **Figure 14** it is possible to understand the functionally of declarations reading the comments.

In **Figure 15** is possible to verify the **setup()** code, inside it there are the comments to understand it. The code in **Figure 15** has been conceived to be very static and the expansion is very simple for the novice programmer. The serial speed has been set at 2 \* 106 bit/s in order to have the maximum communication speed between Arduino and the Master.

The code written in the "WHILE LOOP" could be redesigned to treat the Arduino channels dynamically. We want to say that configuration strings (like **pinMode(4, OUTPUT)**) can be sent directly from the MASTER unit.


#### **Figure 15.** *Code-slave mode setup().*

**93**

**Figure 17.** *Loop() code.*

*LabView and Connections with Third-Party Hardware DOI: http://dx.doi.org/10.5772/intechopen.96056*

**Figure 16.** *Example code for dynamic configuration.*

*LabView and Connections with Third-Party Hardware DOI: http://dx.doi.org/10.5772/intechopen.96056*

*LabVIEW - A Flexible Environment for Modeling and Daily Laboratory Use*

**92**

**Figure 16.**

**Figure 15.**

*Code-slave mode setup().*

*Example code for dynamic configuration.*

**Figure 17.** *Loop() code.*

**Figure 16** shows a small piece of code that is a good starting point for completing the dynamic channel configuration.

At this point we show the code about the blinking procedure and the Serial events procedure, respectively both in **Figure 12**.

In the end we report the loop() code, **Figure 17**. The code is very simple, the final message is made-up by concatenating the message in each "if" statement.
