1

I have to send from MATLAB to Arduino a char line 'S' to start the Arduino program and send back values from a sensor. This is my MATLAB code

clear;
delete(instrfind);
s = serial('/dev/cu.usbmodem1411','BaudRate',9600, 'Terminator','CR/LF');
fopen(s);
fprintf(s,'%s\n','S');

t = 0;
valor=zeros(1,100);
tiempo=zeros(1,100);
fid=fopen('resultats.txt','w');
for i=1:100         
    a = fscanf(s,'%d');
    wd = 0.0189*a - 14.499;
    valor(i)=wd;
    tiempo(i)=t;
    fprintf(fid,'%3d %6d\n', wd,t);  
    t = t+200;
end
plot(tiempo,valor);
fclose(fid);
fclose(s);

and my Arduino code is this

int outValue = 0;
unsigned long time;
void setup() {
  Serial.begin(9600);
  while (Serial.available() <= 0 || Serial.read() != 'S') {
    Serial.println("Send 'S' to start");
    delay(200); }
  Serial.println("Starting measurement");
}
void loop() {
  outValue = analogRead(A0);
  Serial.println(outValue);
  delay(200);
}

the problem is that when I use the MATLAB code it send an 'S' and it makes Arduino start sending values back, but Arduino does not do that and just sending the while loop again and again.

Is the problem in the MATLAB code when it send the char 'S'?

  • Could you try delaying for a couple of seconds between fopen(s); and fprintf(s,'%s\n','S');? – Edgar Bonet Apr 25 '17 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.