0

I'm currently working on a homework problem in simulating a vehicle monitoring system. The problem is with my loop to start the problem when I want to end the program all the user should do is press two and the program will break to the very end. However this does not happen, however everything else in the program works. Thanks!

% VehicleMonitoring.m
% this program monitors several vehicle measurements
% the program then displays the values of those measurements
% back to the user and finally plots graphs of the measurements
%
%
%
%Declaration of variables
%
%EngineOn = variable to begin running the test loop
%ActualTirePressure = Randomized variable assigned to actual tire pressure
%NormalTirePressure = variable array assigned to normal tire pressure
%ActualOilPressure = Randomized Variable assigned to actual oil pressure
%NormalOilpressure = variable  array assigned to normal oil pressure
%ActualWaterTemp = Randomized variable assigned to actual engine temp
%NormalWaterTemp = variable array assigned to normal water tmep
%ActualChargeVolt = Randomized variable assigned to actual alternator output
%NormalChargeVolt = variable array assigned to normal alternator output
%ActualBattVolt = Randomized variable assigned to actual battery voltage
%NormalBattVolt = variable array assigned to normal battery voltage
%ActualTransTemp = randomized variable assigned to actual trans temp
%NormalTransTemp = variable array assigned to normal transmission temp
 
EngineOn = input('Do you want to run the engine? press 1 press 2 if you do not.'); % Assigns variable to start monitor
while EngineOn ~= 2 % if user does not press 2 loop will keep repeating
switch (EngineOn)   % switch statement to begin testing
    case {1}
 
% Measure and output the tire air Pressure
ActualTirePressure = (rand * 50); %create air pressure measurement
NormalTirePressure = 20:.1:35;    %array of normal tire pressures
% algorithm to output temperature to user
if ActualTirePressure > NormalTirePressure
    fprintf('\nWarning Tire Air Pressure HIGH:  %2.0f \n', ActualTirePressure);
     elseif ActualTirePressure < NormalTirePressure
        fprintf('\nWarning Tire Air Pressure LOW:  %2.0f \n', ActualTirePressure);
         elseif ActualTirePressure < 10
           fprintf('\nWARNING A TIRE IS FLAT!!!  %2.0f \n', ActualTirePressure);
else fprintf('\nTire Air Pressure is NORMAL:  %2.0f \n', ActualTirePressure);    
end %end of tire pressure testing
% Measure and output the engine oil pressure
ActualOilPressure = (rand * 100); %create oil pressure measurement
NormalOilPressure = 20:.1:80;     %array of normal oil pressures
if ActualOilPressure > NormalOilPressure
    fprintf('\nWarning Oil Pressure HIGH:  %2.0f \n', ActualOilPressure);
     elseif ActualOilPressure < NormalOilPressure
        fprintf('\nWarning Oil Pressure LOW:  %2.0f \n', ActualOilPressure);
else fprintf('\nOil Pressure is NORMAL:  %2.0f \n', ActualOilPressure);    
end %end of oil pressure testing
% Measure and output the Engine Water Temperature
ActualWaterTemp = (rand * 250); %create water temperature measurements
NormalWaterTemp = 32:.1:210;    %array of normal water temperatures
if ActualWaterTemp > NormalWaterTemp
    fprintf('\nWarning Charging System Voltage is HIGH:  %3.0f \n', ActualWaterTemp);
else fprintf('\nEngine Water Temp is NORMAL:  %3.0f \n', ActualWaterTemp);    
end %end of water temperature testing
% Measure and output the Charging System Voltage
ActualChargeVolt = (rand * 20); %create charging system voltage meausrement
NormalChargeVolt = 11:.1:18;    %array of normal charing system voltages
if ActualChargeVolt > NormalChargeVolt
    fprintf('\nWarning Charging System Voltage is HIGH:  %4.2f \n', ActualChargeVolt);
        elseif ActualChargeVolt < NormalChargeVolt
            fprintf('\nWarning Charging System Voltage is LOW:  %4.2f \n', ActualChargeVolt);
else fprintf('\nCharging System Voltage is NORMAL:  %4.2f \n', ActualChargeVolt);    
end %end of charging system testing
% Measure and output the battery voltage
ActualBattVolt = (rand * 18); % creates battery voltage measurement
NormalBattVolt = 7.2:.1:14.4; % array of normal battery voltages
if ActualBattVolt > NormalBattVolt
    fprintf('\nWarning Battery Voltage is HIGH:  %4.2f \n', ActualBattVolt);
        elseif ActualBattVolt < NormalBattVolt
            fprintf('\nWarning Battery Voltage is LOW:  %4.2f \n', ActualBattVolt);
else fprintf('\nBattery is NORMAL:  %4.2f \n', ActualBattVolt);
end %end of battery voltage testing
% Measure and output the transmission oil Temperature
ActualTransTemp = (rand * 250); % creates transmission temperature measurements
NormalTransTemp = 0:.1:240;     % array of normal transmission temperatures
if ActualTransTemp > NormalTransTemp
    fprintf('\nWarning Transmission Temp is HIGH:  %3.0f \n', ActualTransTemp);
        else fprintf('\nTransmission Temp is NORMAL:  %3.0f \n', ActualTransTemp);    
end %end of engine transmission temperatures testing
%this section creates a figure with the plots of the measurements and then
%graphs them each time erasing the previous plot 
hold off; %creates plots overides and erases plot from previous loop
axis on;  %creates new axis each time the plot is created
figure(1) % creates a plot graph
subplot(2,3,1), plot(ActualTirePressure, NormalTirePressure);
title('\bfTire Pressure PSI'), ylabel('Normal Tire Pressure'), xlabel('Actual Tire Pressure');
subplot(2,3,2), plot(ActualOilPressure, NormalOilPressure);
title('\bfOil Pressure PSI'), ylabel('Normal Oil Pressure'), xlabel('Actual Oil Pressure');
subplot(2,3,3), plot(ActualWaterTemp, NormalWaterTemp);
title('\bfWater Temperature \circ F'), ylabel('Normal Water Temp'), xlabel('Actual Water Temp');
subplot(2,3,4), plot(ActualChargeVolt, NormalChargeVolt);
title('\bfCharging Volts'), ylabel('Normal Charging Volts'), xlabel('Actual Charging Volts');
subplot(2,3,5), plot(ActualBattVolt, NormalBattVolt);
title('\bfBattery Volts'), ylabel('Normal Battery Volts'), xlabel('Actual Battery Volts');
subplot(2,3,6), plot(ActualTransTemp, NormalTransTemp);
title('\bfTransmission Temp \circ F'), ylabel('Normal Trans Temp'), xlabel('Actual Trans Temp');
    case{2} % if user does not select to start the loop  this is the other choice to break and end the program 
  end % end of case switch statement
end % end of while loop
disp('have a nice day');
2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by b4codes
0

I have seen similar problem on other matlab forum http://www.kluid.com . Your code is pretty long, sorry couldn't read it all, the simplest tweak will be just add

if EngineOn == 2
error('You have chosen to stop the program')
end

after this

while EngineOn ~= 2 % if user does not press 2 loop will keep repeating

this will terminate the loop when user presses 2.

: ] B4

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.