Newer
Older
ISCamRecorder / PhotoReflectorIF / PhotoReflectorIF.ino
// フォトリフレクタ入力インターフェース
// 製作者:中口 製作日:2022/6/8
// (Arduino)Seeeduino XIAO M0+ https://www.switch-science.com/catalog/6335/
// (Sensor)TPR-105F https://akizukidenshi.com/download/tpr-105f.pdf
// フォトリフレクタ検出値(0-1023)をシリアルポートへ出力

// グローバル変数
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 = 2000; // 開始ブザー音の長さ(ms)
unsigned long gBuzzerEnd = 0;

// 初期化
void setup() {
  
  Serial.begin(BAUD_RATE);
  pinMode(SW_PIN, INPUT_PULLUP);
}


// メインループ
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);
  }
  if (gBuzzerEnd > 0 && millis() >= gBuzzerEnd) {
    gBuzzerEnd = 0;
    analogWrite(SPEAKER_PIN, 0);    
  }
  delay(LOOP_WAIT);
}