How do I get a real-live distance sensor in Simulink
조회 수: 2 (최근 30일)
이전 댓글 표시
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/167266/image.jpeg)
I would like to have some kind of analog signal.
could please somebody help me.
댓글 수: 0
답변 (1개)
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
댓글 수: 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!