Code generation for Arduino (reading analogue input signal)¶
In this example we will generate code for the Arduino that will take an analogue input measurement and use that input to control the brightness of the "on-board" LED.
Introduction¶
For this demonstration, besides the Arduino board we will need a variable resistor (potentiometer). We will use the Troyka module from the manufacturer Amperka. This potentiometer has a resistance of 10 kOhm and allows us to change the voltage on the signal output from about 0 volts to the Arduino supply voltage (usually 5 volts).
Generally, any analogue potentiometer has three output pins, which we will connect to the Arduino board as follows:
- power pins V (voltage) and G (ground) to pins 5V and GND of the Arduino board
- pin S (signal) - to pin A0 (zero analogue input)
Engee model description¶
Below is a general view of the model analog.engee
, from which we will generate code. It consists of one subsystem.
In one calculation cycle, the model takes input data through the in_ANALOG_PORT
port and calculates two output variables: out_LED_BUILTIN
and param_WAIT_MS
.
The input variable has a range of values from 0 to 1023 (the data comes from a 10-bit ADC), so first we scale it to a range of 0 to 1. The type conversion is done automatically. Component Multiport Switch
alternately returns to us the duration of switching on and duration of switching off the LED. After it is the block Gain
, where the delay is multiplied by 30, and the block Saturation
, which prevents the transmission of negative values to the output param_WAIT_MS
.
.
Interface Model-Arduino¶
The platform-dependent part of this code, which is stored in the file sketch_analog_custom/sketch_analog_custom.ino
, implements the following interface:
- reads the voltage level at the
A0
input of the Arduino platform and passes this 10-bit value to the model input, - receives from the model the state of the LED (on or off) and switches on or off the digital output connected to the diode,
- accepts from the model the delay time in milliseconds that must elapse from the end of the execution of our model code to the next execution.
We could have generated the PWM in other ways:
- by calling the Arduino's built-in function
analogWrite
, - or by comparing the desired value with a sawtooth signal.
Code generation¶
The following cell allows you to make a code from the model analog.engee
that will be placed in the folder sketch_analog_custom/analog_code
.
engee.generate_code( "$(@__DIR__)/analog.engee",
"$(@__DIR__)/sketch_analog_custom/analog_code" )
The output files *.c
and *.h
are already placed in the required subfolder of the project. We connect the file analog.c
to the platform-dependent part of the code using the command include
.
Data transfer to and from the generated code is performed using the structures analog_U
and analog_Y
. The fields of these structures have the same names as the ports in the Engee diagram.
To get a more concise code, try to disable automatic commenting in the model settings (Code Generation tab).
Transferring the model to Arduino¶
To transfer the project to Arduino you need to follow the steps below:
- Download the catalogue
sketch_analog_custom
from a file browser; - Unzip the archive on your local computer;
- Open the file
sketch_analog_custom.ino
and in Arduino IDE click on the buttonUpload
.
As a result, we have the ability to adjust the frequency of the PWM signal to the LED, and therefore its brightness, using a potentiometer. In the same way it is possible to control servos, pumps and various amplifiers (careful with current flow limitations, it is better to connect three or more servos via an additional power supply, bypassing the Arduino voltage stabilisation).
Conclusion¶
We have seen that in Engee it is possible to create full-fledged algorithms for embedded systems - to receive data, to process it and to control electronics connected to the hardware platform with the help of output values.
Code generation is a critical tool for implementing a model-driven approach to development, where a piece of embedded system code can be developed as a model, tested in a model environment and run through semi-natural testing.