% AUTHORS: Ana Marta Dias & Pedro Sousa

clear; clc;

% Muscle Choice Menu
Muscles = ["Deltoid", "Biceps"];
MuscleChoose = 2;
while MuscleChoose ~= 0 && MuscleChoose ~= 1  % Mode Input by the User
    MuscleChoose = input("Muscle for Signal Acquisition: Deltoid - 0 / Biceps - 1: ");
end
Muscles = Muscles(MuscleChoose + 1); clear MuscleChoose;

% Variable Initialization
Fs = 20;
readingTime = 10;       % (in seconds)

% Arduino Setup Configurations
clear a;
a = arduino();
configurePin(a, 'A1', 'AnalogInput');

% For the DELTOID
if Muscles == "Deltoid"
    
    % Loop Main Function
    while ~isempty(a)                                              % While connected to the serial port
        
        tStart = tic;
        
        % EMG Reading
        fprintf("\nReading Arduino Signal...")
        EMG = zeros(0);
        for i = 1 : readingTime * Fs
            tic;
            EMG(length(EMG) + 1) = readVoltage(a, 'A1') - 2.5;
            timeElapsed = toc;
            pause(1/Fs - timeElapsed);                            % Time between samples (in seconds)
        end
        tRead = toc(tStart);
        clear i;
        
        % Write EMG data to text file
        writematrix(([length(EMG); EMG' .* 1000]), "Txt Files\EMG Signal.txt"); % EMG Saving (in uV)
        
        % Calculate Zero Crossings and write to text file
        Low_Threshold = -100;
        High_Threshold = 100;
        ZeroCrossings = func_ZC(EMG, Low_Threshold, High_Threshold, Fs);
        writematrix([length(ZeroCrossings); ZeroCrossings'], 'Txt Files\Zero Crossings.txt');
        clear Low_Threshold High_Threshold;
        
        % Threshold
        threshold = 145;
        warnM = ZeroCrossings >= threshold;     % Warning Matrix
        writematrix([length(warnM); warnM'], 'Txt Files\Warning.txt');
        clear threshold warnM;
        
        % Calculate HF/LF Ratio and write to text file
        Ratio = func_DeltoidHFLF(EMG, Fs);
        writematrix([length(Ratio); Ratio'], 'Txt Files\Ratio HF_LF.txt');
        
        % Calculate Tonic/Clonic Time
        TonicTime = thresholdDeltoid(Ratio);
        writematrix(TonicTime, 'Txt Files\Tonic Time.txt');
        
        %plot(EMG);
        tEnd = toc(tStart);
        fprintf('\nSignal Acquired: %i s\nReading Time: %.3f s\nFull Time: %.3f s', readingTime, tRead, tEnd)
        
    end
    
% For the BICEPS    
else
    
    % Loop Main Function
    while ~isempty(a)                                  % While connected to the serial port
    
        
        tStart = tic;
        
        % EMG Reading
        fprintf("\n\nReading Arduino Signal...\n")
        EMG = zeros(0);
        for i = 1 : readingTime * Fs
            tic;
            EMG(length(EMG) + 1) = readVoltage(a, 'A1') - 2.5;
            timeElapsed = toc;
            pause(1/Fs - timeElapsed);                            % Time between samples (in seconds)
        end
        tRead = toc(tStart);
        clear i;
        
        % Write EMG data to text file
        writematrix(([length(EMG); EMG' .* 1000]), "Txt Files\EMG Signal.txt"); % EMG Saving (in uV)

         % Calculate Zero Crossings and write to text file
        Low_Threshold = -100;
        High_Threshold = 100;
        ZeroCrossings = func_ZC(EMG, Low_Threshold, High_Threshold, Fs);
        writematrix([length(ZeroCrossings); ZeroCrossings'], 'Txt Files\Zero Crossings.txt');
        clear Low_Threshold High_Threshold;
        
        % Threshold
        threshold = 145;
        warnM = ZeroCrossings >= threshold;     % Warning Matrix
        writematrix([length(warnM); warnM'], 'Txt Files\Warning.txt');
        clear threshold warnM;
        
        % Calculate HF/LF Ratio and write to text file
        [Ratio, maxHF, maxLF] = func_BicepsHFLF(EMG, Fs);
        writematrix([length(Ratio); Ratio'], 'Txt Files\Ratio HF_LF.txt');
        
        % Calculate Tonic and Clonic Time
        [TonicTime, ClonicTime] = thresholdBiceps(Ratio, maxHF, maxLF, Fs);
        writematrix(TonicTime, 'Txt Files\Tonic Time.txt');
        writematrix(ClonicTime, 'Txt Files\Clonic Time.txt');

        %plot(EMG);
        tEnd = toc(tStart);
        fprintf('\nSignal Acquired: %i s\nReading Time: %.3f s\nFull Time: %.3f s', readingTime, tRead, tEnd)
            
    end
end
