////////////////////////////////////////////////////////////////////////////////////////////// // THS119には定電流5mAを流し、出力電圧Vhを増幅。アナログピンA0とA1で Voutを読み取る。 // Vout(電圧)=ガウス値となるように抵抗器で増幅率を調整している // A0とA1で読み取る値は Vout(mV)/5000(mV)×1024 となる // モニターへ表示させるときは A0とA1で読み取った値からオフセット値を差し引きし、(5000(mV)/1024)を掛ける ////////////////////////////////////////////////////////////////////////////////////////////// #include // #include // #include // #if (SSD1306_LCDHEIGHT != 64) // #error("Height incorrect, please fix Adafruit_SSD1306.h!"); // #endif // Adafruit_SSD1306 display(-1); // RSTピンがない互換品を使用するので-1を指定 const int pinHall0 = A0; const int pinHall1 = A1; int state=0; // 初回判別用 float VperN=5000.000/1024; // 数値1当たりの電圧(mV) (1mV=1ガウス) //float offsetV0=0; // 初回判別用 //float offsetV1=0; // 初回判別用 long offmeasure0 = 0; // 初回判別用 long offmeasure1 = 0; // 初回判別用 char ns[2] = {'N', 'S'}; //OLEDモニターへの表示用 void setup() { display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //I2Cアドレスは使用するディスプレイに合わせて変更する pinMode(pinHall0, INPUT); pinMode(pinHall1, INPUT); // Serial.begin(9600); } void loop() { display.clearDisplay(); // 画面表示をクリア display.setTextColor(WHITE); // テキスト色を設定 //////////電源投入後の最初の1回目、磁石が近くにない状態でオフセット値(offmeasure0,1)を求める////////// if (state == 0) { for(int i = 0; i < 100; i++){ offmeasure0 += analogRead(pinHall0); offmeasure1 += analogRead(pinHall1); } offmeasure0 /= 100; offmeasure1 /= 100; // offsetV0 = offmeasure0 * VperN; // N極のオフセット電圧 = A0の出力数値 × (5000mV/1024) // offsetV1 = offmeasure1 * VperN; // S極のオフセット電圧 = A1の出力数値 × (5000mV/1024) state = 1; } //////////2回目以降、OLEDに表示////////// long measure0 = 0; long measure1 = 0; long gauss0 = 0; long gauss1 = 0; for(int i = 0; i < 100; i++){ measure0 += analogRead(pinHall0); measure1 += analogRead(pinHall1); } measure0 = measure0/100 - offmeasure0; // measure0 から初回に求めた offmeasure0 を差し引く measure1 = measure1/100 - offmeasure1; // measure1 から初回に求めた offmeasure1 を差し引く gauss0 = measure0 * VperN; // N極ガウス値 (measure0 * VperN)は電圧だが、電圧=ガウス値となるように増幅率を調整している gauss1 = measure1 * VperN; // S極ガウス値 (measure0 * VperN)は電圧だが、電圧=ガウス値となるように増幅率を調整している if (gauss0 < 10 && gauss1 < 10 ) { display.setTextSize(5); // テキストサイズを設定 display.setCursor(10, 15); // テキストの開始位置を設定(左から,上から) display.println("0"); // 「0」と表示 display.display(); // 描画バッファの内容を画面に表示 } else if (gauss0 > gauss1) { // N極ガウス値>S極ガウス値のとき 関数(OLED)に従って、「N」とガウス値を表示 oled(ns[0],gauss0); } else { // 上記以外のとき 関数(OLED)に従って、「S」とガウス値を表示 oled(ns[1],gauss1*1.2); } delay(500); } ////////関数 テキスト表示の位置・サイズを指定//////// void oled(char ns , long i) { display.setTextSize(5); // テキストサイズを設定 display.setCursor(10, 15); // テキストの開始位置を設定(左から,上から) display.println(ns); //「N」又は「S」を表示 display.display(); // 描画バッファの内容を画面に表示 display.setTextSize(3); // テキストサイズを再度設定 if (i < 10) { // ガウス値が1桁のとき display.setCursor(90, 10); // テキストの開始位置を設定(左から,上から) } else if ( i>= 10 && i<100) { // ガウス値が2桁のとき display.setCursor(73, 10); // テキストの開始位置を設定(左から,上から) } else if ( i>= 100 && i<1000) { // ガウス値が3桁のとき display.setCursor(57, 10); // テキストの開始位置を設定(左から,上から) } else { // ガウス値が4桁以上のとき display.setCursor(40, 10); // テキストの開始位置を設定(左から,上から) } display.print(i); display.setTextSize(2); // テキストサイズを再度設定 display.setCursor(60, 38); // テキストの開始位置を設定(左から,上から) display.println("Gauss"); display.display(); // 描画バッファの内容を画面に表示 }