Hello, I am trying to make an ultrasonic sensor with arduino and an ultrasonic transducer using the following arduino code that outputs the number “3” to the serial port:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
Serial.println(3);
delay(1); // delay in between reads for stability
}
long microsecondsToInches(long microseconds)
{
// According to Parallax’s datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/ac … G-v1.3.pdf
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
The output on the serial monitor should repeatedly output the distance, like:
21 in
I would then like to import that information to matlab so i can create a graph. My matlab code looks like:
s = serial(‘COM1’);
set(s,‘BaudRate’,4800);
fopen(s);
fprintf(s,‘*IDN?’)
out = fscanf(s);
fclose(s)
delete(s)
clear s
…in hopes that the serial port would take the number found in the serial monitor of the arduino would be the input of the serial port for the matlab code. However, I get weird numbers as an output of the matlab code, like 10, 13, 53.
Can someone help me fix my code to make the matlab output “3”?