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

2 Ansichten (letzte 30 Tage)
Chris
Chris am 3 Apr. 2017
Kommentiert: Chris am 4 Apr. 2017
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

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 3 Apr. 2017
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 Kommentare
Chris
Chris am 4 Apr. 2017
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 am 4 Apr. 2017
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 -

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Arduino Hardware finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by