This code is a modified version of the code supplied to me by John Brooks - currently also at Gloyer-Taylor Laboratories - for reading in thermocouple data into Matlab for data manipulation. Matlab can control an Arduino board with the Arduino IO package freely available by Matlab but it's somewhat limited in its overall functionality. On the other hand, Matlab makes data manipulation much more convenient than doing so on the board itself.
This code continually searches the serial port for new data sent from the board and stores it, plots it, and writes it to a data file. It can be set up for single or multiple sources sent to the same port. For instance, I use it to record temperatures for multiple thermocouples.
clc; clear all; close all;
% Finds all previously opened ports and closes/deletes them
delete(instrfindall);
% Specify Outputs
plotrealtime='y';
writetofile='y';
datafolder='Multi_Test';
num_sources=3;
if strcmp(writetofile,'y')
date_time=clock;
outputfilename = [num2str(date_time(1)),'-',num2str(date_time(2)),'-',...
num2str(date_time(3)),' ',num2str(date_time(4)),'-',...
num2str(date_time(5)),'-',num2str(date_time(6))];
if exist(datafolder,'dir')==0
mkdir(datafolder)
end
end
%% open port.
% creates and opens new serial port
s = serial ('COM3');
fopen(s);
%% initialize variables
tab=char(9);
I=1;
%data=zeros(1000,3);
%% clears buffer
while(s.BytesAvailable > 0)
fscanf(s);
end
%% start timer
tic;
%% main function
K=1;
while(true)
% pause(1)
%% check to see if data is available at the port
if s.BytesAvailable > 0
%% read data and record time
data=str2double(fscanf(s));
if isempty(data)
break;
else
time=toc;
end
%% adds an additional 1000 points to data and time
% if rem(I,1000)==0
% data=cat(1,data,zeros(1000,2+num_sources));
% end
%% plots results
if strcmp(plotrealtime,'y')
colors1 = '-k.-b.-g.-r.-m.-c.-k.-b.-g.-r.-m.-c.-k.-b.-g.-r.-m.-c.-k.-b.-g.-r.-m.-c.';
plot(time,data,colors1(K*3-2:K*3),'MarkerSize',10);hold on;
xlabel('Time (seconds)')
ylabel('Data')
drawnow
end
%% display results to command window
disp([I,time,data])
if strcmp(writetofile,'y')
dlmwrite([datafolder,'\',outputfilename,'_',num2str(K),'.dat'],[I,time,data],'-append','newline','pc','delimiter','\t')
end
%% iterate counter
% data(I+1,:)=data(I,:);
I=I+1;
K=K+1;
if K>num_sources
K=1;
end
end
end
%% close port
%fclose(s)
and the arduino code
/***************************************************
* This is an example for the Adafruit Thermocouple Sensor w/MAX31855K
*
* Designed specifically to work with the Adafruit Thermocouple Sensor
* ----> https://www.adafruit.com/products/269
*
* These displays use SPI to communicate, 3 pins are required to
* interface
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Limor Fried/Ladyada for Adafruit Industries.
* BSD license, all text above must be included in any redistribution
****************************************************/
#include "Adafruit_MAX31855.h"
void setup() {
Serial.begin(9600);
//Serial.println("MAX31855 test");
// wait for MAX chip to stabilize
delay(500);
}
void loop() {
// basic readout test, just print the current temp
//Serial.print("Internal Temp = ");
//Serial.println(thermocouple.readInternal());
//int thermoDO = 8;
//int thermoCS = 2;
//int thermoCLK = 3;
find_temp(0,2,3);
find_temp(4,5,6);
find_temp(8,9,10);
delay(1000);
}
void find_temp(int thermoDO,int thermoCS,int thermoCLK){
Adafruit_MAX31855 thermocouple(thermoCLK, thermoCS, thermoDO);
double C = thermocouple.readCelsius();
double F = thermocouple.readFarenheit();
if (isnan(C)) {
Serial.println("Something wrong with thermocouple!");
}
else {
//Serial.print("C = ");
Serial.println(C);
//Serial.println(F);
}
//Serial.print("F = ");
//Serial.println(thermocouple.readFarenheit());
//delay(1000);
}
Wednesday, July 10, 2013
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment