classdef arduinodesigngroup < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
StartTurbiditysensorButton matlab.ui.control.Button
Gaugemeter matlab.ui.control.LinearGauge
GaugemeterLabel matlab.ui.control.Label
SoilmoisturesensorPanel matlab.ui.container.Panel
PowerswitchPanel matlab.ui.container.Panel
ONLamp matlab.ui.control.Lamp
ONLampLabel matlab.ui.control.Label
PowerSwitch matlab.ui.control.Switch
LeakageLamp matlab.ui.control.Lamp
LeakageLampLabel matlab.ui.control.Label
waterpumpSwitch matlab.ui.control.RockerSwitch
waterpumpSwitchLabel matlab.ui.control.Label
PumpLamp matlab.ui.control.Lamp
PumpLampLabel matlab.ui.control.Label
OffalarmButton matlab.ui.control.Button
ControlpanelPanel matlab.ui.container.Panel
WaterlevelsensorPanel matlab.ui.container.Panel
WaterLevelLabel matlab.ui.control.Label
Lamp matlab.ui.control.Lamp
StopButton matlab.ui.control.Button
StartwaterlevelsensorButton matlab.ui.control.Button
UIAxes2 matlab.ui.control.UIAxes
UIAxes matlab.ui.control.UIAxes
end
properties (Access = private)
Property % Description
ard = arduino();
end
properties (Access = public)
Property2 % Description
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: OffalarmButton
function OffalarmButtonPushed(app, event)
app.LeakageLamp.Color=[1 0 0];
end
% Value changed function: PowerSwitch
function PowerSwitchValueChanged(app, event)
p = app.PowerSwitch.Value;
if p == "On"
app.ONLamp.Color = [0 1 0];
while p ~= "Off"
vl = readDigitalPin(app.ard, 'D8');
if vl == 0
app.LeakageLamp.Color = [0 1 0]; % Set leakage lamp color to red
else
app.LeakageLamp.Color = [1 0 0]; % Set leakage lamp color to green
clear ard
end
% p = app.PowerSwitch.Value; % Update the value of p inside the loop
pause(1)
end
clear ard
else
app.ONLamp.Color = [1 0 0];
end
clear ard
end
% Value changed function: waterpumpSwitch
function waterpumpSwitchValueChanged(app, event)
motor = app.waterpumpSwitch.Value;
if motor=="On"
while(motor~="Off")
app.PumpLamp.Color=[0 1 0];
writePWMVoltage(app.ard, 'D10', 0); % Set the voltage on the digital pin
writePWMVoltage(app.ard, 'D9', 0)
pause(1000)
end
clear ard
else
app.PumpLamp.Color=[1 0 0];
writePWMVoltage(app.ard, 'D10', 5); % Set the voltage on the digital pin
writePWMVoltage(app.ard, 'D9', 5)
pause(1000)
end
clear ard
end
% Button pushed function: StartwaterlevelsensorButton
function StartwaterlevelsensorButtonPushed(app, event)
global k c
x=0;
br=0;
c = 0;
for k=1:1:inf
b=app.ard.readVoltage('A2');
x=[x,b];
plot(x,'parent',app.UIAxes);
app.Gaugemeter.Value = app.ard.readVoltage('A2');
if b>3
app.Lamp.Color = [0,1,0];
app.WaterLevelLabel.Text="High";
elseif 3>b && 2<b
app.Lamp.Color = [1,1,0];
app.WaterLevelLabel.Text="Low";
elseif b<2
app.Lamp.Color = [1,0,0];
app.WaterLevelLabel.Text="Empty";
end
pause(1);
end
end
% Button pushed function: StopButton
function StopButtonPushed(app, event)
global c
c = 1;
end
% Button pushed function: StartTurbiditysensorButton
function StartTurbiditysensorButtonPushed(app, event)
% Set up Arduino
% Clear previous graph data
cla(app.UIAxes2);
% Initialize variables
numReadings = 5;
interval = 1;
turbidityReadings = zeros(1, numReadings);
turbidityLevels = [1 2 3 4 5]; % Example turbidity levels
% Iterate through each water sample
for i = 1:length(turbidityLevels)
% Update button text
app.StartTurbiditysensorButton.Text = 'Reading...';
drawnow; % Force immediate GUI update
% Display the turbidity level
fprintf('Test %d: NTU\n', turbidityLevels(i));
% Read turbidity sensor
turbidity = readVoltage(app.ard, 'A0');
% Display the reading
fprintf('Turbidity Reading: %.2f\n', turbidity);
% Store the reading
turbidityReadings(i) = turbidity;
% Update the graph
plot(app.UIAxes2, turbidityLevels, turbidityReadings);
xlabel(app.UIAxes2, 'Test');
ylabel(app.UIAxes2, 'Turbidity Reading');
title(app.UIAxes2, 'Turbidity Sensor Readings');
grid(app.UIAxes2, 'on');
drawnow; % Force immediate GUI update
% Wait for a moment before moving to the next sample
pause(2);
end
% Clean up
clear ard;
% Update button text
app.StartTurbiditysensorButton.Text = 'Start';
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 1075 640];
app.UIFigure.Name = 'MATLAB App';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Voltage vs time')
xlabel(app.UIAxes, 'Time/s')
ylabel(app.UIAxes, 'Voltage/V')
zlabel(app.UIAxes, 'Z')
app.UIAxes.Position = [533 258 439 313];
% Create UIAxes2
app.UIAxes2 = uiaxes(app.UIFigure);
title(app.UIAxes2, 'Title')
xlabel(app.UIAxes2, 'X')
ylabel(app.UIAxes2, 'Y')
zlabel(app.UIAxes2, 'Z')
app.UIAxes2.Position = [33 258 472 329];
% Create ControlpanelPanel
app.ControlpanelPanel = uipanel(app.UIFigure);
app.ControlpanelPanel.ForegroundColor = [1 1 1];
app.ControlpanelPanel.Title = 'Control panel';
app.ControlpanelPanel.BackgroundColor = [0.4941 0.1843 0.5569];
app.ControlpanelPanel.FontWeight = 'bold';
app.ControlpanelPanel.FontSize = 18;
app.ControlpanelPanel.Position = [34 7 1009 217];
% Create WaterlevelsensorPanel
app.WaterlevelsensorPanel = uipanel(app.ControlpanelPanel);
app.WaterlevelsensorPanel.Title = 'Water level sensor';
app.WaterlevelsensorPanel.BackgroundColor = [0.0745 0.6235 1];
app.WaterlevelsensorPanel.FontWeight = 'bold';
app.WaterlevelsensorPanel.FontSize = 14;
app.WaterlevelsensorPanel.Position = [799 14 190 158];
% Create StartwaterlevelsensorButton
app.StartwaterlevelsensorButton = uibutton(app.WaterlevelsensorPanel, 'push');
app.StartwaterlevelsensorButton.ButtonPushedFcn = createCallbackFcn(app, @StartwaterlevelsensorButtonPushed, true);
app.StartwaterlevelsensorButton.BackgroundColor = [0.3922 0.8314 0.0745];
app.StartwaterlevelsensorButton.Position = [25 58 140 23];
app.StartwaterlevelsensorButton.Text = 'Start water level sensor';
% Create StopButton
app.StopButton = uibutton(app.WaterlevelsensorPanel, 'push');
app.StopButton.ButtonPushedFcn = createCallbackFcn(app, @StopButtonPushed, true);
app.StopButton.BackgroundColor = [1 0 0];
app.StopButton.Position = [45 14 100 23];
app.StopButton.Text = 'Stop';
% Create Lamp
app.Lamp = uilamp(app.WaterlevelsensorPanel);
app.Lamp.Position = [127 94 20 20];
app.Lamp.Color = [1 1 1];
% Create WaterLevelLabel
app.WaterLevelLabel = uilabel(app.WaterlevelsensorPanel);
app.WaterLevelLabel.FontWeight = 'bold';
app.WaterLevelLabel.Position = [43 95 68 22];
app.WaterLevelLabel.Text = 'WaterLevel';
% Create SoilmoisturesensorPanel
app.SoilmoisturesensorPanel = uipanel(app.UIFigure);
app.SoilmoisturesensorPanel.Title = 'Soil moisture sensor';
app.SoilmoisturesensorPanel.BackgroundColor = [0.302 0.7451 0.9333];
app.SoilmoisturesensorPanel.FontWeight = 'bold';
app.SoilmoisturesensorPanel.FontSize = 14;
app.SoilmoisturesensorPanel.Position = [260 18 541 164];
% Create OffalarmButton
app.OffalarmButton = uibutton(app.SoilmoisturesensorPanel, 'push');
app.OffalarmButton.ButtonPushedFcn = createCallbackFcn(app, @OffalarmButtonPushed, true);
app.OffalarmButton.BackgroundColor = [1 0.4118 0.1608];
app.OffalarmButton.Position = [406 20 100 23];
app.OffalarmButton.Text = 'Off alarm';
% Create PumpLampLabel
app.PumpLampLabel = uilabel(app.SoilmoisturesensorPanel);
app.PumpLampLabel.HorizontalAlignment = 'right';
app.PumpLampLabel.Position = [284 95 36 22];
app.PumpLampLabel.Text = 'Pump';
% Create PumpLamp
app.PumpLamp = uilamp(app.SoilmoisturesensorPanel);
app.PumpLamp.Position = [335 95 20 20];
app.PumpLamp.Color = [1 0 0];
% Create waterpumpSwitchLabel
app.waterpumpSwitchLabel = uilabel(app.SoilmoisturesensorPanel);
app.waterpumpSwitchLabel.HorizontalAlignment = 'center';
app.waterpumpSwitchLabel.Position = [417 66 67 22];
app.waterpumpSwitchLabel.Text = 'water pump';
% Create waterpumpSwitch
app.waterpumpSwitch = uiswitch(app.SoilmoisturesensorPanel, 'rocker');
app.waterpumpSwitch.Orientation = 'horizontal';
app.waterpumpSwitch.ValueChangedFcn = createCallbackFcn(app, @waterpumpSwitchValueChanged, true);
app.waterpumpSwitch.Position = [428 101 45 20];
% Create LeakageLampLabel
app.LeakageLampLabel = uilabel(app.SoilmoisturesensorPanel);
app.LeakageLampLabel.HorizontalAlignment = 'right';
app.LeakageLampLabel.Position = [277 29 51 22];
app.LeakageLampLabel.Text = 'Leakage';
% Create LeakageLamp
app.LeakageLamp = uilamp(app.SoilmoisturesensorPanel);
app.LeakageLamp.Position = [343 29 20 20];
app.LeakageLamp.Color = [1 0 0];
% Create PowerswitchPanel
app.PowerswitchPanel = uipanel(app.SoilmoisturesensorPanel);
app.PowerswitchPanel.Title = 'Power switch';
app.PowerswitchPanel.BackgroundColor = [0.8549 0.702 0.949];
app.PowerswitchPanel.FontWeight = 'bold';
app.PowerswitchPanel.FontSize = 18;
app.PowerswitchPanel.Position = [34 34 204 82];
% Create PowerSwitch
app.PowerSwitch = uiswitch(app.PowerswitchPanel, 'slider');
app.PowerSwitch.ValueChangedFcn = createCallbackFcn(app, @PowerSwitchValueChanged, true);
app.PowerSwitch.FontColor = [0.149 0.149 0.149];
app.PowerSwitch.Position = [121 13 45 20];
% Create ONLampLabel
app.ONLampLabel = uilabel(app.PowerswitchPanel);
app.ONLampLabel.HorizontalAlignment = 'right';
app.ONLampLabel.Position = [21 13 25 22];
app.ONLampLabel.Text = 'ON';
% Create ONLamp
app.ONLamp = uilamp(app.PowerswitchPanel);
app.ONLamp.Position = [61 13 20 20];
app.ONLamp.Color = [1 0 0];
% Create GaugemeterLabel
app.GaugemeterLabel = uilabel(app.UIFigure);
app.GaugemeterLabel.BackgroundColor = [0 1 1];
app.GaugemeterLabel.HorizontalAlignment = 'center';
app.GaugemeterLabel.Position = [980 586 71 22];
app.GaugemeterLabel.Text = 'Gaugemeter';
% Create Gaugemeter
app.Gaugemeter = uigauge(app.UIFigure, 'linear');
app.Gaugemeter.Limits = [0 5];
app.Gaugemeter.Orientation = 'vertical';
app.Gaugemeter.BackgroundColor = [0 1 1];
app.Gaugemeter.Position = [991 258 52 313];
% Create StartTurbiditysensorButton
app.StartTurbiditysensorButton = uibutton(app.UIFigure, 'push');
app.StartTurbiditysensorButton.ButtonPushedFcn = createCallbackFcn(app, @StartTurbiditysensorButtonPushed, true);
app.StartTurbiditysensorButton.BackgroundColor = [0.3922 0.8314 0.0745];
app.StartTurbiditysensorButton.Position = [81 113 128 23];
app.StartTurbiditysensorButton.Text = 'Start Turbidity sensor';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = arduinodesigngroup
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
Comments