problem with dimmer / serial port

Hi I'm trying to use an Arduino as an interface to control lab equipment. As a fist task I am trying to implement an illumination controll wheren I control an LED with PWM. Essentially I want to send a value (0-255) to my arduino via the serial port and it sets the required light intensity until i change it. I fond the dimmer example and thought it would be perfect. I played a little making my LED dim via a loop and all was peach however if send a single byte to the arduino it switches on full power (checked with a scope) for about 1 sec and then goes off and goes off. However if I send two or more bytes of code it goes to the required level and stays there until I change it. For further testing I added a Serial.print() that replies the value received back the computer, and run a (third party) port monitor to record the communication between the board and the computer, and there is no reply to the first byte of data send but all the subsequent bytes are fine. Can anybody explain what goes wrong here or shall I just send my command often enough to be sure?

System Arduino duemillanova with 329 chip, software vers. 0016, Windows XP, I send my comands to the serial port using matlab

Arduino code:

const int ledPin = 9; // the pin that the LED is attached to byte brightness = 0; // set intial brightness to 0

void setup() { // initialize the serial communication: Serial.begin(9600); // initialize the ledPin as an output: pinMode(ledPin, OUTPUT); }

void loop() {

// check if data has been sent from the computer: if (Serial.available()) { // read the most recent byte (which will be from 0 to 255): brightness = Serial.read(); // set the brightness of the LED: analogWrite(ledPin, brightness); Serial.print(brightness, BYTE); } }

matlab code: sl = serial('COM13'); % Initialise port set(sl,'BaudRate',9600); %set speed sl.timeout = 0.5; %read timeout fopen(sl); % open port for bright = 110:113 % Loop fwrite(sl, bright) %wertie binary to port pause(1); % wait fro 1 sec out2 = fread(sl) %record reply and display end fclose(sl); % close port

Port monitor log (not that the first request it two bytes since there is no reply between them Request: 16/06/2009 15:48:55.76964 (+107.2656 seconds)

6E 6F no

Answer: 16/06/2009 15:48:56.31664 (+0.0313 seconds)

6F o

Request: 16/06/2009 15:48:58.80164 (+1.4844 seconds)

70 p

Answer: 16/06/2009 15:48:58.83264 (+0.0313 seconds)

70 p

Request: 16/06/2009 15:48:59.31664 (+1.4844 seconds)

71 q

Answer: 16/06/2009 15:48:59.34764 (+0.0313 seconds)

71 q

Two things come to mind: - How does MATLAB know that "bright" is a single byte and not a 32-bit integer? - Why does data sent look like '6F' and echoed back look like 'n'?

“How does MATLAB know that “bright” is a single byte and not a 32-bit integer?”

  • In matlab the fwrite command sends bytes on the serial port so its’ defined through that (fprint can send other formats), values greater than 255 come out as 255 by default

“Why does data sent look like ‘6F’ and echoed back look like ‘n’?”

  • My port monitor puts the hexadecimal value in the left column and the corresponding ASCII character into the right column, the the ‘n’ is not the echo but the ASCII

My best guess is MATLAB's serial driver is either dropping the first character or there is "gunk" on the line when the port is opened and the first character is corrupted, etc. The problem is only with the first character, right?

Try perhaps a delay between opening the port and sending the first character.

To determine if it is the arduino, or matlab, you could use the serial monitor in the arduino, or your terminal program to send some data, if it works then its matlab, if not, then its the arduino

Thank you all, I has a play with terminal etc. and it seems the arduino needs just over 1 sec to initialize after the port is opened during that time it looses the first few bytes... (no matter what software - tried VB.net, matlab, terminal) I originally even tried a delay but only of 0.5 sec. with 1 sec it sometimes looses a byte with a 2 sec delay it works fine.. still not sure why it would need that long but I'll just add a delay to my code...

it seems the arduino needs just over 1 sec to initialize after the port is opened

It needs that to look and see if it gets data on it's serial port to indicate that it is not to run it's program but to run it's bootloader. Without the delay you would not be able to do a manual reset to download code you would have to relay on an automatic reset and on some hardware systems this is not possible of desirable.