Arduino does not recognize new line from Matlab fprintf

Hi,

WHAT IT SHOULD DO: I want to update 3 parameters via serial port using MATLAB and then turn on and off digital ports accordingly when Arduino encounters the new line command \n.

PROBLEM: Arduino receives the string but does not do what's in the if statement. I think it does not recodgnize the new line (\n) command? (update: the code works when I use the Arduino Serial Monitor and set it to 'Newline'.)

Arduino loop:

int valve = 13;
int ISI = 3 *1000;
int DUR = 2  *1000;
int IRLEDPIN = 0;


void setup()
{
  Serial.begin(9600);
  
  pinMode(1, OUTPUT);      // sets the digital pin as output
  pinMode(2, OUTPUT);      // sets the digital pin as output
  pinMode(3, OUTPUT);      // sets the digital pin as output
  pinMode(4, OUTPUT);      // sets the digital pin as output
  pinMode(5, OUTPUT);      // sets the digital pin as output
  pinMode(6, OUTPUT);      // sets the digital pin as output
  pinMode(7, OUTPUT);      // sets the digital pin as output
  pinMode(8, OUTPUT);      // sets the digital pin as output

  pinMode(0, OUTPUT);
  pinMode(13, OUTPUT);  
}

void loop()
{
    while (Serial.available() > 0) {
      // read the incoming byte:
      valve = Serial.parseInt();
      ISI = Serial.parseInt();
      DUR = Serial.parseInt();

      // say what you got:
      Serial.print("Valve: ");
      Serial.println(valve, DEC);
      Serial.print("ISI: ");
      Serial.println(ISI, DEC);
      Serial.print("DUR: ");
      Serial.println(DUR, DEC);
      
      if (Serial.read() == '\n') {
        delay(ISI);
        digitalWrite(IRLEDPIN, HIGH);   // sets the LED on    
        digitalWrite(valve, HIGH);   // sets the LED on
        delay(DUR);                  // waits for a second
        digitalWrite(valve, LOW);    // sets the LED off
        digitalWrite(IRLEDPIN, LOW);   // sets the LED on        
        }
    }
}

MATLAB code:

s1 = serial('COM4');fopen(s1);

fprintf(s1,'6 1000 1000\n')

disp([char(fscanf(s1)) char(fscanf(s1)) char(fscanf(s1))]);

fclose(s1)

Anybody knows why this problem occurs?

thanks,
Deneck

UPDATE: changed 'loop' to 'statement'.

  pinMode(1, OUTPUT);      // sets the digital pin as output
  pinMode(0, OUTPUT);

Pins 0 and 1 are used by used by hardware Serial so I forsee a problem with using them as outputs

That is not how you use ParseInt.

If you are using it then you can not use Serial.read in the way you do.

does not enter the "if" loop.

What is an if loop?
There is an if statement and code following it but there is no loop in that code.

@UKHeliBob
Thanks for the warning, I will change the ports I use.

@Grumpy_Mike
My code is exactly like this code in regard of how I use parseInt:

Also, the code works when I use the Arduino Serial Monitor and put it to 'Newline'.

I would expect "parseint" to "eat" the character that terminates the number. So if you send "1 2 3\n", parsing the third number will read the \n as well, leaving the next Serial.read() to return the next character AFTER that (or -1 if nothing is there.)
(This doesn't explain why it seems to work when using the serial monitor.)

If this is correct, you can probably fix it by having the matlab code send an explicit terminator before the newline ("1 2 3 \n" - note the extra space.)

westfw:
If this is correct, you can probably fix it by having the matlab code send an explicit terminator before the newline ("1 2 3 \n" - note the extra space.)

this didn't work.

this didn't work.

Of course not. You are expecting the line feed to arrive within a few nanoseconds of the last space. Get real.

PaulS:

this didn't work.

Of course not. You are expecting the line feed to arrive within a few nanoseconds of the last space. Get real.

Are you just being sarcastic or suggesting to include a delay?

He's suggesting a delay.
In general, if you're expecting to read certain characters, you should replace Serial.read() calls with a function that waits for the traffic:

int getcwait() {
   while (Serial.available() == 0)
     { /* spin */ }
   return Serial.read();
}