How to fix this issue, I'm trying to run a temperature sensor in Arduino Uno
2 次查看(过去 30 天)
显示 更早的评论
>> % Create Arduino object
a = arduino();
% Define pin numbers
lm35Pin = 'A0';
fanPin = 'D9';
% Threshold temperature values (manipulate values if necessary)
fanOnTemp = 30.0;
fanOffTemp = 35.0;
% Create figure and axes
figure;
ax = axes('Parent', gcf);
% Initialize variables
time = 0;
temperature = 0;
fanState = 0; % 0 = Fan off, 1 = Fan on
% Initialize arrays to store temperature and time data
maxDataPoints = 60; % Adjust the number of data points to display
timeData = zeros(1, maxDataPoints);
temperatureData = zeros(1, maxDataPoints);
% Plot the initial data
plot (ax, timeData, temperatureData, 'r', 'LineWidth', 2);
xlabel ('time (s)');
ylabel ('Temperature (°C)');
title ('Real-Time LM35 Temperature Plot');
grid on;
xlim ([0, maxDataPoints]);
ylim ([min(temperatureData)-1, max(temperatureData)+1]); % Initialize y-axis limits
% Main loop
while true
% Read voltage from LM35D2 sensor
sensorValue = readVoltage (a, lm35Pin);
% Convert sensor value to temperature in Celsius
voltage = sensorValue * (1 / 0.00488750);
temperature = voltage * 0.48828125;
% Print the temperature
fprintf ('Temperature: %.2f*C\n', temperature);
% Store the temperature and time data in arrays
timeData = [timeData (2:end), time];
temperatureData = [temperatureData(2:end), temperature];
% Update the plot
set (ax, Children, 'XData', timeData, 'YData', temperatureData);
% Adjust y-axis limits to fit the current temperature range
ylim ([min(temperatureData)-1, max(temperatureData)+1]);
% Check if temperature exceeds the threshold to turn on the fan
if temperature > fanOnTemp && ~fanState
% Turn on the fan
writeDigitalPin(a, fanPin, 1);
disp('Fan is ON');
fanState = 1;
elseif temperature < fanOffTemp && fanState
% Turn off the fan
writeDigitalPin(a, fanPin, 0);
disp('Fan is OFF');
fanState = 0;
end
% Delay between readings
pause(1);
% Update time
time = time + 1;
end
Error: The end operator must be used within an array index expression.
I highlighted the error in bold style.
0 个评论
回答(1 个)
Manikanta Aditya
2024-5-1
编辑:Manikanta Aditya
2024-5-2
The error message you’re seeing is due to the use of end in the set function call. In MATLAB, end is used to refer to the last element of an array or matrix. However, it can only be used within an array index expression.
This code will fix the errors you encountered:
% Create Arduino object
a = arduino();
% Define pin numbers
lm35Pin = 'A0';
fanPin = 'D9';
% Threshold temperature values (manipulate values if necessary)
fanOnTemp = 30.0;
fanOffTemp = 35.0;
% Create figure and axes
figure;
ax = axes('Parent', gcf);
% Initialize variables
time = 0;
temperature = 0;
fanState = 0; % 0 = Fan off, 1 = Fan on
% Initialize arrays to store temperature and time data
maxDataPoints = 60; % Adjust the number of data points to display
timeData = zeros(1, maxDataPoints);
temperatureData = zeros(1, maxDataPoints);
% Plot the initial data
h = plot(ax, timeData, temperatureData, 'r', 'LineWidth', 2);
xlabel ('time (s)');
ylabel ('Temperature (°C)');
title ('Real-Time LM35 Temperature Plot');
grid on;
xlim ([0, maxDataPoints]);
ylim ([min(temperatureData)-1, max(temperatureData)+1]); % Initialize y-axis limits
% Main loop
while true
% Read voltage from LM35D2 sensor
sensorValue = readVoltage (a, lm35Pin);
% Convert sensor value to temperature in Celsius
voltage = sensorValue * (1 / 0.00488750);
temperature = voltage * 0.48828125;
% Print the temperature
fprintf ('Temperature: %.2f*C\n', temperature);
% Store the temperature and time data in arrays
timeData = [timeData(2:end), time];
temperatureData = [temperatureData(2:end), temperature];
% Update the plot
set(h, 'XData', timeData, 'YData', temperatureData);
% Adjust y-axis limits to fit the current temperature range
ylim ([min(temperatureData)-1, max(temperatureData)+1]);
% Check if temperature exceeds the threshold to turn on the fan
if temperature > fanOnTemp && ~fanState
% Turn on the fan
writeDigitalPin(a, fanPin, 1);
disp('Fan is ON');
fanState = 1;
elseif temperature < fanOffTemp && fanState
% Turn off the fan
writeDigitalPin(a, fanPin, 0);
disp('Fan is OFF');
fanState = 0;
end
% Delay between readings
pause(1);
% Update time
time = time + 1;
end
Hope it helps.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!