a = arduino('com3','uno','libraries','ExampleLCD/LCDAddon');          % creates an arduino object specifying the ExampleLCD/LCDAddon library.
lcd = addon(a,'ExampleLCD/LCDAddon',{'D7','D6','D5','D4','D3','D2'}); % creates the LCD object, and specifies the pins configured on the Arduino device.
initializeLCD(lcd, 'Rows', 2, 'Columns', 16);                         % initializes the lcd screen

setpref('Internet','SMTP_Server','smtp.gmail.com');
setpref('Internet','E_mail','bwnanney@gmail.com'); % mail account to send from
setpref('Internet','SMTP_Username','bwnanney');    % senders username
setpref('Internet','SMTP_Password','Morill1204');  % senders password
props = java.lang.System.getProperties;
props.setProperty('mail.smtp.auth','true');
props.setProperty('mail.smtp.socketFactory.class', 'javax.net.ssl.SSLSocketFactory');
props.setProperty('mail.smtp.socketFactory.port','465');


b=readDigitalPin(a,'D9');           % read a digital pin (a digital sensor)
reading= readVoltage(a,'A0');       % read the voltage on analog thermister connected to pin A0
reading2= readVoltage(a,'A2');      % read the voltage on analog thermister connected to pin A2
while true                          % loops through the existing statement as long as true
    
    b=readDigitalPin(a,'D9');       % read a digital pin (a digital sensor)
    reading= readVoltage(a,'A0');   % read the voltage on analog thermister connected to pin A0
    if reading>= 0.6 && reading< 0.68 % conditionally executes a single set of statements (a block).
        clearLCD(lcd)               % clears the lcd screen
        printLCD(lcd, 'Temp 64 F'); % Print a string to the LCD.
        pause(0.5)                  % pauses before executing the next command
        
    elseif reading>= 0.68 && reading<0.69 % conditionally executes a single set of statements (a block).
        clearLCD(lcd)               % clears the lcd screen
        printLCD(lcd, 'Temp 66 F'); % Print a string to the LCD.
        pause(0.5)                  % pauses before executing the next command
        
    elseif reading>= 0.69 && reading<0.7  % conditionally executes a single set of statements (a block).
        clearLCD(lcd)               % clears the lcd screen
        printLCD(lcd, 'Temp 68 F'); % Print a string to the LCD.
        pause(0.5)                  % pauses before executing the next command
        
    elseif reading>=0.7 && reading<0.71  % conditionally executes a single set of statements (a block).
        clearLCD(lcd)               % clears the lcd screen
        printLCD(lcd, 'Temp 70 F'); % Print a string to the LCD.
        pause(0.5)                  % pauses before executing the next command
        
    elseif reading>=0.71 && reading<0.72 % conditionally executes a single set of statements (a block).
        clearLCD(lcd)               % clears the lcd screen
        printLCD(lcd, 'Temp 72 F'); % Print a string to the LCD.
        pause(0.5)                  % pauses before executing the next command
        
    elseif reading>=0.72 && reading<0.73 % conditionally executes a single set of statements (a block).
        clearLCD(lcd)               % clears the lcd screen
        printLCD(lcd, 'Temp 74 F'); % Print a string to the LCD.
        pause(0.5)                  % pauses before executing the next command
        
    elseif reading>= 0.73 && reading<0.74 % conditionally executes a single set of statements (a block).
        clearLCD(lcd)               % clears the lcd screen
        printLCD(lcd, 'Temp 76 F'); % Print a string to the LCD.
        pause(0.5)                  % pauses before executing the next command
        
    elseif reading>= 0.74 && reading<0.75 % conditionally executes a single set of statements (a block).
        clearLCD(lcd)               % clears the lcd screen
        printLCD(lcd, 'Temp 78 F'); % Print a string to the LCD.
        pause(0.5)                  % pauses before executing the next command
        
    elseif reading>= 0.75 && reading<0.76 % conditionally executes a single set of statements (a block).
        clearLCD(lcd)               % clears the lcd screen
        printLCD(lcd, 'Temp 80 F'); % Print a string to the LCD.
        pause(0.5)                  % pauses before executing the next command
        
    elseif reading>= 0.76 && reading<0.77 % conditionally executes a single set of statements (a block).
        clearLCD(lcd)               % clears the lcd screen
        printLCD(lcd, 'Temp 82 F'); % Print a string to the LCD.
        pause(0.5)                  % pauses before executing the next command
        
    elseif reading>= 0.77 && reading<0.8 % conditionally executes a single set of statements (a block).
        clearLCD(lcd)               % clears the lcd screen
        printLCD(lcd, 'Temp 84 F'); % Print a string to the LCD.
        pause(0.5)                  % pauses before executing the next command
        
        playTone(a,'D10',880,0.25) %  plays a note(a,pin,frequency,duration)
        pause(0.3)                 %  pauses after note for set duration
        playTone(a,'D10',880,0.25) %  plays a note(a,pin,frequency,duration)
        pause(0.3)                 %  pauses after note for set duration
        playTone(a,'D10',880,0.25) %  plays a note(a,pin,frequency,duration)
        pause(0.3)                 %  pauses after note for set duration
        playTone(a,'D10',880,0.25) %  plays a note(a,pin,frequency,duration)
        pause(0.3)                 %  pauses after note for set duration
        playTone(a,'D10',987,0.25) %  plays a note(a,pin,frequency,duration)
        pause(0.3)                 %  pauses after note for set duration
        playTone(a,'D10',987,0.25) %  plays a note(a,pin,frequency,duration)
        pause(0.3)                 %  pauses after note for set duration
        playTone(a,'D10',880,0.12) %  plays a note(a,pin,frequency,duration)
        pause(0.1)                 %  pauses after note for set duration
        playTone(a,'D10',740,0.12) %  plays a note(a,pin,frequency,duration)
        pause(0.1)                 %  pauses after note for set duration
        playTone(a,'D10',587,0.25) %  plays a note(a,pin,frequency,duration)
        pause(0.3)
        
        sendmail('bwnanney@gmail.com','Heating and Cooling Unit in Need of Service!!!', ...
            'The heating and cooling system is operating inefficiently. Please have a technician service the unit as soon as possible.') % Sends an email to the specified email account
        
    elseif reading>= 0.8 && reading<0.85 % conditionally executes a single set of statements (a block).
        clearLCD(lcd)               % clears the lcd screen
        printLCD(lcd, 'Temp 86 F'); % Print a string to the LCD.
        pause(0.5)                  % pauses before executing the next command
        
    elseif reading>= 0.85 && reading<2 % conditionally executes a single set of statements (a block).
        clearLCD(lcd)                     % clears the lcd screen
        printLCD(lcd, 'HOT HOT BABY!!!'); % Print a string to the LCD.
        pause(0.5)                      % pauses before executing the next command
        
    end % ends if statement
    
    
    
    
    while b==0                                  % loops through the existing statement as long as false
        
        b=readDigitalPin(a,'D9');               % read a digital pin (a digital sensor)
        reading2= readVoltage(a,'A2');          % read the voltage on analog thermister connected to pin A2
        if reading2>= 0.6 && reading2< 0.68     % conditionally executes a single set of statements (a block).
            clearLCD(lcd)                       % clears the lcd screen
            printLCD(lcd, 'Efficiency 80%');    % Print a string to the LCD.
            pause(0.5)                          % pauses before executing the next command
            
        elseif reading2>= 0.68 && reading2<0.69 % conditionally executes a single set of statements (a block).
            clearLCD(lcd)                       % clears the lcd screen
            printLCD(lcd, 'Efficiency 90%');    % Print a string to the LCD.
            pause(0.5)                          % pauses before executing the next command
            
        elseif reading2>= 0.69 && reading2<0.7  % conditionally executes a single set of statements (a block).
            clearLCD(lcd)                       % clears the lcd screen
            printLCD(lcd, 'Efficiency 95%');    % Print a string to the LCD.
            pause(0.5)                          % pauses before executing the next command
            
        elseif reading2>=0.7 && reading2<0.71   % conditionally executes a single set of statements (a block).
            clearLCD(lcd)                       % clears the lcd screen
            printLCD(lcd, 'Efficiency 97%');    % Print a string to the LCD.
            pause(0.5)                          % pauses before executing the next command
            
        elseif reading2>=0.71 && reading2<0.72  % conditionally executes a single set of statements (a block).
            clearLCD(lcd)                       % clears the lcd screen
            printLCD(lcd, 'Efficiency 100%');   % Print a string to the LCD.
            pause(0.5)                          % pauses before executing the next command
            
        elseif reading2>=0.72 && reading2<0.73  % conditionally executes a single set of statements (a block).
            clearLCD(lcd)                       % clears the lcd screen
            printLCD(lcd, 'Efficiency 95%');    % Print a string to the LCD.
            pause(0.5)                          % pauses before executing the next command
            
        elseif reading2>= 0.73 && reading2<0.74 % conditionally executes a single set of statements (a block).
            clearLCD(lcd)                       % clears the lcd screen
            printLCD(lcd, 'Efficiency 90%');    % Print a string to the LCD.
            pause(0.5)                          % pauses before executing the next command
            
        elseif reading2>= 0.74 && reading2<0.75 % conditionally executes a single set of statements (a block).
            clearLCD(lcd)                       % clears the lcd screen
            printLCD(lcd, 'Efficiency 85%');    % Print a string to the LCD.
            pause(0.5)                          % pauses before executing the next command
            
        elseif reading2>= 0.75 && reading2<0.76 % conditionally executes a single set of statements (a block).
            clearLCD(lcd)                       % clears the lcd screen
            printLCD(lcd, 'Efficiency 80%');    % Print a string to the LCD.
            pause(0.5)                          % pauses before executing the next command
            
        elseif reading2>= 0.76 && reading2<0.77 % conditionally executes a single set of statements (a block).
            clearLCD(lcd)                       % clears the lcd screen
            printLCD(lcd, 'Efficiency 75%');    % Print a string to the LCD.
            pause(0.5)                          % pauses before executing the next command
            
        elseif reading2>= 0.77 && reading2<0.8  % conditionally executes a single set of statements (a block).
            clearLCD(lcd)                       % clears the lcd screen
            printLCD(lcd, 'Efficiency 70%');    % Print a string to the LCD.
            pause(0.5)                          % pauses before executing the next command
            
        elseif reading2>= 0.8 && reading2<0.85  % conditionally executes a single set of statements (a block).
            clearLCD(lcd)                       % clears the lcd screen
            printLCD(lcd, 'Efficiency 65%');    % Print a string to the LCD.
            pause(0.5)                          % pauses before executing the next command
            
        elseif reading2>= 0.85 && reading2<2    % conditionally executes a single set of statements (a block).
            clearLCD(lcd)                       % clears the lcd screen
            printLCD(lcd, 'ON FIRE!!!');        % Print a string to the LCD.
            pause(0.5)                          % pauses before executing the next command
            
        end % end if statement
    end % end while loop
end % end while loop