Schritt 2: Schreiben Sie den code
ARDUINO CODE
Der Code für Arduino ist im Grunde dasselbe wie du hier findest: sondern weil hier ich nur wollen die u/min zeigen Wert (nicht der rps-Wert und der "rps" und "u" / Text), so ich bearbeitet einige Linien (diejenigen, die Serial.print(), weil vorher das Programm entwickelt wurde, um das Lesen auf serial Monitor Notizblock-Stil zu präsentieren, aber jetzt brauchen wir nur die u/min noch-Wert um in den statischen Text-String zu füttern). OK, um es schnell und einfach, die ich kopieren Sie den Code können hier, Sie durch Ihren selbst für den Vergleich mit dem vorherigen Code überprüfen. Denken Sie daran, dass der Hauptzweck des Arduino Code nur serielle Comm einen Wert übergeben, so ist dieses Programm nur als Beispiel, haben Sie Ihr eigenes Programm, das den Wert der jeden Sensorwert auf seriell druckt dann ignorieren diese gehen voran, die MATLAB-Code.
int sensorvalue;<br>int state1 = HIGH; int state2; float rps; float rpm; long prevMillis = 0; long interval = 100; long currentTime; long prevTime = 1; long diffTime; int sensorthreshold = 30; // this value indicates the limit reading between dark and light, // it has to be tested as it may change acording on the // distance the leds are placed. // to see what number is good, check the sensorvalue variable value // as printed out in the serial monitor</p><p>void setup() { Serial.begin(9600); pinMode(13,OUTPUT); // assign pin 13 led as indicator because we cannot se the IR light } void loop() { sensorvalue = analogRead(0); // read from pin 0 if(sensorvalue < sensorthreshold) state1 = HIGH; else state1 = LOW; digitalWrite(13,state1); // as iR light is invisible for us, the led on pin 13 // indicate the state of the circuit. if(state2!=state1){ //counts when the state change, thats from (dark to light) or //from (light to dark), remember that IR light is invisible for us. if (state2>state1){ currentTime = micros(); // Get the arduino time in microseconds diffTime = currentTime - prevTime; // calculate the time difference from the last sensors meet-up rps = 1000000/diffTime; // calculate how many rev per second - good to know rpm = 60000000/diffTime; // calculate how many rev per minute unsigned long currentMillis = millis(); // print to serial at every interval - defined at the variables declaration if(currentMillis - prevMillis > interval){ // see if now already an interval long prevMillis = currentMillis; Serial.println(rpm); // this line is edited from the code in the prev instructable } prevTime = currentTime; } state2 = state1; } /* only for testing to determine the sensorthreshold value delay(500); Serial.println(sensorvalue); */ }
MATLAB-CODE
Klicken Sie in Ihrem MATLAB GUI Layout Design-Fenster auf Ansicht - Editor (oder finden Sie in der Symbolleiste ein Bild von Papier und Bleistift ohne Hand). Öffnet sich ein Editorfenster mit einigen Codes bereits geschrieben, schrieb MATLAB für Sie kein Problem. Nur schreiben der Callback-Funktion für die Anzeige-Taste, der Rest des Codes unangetastet bleiben kann. In meinem Fall benannt habe ich meine Umschaltfläche OnOffToggle, so ist die Funktion, unter der ich den Code schrieb, Funktion OnOffToggle_Callback (hObject, Eventdata, Handles).
Keine Daten über die serielle gesendet werden hier abgerufen als eine Variable Rpmdata, so dass wir nur, dass u/min-Daten auf dem Bildschirm ausdrucken möchten, das war's. Eine weitere Sache, überprüfen Sie, ob Sie Ihre Arduino mit den richtigen COM-Port verbunden sind, wie Sie in den Code schreiben. In diesem Code habe ich weil ich mein Arduino COM3 Verbindung COM3 geschrieben.
Dann speichern Sie die m-Datei.
Im folgenden ist der vollständige Code (Edit nur die OnOffToggle_Callback-Funktion):
function varargout = gui(varargin) % GUI MATLAB code for gui.fig % GUI, by itself, creates a new GUI or raises the existing % singleton*. % % H = GUI returns the handle to a new GUI or the handle to % the existing singleton*. % % GUI('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in GUI.M with the given input arguments. % % GUI('Property','Value',...) creates a new GUI or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before gui_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to gui_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES % Edit the above text to modify the response to help gui % Last Modified by GUIDE v2.5 14-Mar-2015 01:06:09</p><p>% Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', ... 'gui_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 % End initialization code - DO NOT EDIT % --- Executes just before gui is made visible. function gui_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to gui (see VARARGIN) % Choose default command line output for gui handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes gui wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = gui_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output; function currentEdit_Callback(hObject, eventdata, handles) function currentEdit_CreateFcn(hObject, eventdata, handles) % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end function OnOffToggle_Callback(hObject, eventdata, handles) button_state = get(hObject,'Value'); if button_state == get(hObject,'Max') set(handles.OnOffToggle,'String','Stop'); drawnow; i=2; while i > 1 rpmdata = serial('COM3','BaudRate',9600); % this Baud rate should be the same as that in Arduino code fclose(instrfindall); fopen(rpmdata); b = fscanf(rpmdata); set(handles.textCurrent,'String',b); drawnow; delete(rpmdata) if get(hObject,'Value') == get(hObject,'Min') break end end set(handles.OnOffToggle,'String','Start'); drawnow; rpmdata = serial('COM3','BaudRate',9600); fclose(rpmdata); end