diff --git a/PhotoReflectorIF/PhotoReflectorIF.ino b/PhotoReflectorIF/PhotoReflectorIF.ino index 320ae3a..0ab907b 100644 --- a/PhotoReflectorIF/PhotoReflectorIF.ino +++ b/PhotoReflectorIF/PhotoReflectorIF.ino @@ -5,18 +5,36 @@ // フォトリフレクタ検出値(0-1023)をシリアルポートへ出力 // グローバル変数 -const int BAUD_RATE = 9600; // シリアル通信ボーレート -const int ANALOG_PIN = A7; // 入力ピン +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); }