//Pro
clc;
clf;
function y = adctotemp(rd)
    m=0.103;
    c=298;
    y=(rd*m+c)-273;
endfunction

serialport=openserial(4,"9600,n,8,1");   // open serial port
numsamples = 10;
temp=[1];       // initialise array to hold temperature;
for i=1:numsamples
    q=[0,0];
    while q(1) < 1                            // pause while no data in serial buffer
        [q,flags]=serialstatus(serialport);
    end
    
    values='';
    value=ascii(0);
    v='';
    
    while q(1) > 0                          // if data in the buffer
        while(value~=ascii(13))
            value=readserial(serialport,1);
            values=values+value;
            v=strsubst(values,string(ascii(10)),'')
            v=strsubst(v,string(ascii(13)),'')
            data=evstr(v);
            q=q-1;
        end
        
        q(1)=q(1)-1;
    end
    
    counts = data;
    
    temp(i)=adctotemp(counts);
    
    mprintf("Input device temperature is %2.2f degrees C \n",temp(i));


end
set(gca(),"grid",[1 1]);
plot(temp);
set(gca(),"grid",[1 1]);
set(gca(),"data_bounds",matrix([1,numsamples,0,55],2,-1));
xtitle("Temperature/Time", "Time S", " Temperature C");
closeserial(serialport);               //close serial port                  
