i'm trying to just send a number from Matlab to my arduino. It does connect and communicate, however, the returned number from the arduino is not what matlab sent. Here is my Matlab Code:
arduino = serialport('COM7',9600,"Timeout",15);
pause(1)
% Send a number to Arduino
flush(arduino);
write(arduino,6, 'string');
pause(1);
% Read the number sent back by Arduino
receivedNumber = read(arduino, 1,'string');
disp(receivedNumber);
Arduino Code:
int duty_cycle;
void setup() {
Serial.begin(9600);
}
void loop() {
while(Serial.available()>0){
duty_cycle = Serial.read();
Serial.println(data_num);
}
}
When I do disp(receivedNumber), it keeps showing the number 0. If I change the data type in my matlab code to 'int8' it'll only return the number 48. Not sure why this is. I already read through the Serial Input Basics forum.