前提・実現したいこと
MCP9600というK型熱電対をつかって温度の記録をグラフ化したいと思っています。
今はWINDOWS7からARDUINOUNOを動かしています。
https://ncd.io/k-type-thermocouple-mcp9600-with-arduino/
このサイトからコードをコピーしてARDUINOで温度を計ることはできました。
いろいろなサイトを検索してFirmatというライブラリもいれました。
#include <Boards.h> #include <Firmata.h> #include <FirmataConstants.h> #include <FirmataDefines.h> #include <FirmataMarshaller.h> #include <FirmataParser.h> #include<Wire.h> #define Addr 0x67 long data1; long data2; long temp; byte stat; void setup() { // Initialise I2C communication Wire.begin(); // Initialise Serial Communication, set baud rate = 9600 Serial.begin(9600); temrmo_set(); device_set(); Serial.println("start"); } ///// this function will read the hot and cold junction temp int read_temp() { Wire.beginTransmission(Addr); Wire.write(0x00); Wire.endTransmission(); Wire.requestFrom(Addr, 2); if (Wire.available() == 2) { data1 = Wire.read(); data2 = Wire.read(); // Serial.println(data1); // Serial.println(data2); if((data1 & 0x80) == 0x80) { data1 = data1 & 0x7F; temp = 1024 - ( data1 * 16 + data2/16); Serial.print("Temperature :"); Serial.print(temp); Serial.println("\n"); } else data1 = data1 *16; data2 = data2 * 0.0625; // Serial.println(data1); // Serial.println(data2); temp = data1 + data2; // temp = ( data1 * 16 + data2/16); Serial.print("Temperature :"); Serial.print(temp); Serial.println("\n"); } } /// this function can be used to see if the temp conversion is complete or not int temp_stat() { Wire.beginTransmission(Addr); Wire.write(0x04); Wire.endTransmission(); delay(50); Wire.requestFrom(Addr, 1); if (Wire.available() == 1) { stat = Wire.read(); } // Serial.print(stat); return stat; } /// this functon can be used to clear the temp conversion flag int temp_stat_clr() { Wire.beginTransmission(Addr); Wire.write(0x04); Wire.write(0x0F); Wire.endTransmission(); } ////// this function can be used to set the thermocuple configuration int temrmo_set() { Wire.beginTransmission(Addr); Wire.write(0x05); Wire.write(0x00); Wire.endTransmission(); } ////////////// this function can be used to set the device config int device_set() { Wire.beginTransmission(Addr); Wire.write(0x06); Wire.write(0x00); Wire.endTransmission(); } void loop() { temp_stat(); byte thermo_ready = (stat && 0x40); if(thermo_ready) { read_temp(); // Serial.print("hello"); Serial.println(temp); } temp_stat_clr(); delay(1000); }
これに対してのProcessingのコードは以下のサイトを参考にしてつくりました。
http://tetsuakibaba.jp/index.php?page=workshop/csv/main
import cc.arduino.*; import org.firmata.*; import processing.serial.*; PrintWriter output; Serial myPort; boolean flg_start = false; long time=0; String str_format = "Temperature,temp"; String port = "COM3"; void setup() { size(320, 240); myPort = new Serial(this, port, 9600); } void draw() { while ( myPort.available() > 0 ) { String inBuffer = myPort.readString(); if ( inBuffer != null ) { if ( flg_start ) output.print(inBuffer); } } } void keyPressed() { if ( key == 's' ) { flg_start = !flg_start; if ( flg_start == false ) { // end of recording output.flush(); output.close(); } else if ( flg_start == true ) { // begining of recording String filename = nf(year(), 4) + nf(month(), 2) + nf(day(), 2) + nf(hour(), 2) + nf(minute(), 2) ; output = createWriter( filename + ".csv"); output.println( str_format ); } } }
発生している問題・エラーメッセージ
ここからProcessingを実効するのですが、
CSVファイルは作成されるのですが、
中に温度が記入されません。
ファイル名は時間どおりに作成されます。
該当のソースコード
String str_format = "Temperature,temp"; String port = "COM3";
このあたりが怪しいのですが、
まだ勉強したばかりでわかりません。
試したこと
以下のサイトのコードをコピペなどしましたが、
よくわかりませんでした。
https://yoppa.org/tau_bmaw13/4772.html
https://auxin01.wordpress.com/2012/07/15/arduino_and_pc_4/
補足情報(FW/ツールのバージョンなど)
初心者で申し訳ありませんが、
修正ポイントを教えていただければと思います。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/03 06:38