How do I get a real-live distance sensor in Simulink

1 view (last 30 days)
I have distance sensor that works in my arduino code. Now I want to read this distance in Simulink. Eventually I want that my ship reacts on the changing distance. I worked with the blocks 'serial receive' in Simulink. However, than the plot of run doesn't look at all at nice distance plot. this is my arduino code. This works.
#define trigPin 10 #define echoPin 11 void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); }
void loop() { // put your main code here, to run repeatedly: long duration, distance; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1;
if(distance >= 400 distance <= 2){ Serial.println("Out of Range");
} else { Serial.print(distance); Serial.println("cm"); }
delay(500); }
The picture is the plot that I get when I run the serial receive block.
I would like to have some kind of analog signal.
could please somebody help me.

Answers (1)

Madhu Govindarajan
Madhu Govindarajan on 5 Dec 2017
If you are using HC-SR04 ultrasonic sensor, here is a post that has a block that provides you distance values. https://www.mathworks.com/matlabcentral/fileexchange/51956-auto-flush?s_tid=prof_contriblnk
If not, you can see how they implemented this block here and use that to create a similar block for your sensor.
Madhu

Categories

Find more on Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!