diff --git a/PhotoReflectorIF/PhotoReflectorIF.ino b/PhotoReflectorIF/PhotoReflectorIF.ino index 120f79b..5be4909 100644 --- a/PhotoReflectorIF/PhotoReflectorIF.ino +++ b/PhotoReflectorIF/PhotoReflectorIF.ino @@ -1,40 +1,79 @@ // フォトリフレクタ入力インターフェース -// 製作者:中口 製作日:2022/6/8 +// 製作者:中口 製作日:2022/7/24 // (Arduino)Seeeduino XIAO M0+ https://www.switch-science.com/catalog/6335/ -// (Sensor)TPR-105F https://akizukidenshi.com/download/tpr-105f.pdf -// フォトリフレクタ検出値(0-1023)をシリアルポートへ出力 +// (Sensor)S11059 https://akizukidenshi.com/download/ds/akizuki/S11059_module_manual.pdf +// S11059接続 GND 黒, SCL 白, SDA 灰, +V 紫 +// カラーセンサ値(B, R, G, IR)とスイッチ入力(0/1)をシリアルポートへ出力 -// グローバル変数 -const int BAUD_RATE = 115200; // シリアル通信ボーレート -const int ANALOG_PIN = A7; // フォトセンサ入力ピン -const int SW_PIN = 6; // 開始スイッチ入力ピン -const int SPEAKER_PIN = 4; // スピーカー出力 -const int LOOP_WAIT = 30; // ループ毎のウェイト(ms) -const unsigned long BUZZER_LENGTH = 500; // 開始ブザー音の長さ(ms) -unsigned long gBuzzerEnd = 0; +#include + +//アドレス指定 +#define S11059_ADDR 0x2A +#define CONTROL_MSB 0x00 +#define CONTROL_1_LSB 0x89 // 固定時間モード,Highゲイン,Tint=01(1.4ms),積分時間1.4ms/ch +#define CONTROL_2_LSB 0x09 // 固定時間モード,Highゲイン,Tint=01(1.4ms),積分時間1.4ms/ch +#define SENSOR_REGISTER 0x03 +#define SW_PIN 7 // スイッチ入力ピン // 初期化 -void setup() { +void setup() +{ + Serial.begin(115200);//シリアル通信を115200bpsで初期化 + Wire.begin();//I2Cを初期化 + pinMode(SW_PIN, INPUT_PULLUP); // デジタル入力初期化 + + Wire.beginTransmission(S11059_ADDR);//I2Cスレーブ データ送信開始 + Wire.write(CONTROL_MSB);//コントロールバイトを指定 + Wire.write(CONTROL_1_LSB);//ADCリセット、スリープ解除 + Wire.endTransmission();//I2Cスレーブ データ送信終了 - Serial.begin(BAUD_RATE); - pinMode(SW_PIN, INPUT_PULLUP); + Wire.beginTransmission(S11059_ADDR);//I2Cスレーブ データ送信開始 + Wire.write(CONTROL_MSB);//コントロールバイトを指定 + Wire.write(CONTROL_2_LSB);//ADCリセット解除、バスリリース + Wire.endTransmission();//I2Cスレーブ データ送信終了 } - -// メインループ +// ループ void loop() { - int val = analogRead(ANALOG_PIN); - int sw = digitalRead(SW_PIN); - Serial.print(sw); - Serial.print(","); - Serial.println(val); - if (sw == LOW && gBuzzerEnd == 0) { - gBuzzerEnd = millis() + BUZZER_LENGTH; - analogWrite(SPEAKER_PIN, 128); + //変数宣言 + int high_byte, low_byte, red, green, blue, IR, sw; + + delay(50);//50msec待機 + + Wire.beginTransmission(S11059_ADDR);//I2Cスレーブ データ送信開始 + Wire.write(SENSOR_REGISTER);//出力データバイトを指定 + Wire.endTransmission();//I2Cスレーブ データ送信終了 + + Wire.requestFrom(S11059_ADDR, 8);//I2Cデバイス「S11059_ADDR」に8Byteのデータ要求 + if(Wire.available()){ + high_byte = Wire.read();//high_byteに「赤(上位バイト)」のデータ読み込み + low_byte = Wire.read();//high_byteに「赤(下位バイト)」のデータ読み込み + red = high_byte << 8|low_byte;//1Byte目のデータを8bit左にシフト、OR演算子で2Byte目のデータを結合して、redに代入 + + high_byte = Wire.read();//high_byteに「緑(上位バイト)」のデータ読み込み + low_byte = Wire.read();//high_byteに「緑(下位バイト)」のデータ読み込み + green = high_byte << 8|low_byte;//1Byte目のデータを8bit左にシフト、OR演算子で2Byte目のデータを結合して、greenに代入 + + high_byte = Wire.read();//high_byteに「青(上位バイト)」のデータ読み込み + low_byte = Wire.read();//high_byteに「青(下位バイト)」のデータ読み込み + blue = high_byte << 8|low_byte;//1Byte目のデータを8bit左にシフト、OR演算子で2Byte目のデータを結合して、blueに代入 + + high_byte = Wire.read();//high_byteに「赤外(上位バイト)」のデータ読み込み + low_byte = Wire.read();//high_byteに「赤外(下位バイト)」のデータ読み込み + IR = high_byte << 8|low_byte;//1Byte目のデータを8bit左にシフト、OR演算子で2Byte目のデータを結合して、IRに代入 } - if (gBuzzerEnd > 0 && millis() >= gBuzzerEnd) { - gBuzzerEnd = 0; - analogWrite(SPEAKER_PIN, 0); - } - delay(LOOP_WAIT); + Wire.endTransmission();//I2Cスレーブ「Arduino Uno」のデータ送信終了 + + sw = digitalRead(SW_PIN); + + Serial.print(blue);//「blue」をシリアルモニタに送信 + Serial.print(",");//文字列「,」をシリアルモニタに送信 + Serial.print(red);//「red」をシリアルモニタに送信 + Serial.print(",");//文字列「,」をシリアルモニタに送信 + Serial.print(green);//「green」をシリアルモニタに送信 + Serial.print(",");//文字列「,」をシリアルモニタに送信 + Serial.print(IR);//「IR」をシリアルモニタに送信 + Serial.print(",");//文字列「,」をシリアルモニタに送信 + Serial.print(sw);//「Switch」をシリアルモニタに送信 + Serial.println("");//改行 }