how to send Data from matlab GUI to arduino? -
i'm doing remote control using matlab gui have no experience in matlab code, not implemented :
1- arduino code ( transmitter ) :
int matlabdata; const int apin=13; const int bpin=12; #include <virtualwire.h> char *controller; void setup() { serial.begin(9600); pinmode(apin,output); vw_set_ptt_inverted(true); // vw_set_tx_pin(bpin); vw_setup(4000);// speed of data transfer kbps } void get_action(){ if(serial.available()>0) // if there data read matlabdata=serial.read(); // read data if(matlabdata==1){ //forward controller="f"; serial.println("forward"); } if(matlabdata==2){ //backward controller="b"; serial.println("backward"); } if(matlabdata==3){ //right controller="r"; serial.println("right"); } if(matlabdata==4){ //left controller="l"; serial.println("left"); } if(matlabdata==5) { //stop controller="s"; serial.println("stop"); } } } void send_rf(){ vw_send((uint8_t *) controller, strlen(controller)); vw_wait_tx(); // wait until whole message gone digitalwrite(apin,1); delay(50); digitalwrite(apin,0); } void loop(){ get_action(); send_rf(); //delay(1000); }
2- matlab code "here problem" :
clear clc answer=1; % we'll store user's answer arduino=serial('com10','baudrate',9600); % create serial communication object on port com10 fopen(arduino); % initiate arduino communication while answer fprintf(arduino,'%s',char(answer)); % send answer variable content arduino function pushbutton1_callback(hobject, eventdata, handles) answer= 1; function pushbutton2_callback(hobject, eventdata, handles) answer= 2; function pushbutton3_callback(hobject, eventdata, handles) answer= 3; function pushbutton4_callback(hobject, eventdata, handles) answer= 4; function pushbutton5_callback(hobject, eventdata, handles) answer= 5; end fclose(arduino); % end communication arduino
3-arduino code ( receiver) :
const int apin=13; const int bpin=12; #include <virtualwire.h> #include "l298_motor.h" l298_motor l298(5,4,6,7); void setup() { serial.begin(9600); l298.enable_active(11,10); l298.enable_a('on'); l298.stop(); vw_set_ptt_inverted(true); // required dr3100 vw_set_rx_pin(bpin); vw_setup(4000); // bits per sec pinmode(apin, output); vw_rx_start(); // start receiver pll running } void loop() { uint8_t buf[vw_max_message_len]; uint8_t buflen = vw_max_message_len; if (vw_get_message(buf, &buflen)) // non-blocking { if(buf[0]=='f'){ //forward l298.backward(150); // serial.println("forward"); digitalwrite(apin,1); } if(buf[0]=='b'){ //backward l298.forward(150); // serial.println("backward"); digitalwrite(apin,1); } if(buf[0]=='r'){ //right l298.turnleft(140); // serial.println("right"); digitalwrite(apin,0); } if(buf[0]=='l'){ //left l298.turnright(140); // serial.println("left"); digitalwrite(apin,0); } if(buf[0]=='s'){ //stop' l298.stop(); // serial.println("stop"); digitalwrite(apin,0); } } }
please ..
1- don't define callback functions inside while loop.
2- variable answer not global. can use setappdata
set answer variable.
3- code sending variable answer fast possible! don't that, have use sleep(time)
in while loop.
if don't need continuous data streaming, avoid while , call fprintf inside callbacks.
Comments
Post a Comment