currentSoilState

 avatar
unknown
matlab
5 months ago
813 B
12
Indexable
function state = currentSoilState( a )

    % Calibration Information (Replace with your measured values)
    % Not immersed (in air): 2.4 Volts
    % Immersed in dry soil: 2.3 Volts
    % Immersed in water-saturated soil: 2.1 Volts
    % Immersed in a glass of water: 2.0 Volts

    %Read voltage from sensor at pin A0
    voltage = readVoltage(a, 'A0');
    
    % Define voltage thresholds (replace with your measured values)
    thresholds = [2.4, 2.3, 2.1, 2.0];
    states = {'dry air', 'dry soil', 'wet soil', 'water'};

    % Find the closest state
    [~, idx] = min(abs(thresholds - voltage));
    state = states{idx};

    % Handle equidistant case
    if idx > 1 && abs(thresholds(idx) - voltage) == abs(thresholds(idx - 1) - voltage)
        state = states{idx - 1}; % Choose drier state
    end
end 
Editor is loading...
Leave a Comment