필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
Duco Coops
Duco Coops 2017년 12월 5일
답변: Madhu Govindarajan 2017년 12월 5일
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.

답변 (1개)

Madhu Govindarajan
Madhu Govindarajan 2017년 12월 5일
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

카테고리

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