Using Simulink --> User Defined Matlab Function --> Arduino
2 次查看(过去 30 天)
显示 更早的评论
I'm trying to run what I thought was going to be a simple code, but for some reason the output is incorrect. I've used Simulink and Arduino Uno in other projects and it works great. This time I'm using an analog input voltage to determine which of my eight LEDs will light up. Like how a fuel gauge works, as you have more fluid, more LEDs light up. So in Simulink, I have an Analog Pin (0) reading voltage (fuel level). The line coming out of that block is then split into 8 different Matlab Function blocks. Inside each Matlab block is an if/then function, where if u > i, then y = 1, however, if u < i, then y = 0. The variable i is an integer that is different for each Matlab block. The output for each block is connected to its own Digital Output block.
When I send the code from Simulink to the Arduino, all of the LEDs turn on, regardless of how much fluid is in the tank. I can run a different code from Matlab to the Arduino, and the LEDs light up correctly as the fluid goes up and down, but I want to have a stand-alone device not connected to my computer, which is why I want to use Simulink. I tried to put this different code into a single Simulink Matlab Function block, but the code wouldn't load to the Arduino, so I separated the code into eight separate blocks. At least now the code is being sent to the Arduino and I'm getting some kind of response.
Any suggestions would be greatly appreciated! I didn't think I would have spent so much time on what I thought would take ten minutes to finish!
Thanks,
Chris
0 个评论
采纳的回答
Walter Roberson
2017-4-3
If your break values are unevenly spaced then I suggest you do a 1D Lookup Table to look up how many lights are to be lit.
If your break values are evenly spaced then I suggest you use an arithmetic block to multiply the input by some constant, and then possibly another to floor() the result, to look up how many lights are to be lit.
Once you have calculated how many lights are to be lit, one approach would be to have a switchport for each of the levels, with the result driving the appropriate pin level.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Arduino Hardware 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!