MATLAB Answers

Simulink + Arduino Uno: Cannot upload to Arduino when using tone() function

조회 수: 8(최근 30일)
Peter Benko
Peter Benko 14 Jan 2014
댓글: Euan French 18 Dec 2018
Hello! I'm trying to do the projects (Light Theremin)described in the Arduino Projects Book (came with the Starter Kit) with Simulink as well, but I ran into a problem. In one of the projects the task is to operate a piezo speaker according to the signal of a photoresistor. The buildup is fairly simple: The Arduino calibrates the photoresistor for 5 seconds (you can define how many light you want to control the speaker with) in the setup phase. In the loop it reads the photoresistor via A0 analog input, maps it from 0-1023 to 0-4000 and by using the tone function it controls a piezo speaker (Pin 9). I'm using an Arduino Uno and Simulink on a Windows 8.1 x64 PC.
As there is no equivalent of the tone function in Simulink I wanted to write an S-Function block with the S-Function Builder based on this description:
When I follow the instructions, everything works perfectly. The example in the guide uses only the pinMode and digitalWrite functions.
When I try to modify the working code by adding the tone function, although the S-Function builds normally, upon uploading to Arduino I get the following error:
The call to realtime_make_rtw_hook, during the after_make hook generated the following error:
The build failed with the following message:
C:\Users\[Username]\Documents\MATLAB\DriverTutorial_rtt/sfcn_exout_slsp_wrapper.c:64: undefined reference to `tone'
make: *** [MW/DriverTutorial.elf] Error 1
I transformed the working Arduino code from the book as follows: 1) I defined three inputs for the tone function:
  • Pin
  • Freq
  • Duration
(Reference: There was no output defined.
2) The initially defined variables became parameters: Arduino:
int sensorValue;
int sensorLow = 1023;
int sensorHigh = 0;
const int ledPin = 13;
Simulink S-Function Builder:
3) At the Initialization panel I set the Number of discrete states to 1, and the Sample mode to Inherited
4) The setup block from the Arduino code has been inserted to the Discrete Update tab as follows:
if (xD[0]!=1) {
pinMode(LEDpin[0], OUTPUT);
digitalWrite(LEDpin[0], HIGH);
while (millis() < 5000) {
sensorValue[0] = analogRead(A0);
if (sensorValue[0] > sensorHigh[0]) {
sensorHigh[0] = sensorValue[0];
if (sensorValue[0] < sensorLow[0]) {
sensorLow[0] = sensorValue[0];
digitalWrite(LEDpin[], LOW);
5) The loop code from Arduino has been inserted to the Outputs tab:
if (xD[0]==1) {
tone(Pin[0], Freq[0], Duration[0]);
6) I've linked the arduino library at the Libraries tab:
# include <Arduino.h>
# endif
7) The build process is successful, but upon upload I get the above mentioned error.
I do not understand the error message, as the original Arduino code requires no additional libraries to use the tone function.
What have I done wrong? Please help me.
PS.: Is there an equivalent for the map() function? (<>) PS2: Sorry for the long post, but I hope I've included every potentially important information.

  댓글 수: 4

표시 이전 댓글 수: 1
Aldiaz Nabil Faekar
Aldiaz Nabil Faekar 16 Nov 2014
Hi peter.. Have you solved the error above?i have the same problem with map() function. Thanks Aldiaz
willy 6 Sep 2016
Hi peter.. Have you solved the error above?i have the same problem with 'shiftOut' function. Thanks willyang
Euan French
Euan French 18 Dec 2018
Bumping, exact same issue in 2018, no fix!

Sign in to comment.


Sreeram 22 Jul 2014
Hi Peter,
Could you please post the model if possible that would help in debugging ?
Thanks, Sreeram Mohan

  댓글 수: 0

Sign in to comment.

Translated by