Temperature App Development - Code 3 of 3

Опубликовано: 26 Февраль 2026
на канале: Doug Klein
1,034
0

Code Insert 1
Voltage = 5; % Placeholder for Voltage
TempK = 298; % Place Holder for Temp in Kelvin

switch app.UnitsDropDown.Value %Identifying what to reference

case "Voltage" % When the string is "Voltage"
app.TempLabel.Text = num2str(Voltage,3); % Convert the value to a string and assign to the label. Precision 3
case 'Celsius' % When the string is "Celsius"
TempC = TempK - 273.15; % Convert Kelvin to Celsius
app.TempLabel.Text = num2str(TempC,3); % Convert the value to a string and assign to the label. Precision 3
case 'Fahrenheit' % When the string is "Fahrenheit"
TempF=((TempK -273.15)*1.8)+32; % Convert Kelvin to Fahrenheit
app.TempLabel.Text = num2str(TempF,3); % Convert the value to a string and assign to the label. Precision 3
end

Code Insert 2
app.a = arduino(); % Connects the Arduino
app.TakeReadingA0Button.BackgroundColor = [0.30,0.75,0.93]; % Changes color of button after connection


Code Insert 3
volts = readVoltage(app.a,'A1'); % Get value from the digital pin an store it in digital
R2=10000; % the value of the precision resistor
temp0 = 298.15; % reference temperature (25 C), in Kelvin
res0 = 10000; % resistance at reference temperature, in Ohms
B = 3950; % thermistor B parameter, in Kelvin
current=volts/R2; % calculate the current using Ohm's law
Resistance=(5-volts)/current; % find the resistance of the thermistor
recip_temp = 1/temp0 + log(Resistance/res0)/B; % Calculate the reciprocal of the TempK
TempK = 1./recip_temp; % Calculate Temp in Kelvin

switch app.UnitsDropDown.Value % Identifying what to reference

case "Voltage" % When the string is "Voltage"
app.TempLabel.Text = num2str(volts,3) % Convert the value to a string and assign to the label. Precision 3
case "Celsius" % When the string is "Voltage"
TempC = TempK -273.15 % Convert to Celsius
app.TempLabel.Text = num2str(TempC,3) % Convert the value to a string and assign to the label. Precision 3
case "Fahrenheit" % When the string is "Voltage"
TempF=((TempK -273.15)*1.8)+32; % Convert to Fahrenheit
app.TempLabel.Text = num2str(TempF,3) % Convert the value to a string and assign to the label. Precision 3
end