hi there,
i searched the forum some hints with my problem, but i didnt find the right solution.
i want to send multiple valules (read with my arduino inputs A0 A1 A2...) to Matlab.
i want to send something like this with serial
A013 B123 C534 D023 E234 F235 A014 B124...... over and over again (were A=A0, B=A1, C=A2...)
i can do it the other way, from Matlab to Arduino with switch cases, but i dont know how to do this in matlab.
here is my code with Arduino:
/*Send several Values to Matlab
*/
#include <Servo.h>
Servo meinServo;
int ServoRead = 12;
int LDR0;
int LDR1;
int messung = 0;
int positionservo = 90;
int messIntervall = 50; // measure intervall
int wert = 0;
unsigned long zeitLetzteMessung = 0;
int sensitivity = 30; //
float servospeed = 1; //
//---------------------------------------------------------------------------------
void setup()
{
Serial.begin(115200);
meinServo.attach(ServoRead);
meinServo.write(positionservo);
establishContact();
}
//-----------------------------------------------------------------------------------
void loop()
{
LDR0 = analogRead(A1);
LDR1 = analogRead(A2);
if(millis() - zeitLetzteMessung > messIntervall)
{
messung = (LDR1 - LDR0);
if(messung > sensitivity)
{ wert = 1*servospeed;}
if(messung < -(sensitivity))
{ wert = -1*servospeed;}
if(messung < sensitivity && messung > -(sensitivity))
{wert = 0;}
positionservo= positionservo + wert;
if(positionservo <= 0)
{positionservo = 0;}
if(positionservo >= 179)
{positionservo = 179;}
meinServo.write(positionservo);
zeitLetzteMessung = millis();
Serial.println(A); %Channel A0
Serial.println(LDR0);
Serial.println(B); %Channel A1
Serial.println(LDR1);
Serial.println(C); %Channel A2
Serial.println(0);
Serial.println(D); %Channel A3
Serial.println(0);
Serial.println(E); %Channel A4
Serial.println(0);
Serial.println(F); %Channel A5
Serial.println(0);
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.println('A', BYTE); // send a capital A
delay(300);
}
}
here s my Matlab code
function varargout = mygui(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @mygui_OpeningFcn, ...
'gui_OutputFcn', @mygui_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function mygui_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
% changeIcon
jFrame=get(handles.figure1,'javaframe');
jIcon=javax.swing.ImageIcon('icon.gif');
jFrame.setFigureIcon(jIcon);
% changeUserData
set(handles.pushbutton_run,'UserData',0);
function varargout = mygui_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function pushbutton_run_Callback(hObject, eventdata, handles)
if get(handles.pushbutton_run,'UserData')==1, return; end
set(handles.pushbutton_run,'UserData',1);
x = get(handles.pushbutton_run,'UserData');
disp('RUN')
s1 = serial('/dev/ttyACM0'); % define serial port
s1.BaudRate=115200; % define baud rate
set(s1, 'terminator', 'LF'); % define the terminator for println
fopen(s1);
try % use try catch to ensure fclose
% signal the arduino to start collection
w=fscanf(s1,'%s'); % must define the input % d or %s, etc.
if (w=='A')
display(['Collecting data']);
fprintf(s1,'%s\n','A'); % establishContact just wants
% something in the buffer
end
i=0;
%value = [];
while x==1;
y=fscanf(s1,'%d')
y=num2str(y);
set(handles.ausgabe,'String',y);
if get(handles.pushbutton_run,'UserData')==0
set(handles.ausgabe,'String','0');
pause(0.1);
%cla;
break;
end
pause(0.025); % Lese-Intervall, muss kleiner sein als Abtastintervall bei Arduino
x = get(handles.pushbutton_run,'UserData');
clc;
end
fclose(s1);
catch exception
fclose(s1); % always, always want to close s1
%throw (exception);
end
function pushbutton_stop_Callback(hObject, eventdata, handles)
set(handles.pushbutton_run,'UserData',0);
disp('STOP')
set(handles.ausgabe,'String','0');
function ausgabe_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function ausgabe_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
i think i have to send bytes, but i dont know how to do this exactly.
in my matlab code i connect with a gui interface.
if run is pressed, it connects and reveives the data in real time
if i hit stop, the connection is closed
no i want to display multiple values within the gui.
anybody can help me?
--moe