I'm trying to implement a communication from MATLAB to an Arduino Uno using Serial port.
I tried the simplest case I could imagine to test this communication but it still doesn't work.
My arduino code is the following :
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
myservo.attach(3);
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()>0)
{
myservo.write(80);
}
}
Basically, when Serial recieves anything, it moves the servo to a random position. I tried it with directly sending something in the serial monitor and it works fine.
However,
When I try to send anything with MATLAB using the simple following code :
Maybe I expressed myself poorly. From my understanding the code I wrote constantly listens to the port and when it detects any data published to the port, the servo moves to a specific position. Is it correct ?
It uses the COM16, as specified in the MATLAB code.
Another thing to bear in mind when using PC software to communicate with your Arduino is that sometimes before you start you should peform a reset on the Arduino by setting RTS or DTR high like this
setRTS(s,true)
setDTR(s,true)
do this after creating your serial connection and before you try to communicate.