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'.