Using Simulink --> User Defined Matlab Function --> Arduino

조회 수: 17 (최근 30일)
Chris
Chris 2017년 4월 3일
댓글: Chris 2017년 4월 4일
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

채택된 답변

Walter Roberson
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.
  댓글 수: 2
Chris
Chris 2017년 4월 4일
By break values do you mean breakpoints? I've never used Lookup Tables, switchports, or floor() before, this might be my big opportunity. Thank you for your advice -
Chris
Chris 2017년 4월 4일
Ah, I think I understand your answer. No, the break values are not evenly spaced out. So I'll try the Lookup Table and see how that goes. Thanks again -

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Arduino Hardware에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by