matlab - Numerical data from simulink to arduino uno -


i want send numerical data simulink arduino uno.

because don't know how make work in simulink i'm trying matlab.

this code sends numerical data char. 1 character @ time arduino. after that, have concatenate characters construct numerical value , give arduino treat. send matlab same way.

i know if there possibility send numerical data numeric arduino, , send matlab/simulink numerical data.

this code i'm using in matlab :

close all; clear ;  clc; delete (instrfind({'port'},{'com5'}))  s = serial('com5');  set(s,'databits',8); set(s,'stopbits',1); set(s,'baudrate',4800); set(s,'parity','none');  fopen(s)  while (1)     if  (s.bytesavailable)         readdata=fscanf(s)     else         fprintf(s,'arduino');     end end  fclose(s) 

and code i'm using in arduino :

int sensorpin = a0;   int sensorvalue = 0;  char incomingdata;  void setup() {   serial.begin(4800); }  void loop() {      if (serial.available() > 0)      {       incomingdata = serial.read(); //read incoming data       serial.println(incomingdata);       delay(100);     }     else {        sensorvalue = analogread(sensorpin);       serial.println(sensorvalue);           delay(100);     } } 

i ask question above. , found answer, wanted share you.

first, have close communications serial port, , initialise values of communication. way.

close ;  clc; delete (instrfind({'port'},{'com3'})) s = serial('com3'); set(s,'databits',8); set(s,'stopbits',1); set(s,'baudrate',9600); set(s,'parity','none'); 

remarque : not time have 'com3' port, have see in arduino port using. also, have make exacte value of "baudrate" in matlab , in arduino too.

second, send float number, read way :

yourvalue = 123.456; % exemple while (1)     fprintf(s,'%f',yourvalue);  % sendig value arduino     readdata=fscanf(s,'%f')     % receiving value arduino , printing end fclose(s) 

now, arduino part, simple. code presented below :

int sensorpin = a0;    // select input pin potentiometer int sensorvalue = 0;  // variable store value coming sensor float incomingdata;  void setup() {   serial.begin(9600); }  void loop() {    if (serial.available())    {     incomingdata = serial.parsefloat(); //read incoming data     delay(100);   }   else    {     serial.println(incomingdata);     delay(100);   } } 

remarque : when send many values matlab arduino, send them matlab. have first value printed way [] or way 0. don't know problem is.

thanks of !!


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -