如何将数据从MatlabGUI发送到arduino?
我正在用MatlabGUI做一个远程控制 但是我在MATLAB方面没有经验 这是代码,但未实现: 1-arduino代码(变送器): 3-arduino代码(接收器):如何将数据从MatlabGUI发送到arduino?,matlab,arduino,Matlab,Arduino,我正在用MatlabGUI做一个远程控制 但是我在MATLAB方面没有经验 这是代码,但未实现: 1-arduino代码(变送器): 3-arduino代码(接收器): const int APIN=13; 常数int BPIN=12; #包括 #包括“L298_电机.h” L298_电机L298(5,4,6,7); 无效设置() { Serial.begin(9600); L298.使能_激活(11,10); L298.启用_A('ON'); L298.停止(); vw_set_ptt_inv
const int APIN=13;
常数int BPIN=12;
#包括
#包括“L298_电机.h”
L298_电机L298(5,4,6,7);
无效设置()
{
Serial.begin(9600);
L298.使能_激活(11,10);
L298.启用_A('ON');
L298.停止();
vw_set_ptt_inversed(true);//DR3100需要
vw_设置_接收_引脚(BPIN);
vw_设置(4000);//位/秒
pinMode(APIN,输出);
vw_rx_start();//启动接收器PLL运行
}
void循环()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8\u t buflen=大众最大消息长度;
if(vw_get_message(buf,&buflen))//非阻塞
{
if(buf[0]=='F'){
//前进
L298.向后(150);
//序列号。打印号(“转发”);
数字写入(APIN,1);
}
if(buf[0]=='B'){
//落后的
L298.前进(150);
//SERIAL.PRINTLN(“向后”);
数字写入(APIN,1);
}
if(buf[0]=='R'){
//对
L298.左转(140);
//序列号。打印号(“右”);
数字写入(APIN,0);
}
if(buf[0]=='L'){
//左
L298.右转(140);
//SERIAL.PRINTLN(“左”);
数字写入(APIN,0);
}
if(buf[0]=='S'){
//停下来
L298.停止();
//SERIAL.PRINTLN(“停止”);
数字写入(APIN,0);
}
}
}
请帮助..1-不要在while循环中定义回调函数 2-变量答案不是全局的。您可以使用
setappdata
设置应答变量
3-您的代码正在尽可能快地发送变量答案!不要这样做,您必须在while循环中使用sleep(TIME)
如果不需要连续数据流,请避免使用while,并在回调中调用fprintf MinaKamel感谢您的帮助,但您的意思是这样的代码:
function pushbutton1_Callback(hObject, eventdata, handles)
setappdata(handles.pushbutton1,'Forward',1)
answer=getappdata(handles.pushbutton1,'Forward');
function pushbutton2_Callback(hObject, eventdata, handles)
setappdata(handles.pushbutton2,'Backward',2)
answer=getappdata(handles.pushbutton2,'Backward');
while answer
fprintf(arduino,'%s',char(answer)); % send answer variable content to arduino
answer ;
sleep(1000) ;
end
MinaKamel感谢您的帮助,但您的意思是这样的代码:
函数pushbutton1\u回调(hObject,eventdata,handles)setappdata(handles.pushbutton1,'Forward',1)answer=getappdata(handles.pushbutton1,'Forward');函数pushbutton2_回调(hObject,eventdata,handles)setappdata(handles.pushbutton2,'Backward',2)answer=getappdata(handles.pushbutton2,'Backward');当回答fprintf(arduino,'%s',char(回答));%向arduino应答发送应答变量内容;睡眠(1000);结束
clear all
clc
answer=1; % this is where we'll store the 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 to 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 with arduino
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 FOR DR3100
vw_set_rx_pin(BPIN);
vw_setup(4000); // BITS PER SEC
pinMode(APIN, OUTPUT);
vw_rx_start(); // START THE 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);
}
}
}
function pushbutton1_Callback(hObject, eventdata, handles)
setappdata(handles.pushbutton1,'Forward',1)
answer=getappdata(handles.pushbutton1,'Forward');
function pushbutton2_Callback(hObject, eventdata, handles)
setappdata(handles.pushbutton2,'Backward',2)
answer=getappdata(handles.pushbutton2,'Backward');
while answer
fprintf(arduino,'%s',char(answer)); % send answer variable content to arduino
answer ;
sleep(1000) ;
end