Borrar filtros
Borrar filtros

How to make the x-axis of a graph restart from 0 again when pushing a start button after have stopped the plotting process?

5 visualizaciones (últimos 30 días)
I'm working in a GUI which shows current and speed vs time of a DC Motor.
When pushing the start button from the beginning both graphs start from time 0.
The problem that I have is that when pushing the stop buttton to stop the process and when I'm using the GUI to collect these signals to plot them, so I would like you to help me to make the ESP32 time set to zero when pushing the start button again to plot current and speed correctly.
I'm attaching the code as well as the video that shows the GUI working.
% Button pushed function: StartButton
function StartButtonPushed(app, event)
clear app.current app.speed app.time
app.current = [];
app.speed = [];
app.time = [];
cla(app.UIAxes);
cla(app.UIAxes2);
set(app.StartButton,'Enable','off')
set(app.SaveButton,'Enable','off')
set(app.LoadButton,'Enable','off')
if strcmpi(app.StartButton.Enable,'off')
MQTTSignal = mqttclient('tcp://broker.hivemq.com','Port',1883);
Topic = "ESP32/status";
Message = "1";
app.SystemOffLamp.Color = 'g';
app.SystemOffLabel.Text = 'System On';
write(MQTTSignal,Topic,Message);
end
% Connection to MQTT
MQTTSignal = mqttclient('tcp://broker.hivemq.com','Port',1883);
Topic = "ESP32/data";
Data = subscribe(MQTTSignal,Topic);
pause(3);
while strcmpi(app.StartButton.Enable,'off')
dataTT = read(MQTTSignal);
str = (dataTT.Data);
str = strrep(str, '{', ''); % remove opening bracket
str = strrep(str, '}', ''); % remove closing bracket
str = split(str,",");
app.current_temp = str2double(str(:,1));
app.speed_temp = str2double(str(:,2));
app.time_temp = str2double(str(:,3));
app.current = [app.current;app.current_temp];
app.speed = [app.speed;app.speed_temp];
app.time = [app.time;app.time_temp];
plot(app.UIAxes,app.time,app.current);
plot(app.UIAxes2,app.time,app.speed);
drawnow
pause(3)
end
% Button pushed function: StopButton
function StopButtonPushed(app, event)
if strcmpi(app.StopButton.Enable,'on')
MQTTSignal = mqttclient('tcp://broker.hivemq.com','Port',1883);
Topic = "ESP32/status";
Message = "0";
app.SystemOffLamp.Color = 'r';
app.SystemOffLabel.Text = 'System Off';
write(MQTTSignal,Topic,Message);
end
set(app.StartButton,'Enable','on');
set(app.SaveButton,'Enable','on');
  2 comentarios
Ayush
Ayush el 29 de Mayo de 2023
To set the ESP32 time to zero when pushing the start button again, you can use the following steps:
  1. Add a variable to store the start time when the start button is pressed for the first time.
  2. When the start button is pressed, check if the start time variable is null. If it is null, set the start time to the current time.
  3. If the start time variable is not null, subtract it from the current time to get the elapsed time. Use this elapsed time as the x-axis value for the current and speed graphs.
  4. When the stop button is pressed, set the start time variable to null.

Iniciar sesión para comentar.

Respuesta aceptada

Ayush
Ayush el 31 de Mayo de 2023
To set the ESP32 time to zero when pushing the start button again, you can use the following steps:
  1. Add a variable to store the start time when the start button is pressed for the first time.
  2. When the start button is pressed, check if the start time variable is null. If it is null, set the start time to the current time.
  3. If the start time variable is not null, subtract it from the current time to get the elapsed time. Use this elapsed time as the x-axis value for the current and speed graphs.
  4. When the stop button is pressed, set the start time variable to null.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by