Borrar filtros
Borrar filtros

How to fix this issue, I'm trying to run a temperature sensor in Arduino Uno

1 visualización (últimos 30 días)
>> % 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.

Respuestas (1)

Manikanta Aditya
Manikanta Aditya el 1 de Mayo de 2024
Editada: Manikanta Aditya el 2 de Mayo de 2024
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.

Categorías

Más información sobre Graphics Object Identification en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by