Simulink + Arduino Uno: Cannot upload to Arduino when using tone() function
1 view (last 30 days)
Show older comments
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: http://www2.sorocaba.unesp.br/professor/marilza/SM-II/PDFs%20para%20Projeto%20SM-II/DriverGuide.pdf
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
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) {
#ifndef MATLAB_MEX_FILE
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);
#endif
xD[0]=1;
}
5) The loop code from Arduino has been inserted to the Outputs tab:
if (xD[0]==1) {
# ifndef MATLAB_MEX_FILE
tone(Pin[0], Freq[0], Duration[0]);
#endif
}
6) I've linked the arduino library at the Libraries tab:
# ifndef MATLAB_MEX_FILE
# 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? (<http://arduino.cc/en/Reference/map>) PS2: Sorry for the long post, but I hope I've included every potentially important information.
4 Comments
willy
on 6 Sep 2016
Hi peter.. Have you solved the error above?i have the same problem with 'shiftOut' function. Thanks willyang
Answers (1)
Sreeram
on 22 Jul 2014
Hi Peter,
Could you please post the model if possible that would help in debugging ?
Thanks, Sreeram Mohan
0 Comments
See Also
Categories
Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!