Calculate RPS by Matlab GUI and Arduino. I can't put the timing correctly. how can I do it?
1 view (last 30 days)
Show older comments
% --- Executes on button press in togglebutton3.
function togglebutton3_Callback(hObject, eventdata, handles)
% hObject handle to togglebutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of togglebutton3
global a;
tic
c = 0; %initial amount of c as 0
while get(hObject,'Value')
ti=toc
val = readDigitalPin(a, 'D6'); %reading the analog value from the sensor
if val == 1 %if it gets 1
c=c+1 %c increments by 1
c=c/ti %dividing number of pulses by seconds
set(handles.edit2,'String', c) %displays rev/sec
elseif val==0
set(handles.edit2,'String', 0) %displays rev/sec
end
pause(0.2);
end
0 Comments
Answers (1)
BhaTTa
on 11 Mar 2025
Hey @Omar Hameed, Let's take a look at your setup for calculating RPS using MATLAB GUI and Arduino. It seems like there are a couple of tweaks we can make to get the timing right and ensure you're calculating revolutions per second accurately.
Here's a refined version of your code with some explanations:
% --- Executes on button press in togglebutton3.
function togglebutton3_Callback(hObject, eventdata, handles)
global a; % Make sure your Arduino object is accessible
c = 0; % Start with zero pulses counted
prevTime = tic; % Start the timer to measure time intervals
while get(hObject, 'Value') % Keep running as long as the button is toggled on
% Read the digital signal from the Arduino
val = readDigitalPin(a, 'D6');
% Check if a pulse is detected
if val == 1
c = c + 1; % Increment the pulse count
% Calculate the elapsed time since the last reset
elapsedTime = toc(prevTime);
% Compute revolutions per second
if elapsedTime > 0
rps = c / elapsedTime; % Calculate RPS
set(handles.edit2, 'String', rps); % Update the GUI with the RPS value
end
% Reset for the next measurement
c = 0;
prevTime = tic; % Restart the timer
end
% Small pause to prevent rapid fluctuations and handle debounce
pause(0.2);
end
end
To accurately calculate revolutions per second (RPS) using MATLAB GUI and Arduino, you need to ensure that the timing is correctly managed. The current implementation has some issues: the value of c is being divided by ti inside the loop, which may not give you the correct RPS, and the timing (tic and toc) is not being reset correctly for each revolution.
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!