Hello Arduino community! I've just recently gotten into the world of Arduinos and am testing functions out, especially in regards to communication with MATLAB, since I expect to be doing that in the coming future. I've written code that worked pretty well for communication, but I was only sending simple 1 and 0. I'm now doing some projects with friends that involve sending more than just a simple number out to the Arduino: for instance, 015, 2500, etc. I've edited my code to display what I am sending from MATLAB to the Arduino as well as what Arduino has received (by sending what it got back to MATLAB), but it seems like it the data isn't sending correctly. At times I've seen there be a delay in the values I send, with 0's filling gaps between my sends, or the values themselves would be completely off.
In this project the first character sent (we can ignore letters here, just digits) is used to decide the course of action, and the numbers following the first will be the actual number to be interpreted. A delimiter of / will be used, but is not in the code at the moment. Because this is not feasible by utilizing (if Serial.available() > 0), I have considered using a while loop to take the data iteratively (as well as sending the data one at a time) and save each bit (int -> char) it as a string, then interpret the entire thing after available serial data is 0. At the moment that seems like the best plan, but I'm not sure if it the most efficient in terms of interpreting data from MATLAB to arduino and bypassing the one-bit-limit of the Serial.read() function. What are your takes on this?
In short I want to be able to read and interpret more data at a time from MATLAB sending to Arduino due to the one-bit limit, and am wondering whether an iterative approach using a while loop would accomplish this, or if a more efficient method is going under my radar. Thanks! Here's the basic code I'm using to test out this data sending problem. As an output I'm receiving a number that ranges in the 45-55 range.
if (Serial.available() > 0)
val = Serial.read();
a = serial('COM3', 'BaudRate', 9600);
while (0 == 0)
send = input('Send what? ', 's');
if (send == ' ')
%len = length(send);
%for k = 1: len
% fwrite(a, send(k));
Of course the Arduino and MATLAB code posted here is extremely simple, but if we can get the proof-of-concept idea out there shouldn't be any reason we're unable to implement that into a much larger program. :) Thanks for any help!
You would be well advised to look at the Reference section to read up on Serial. As well, have a look at Nick Gammon's excellent tutorial, http://gammon.com.au/serial (http://gammon.com.au/serial)
Can't help with the MatLab stuff, except to say that you don't need a delay in sending unless you find that the Serial buffer (64 bytes) in the Arduino starts overrunning. No idea why MatLab might be sending zeros when it isn't supposed to.