Skip to content

Instantly share code, notes, and snippets.

@snt
Created August 30, 2014 02:41
Show Gist options
  • Save snt/7ff8ff84832f71251ef6 to your computer and use it in GitHub Desktop.
Save snt/7ff8ff84832f71251ef6 to your computer and use it in GitHub Desktop.
http://www.atmarkit.co.jp/ait/articles/1408/27/news036_2.html を試したけれど、ループ内で 毎回 `loop()` を呼び出しているのがオーバーヘッドになっているのか、長さが正しく記録できてなかったので、 `loop()` 内でループするように変更。
#define IR_RECV_PIN 2
#define IR_TIMEOUT_USEC 1000000
void setup()
{
Serial.begin(115200);
pinMode(IR_RECV_PIN, INPUT);
}
void loop()
{
int previousBit = digitalRead(IR_RECV_PIN);
Serial.println("ready");
while(previousBit == digitalRead(IR_RECV_PIN)) {}
Serial.println(">>>");
unsigned long initTime = micros();
unsigned long previousTimeUSec = micros();
previousBit = digitalRead(IR_RECV_PIN);
while (micros() - previousTimeUSec < IR_TIMEOUT_USEC) {
int currentBit = digitalRead(IR_RECV_PIN);
if (previousBit == currentBit) continue;
unsigned long now = micros();
unsigned long dur = now - previousTimeUSec;
Serial.print(dur);
Serial.print(", ");
previousBit = currentBit;
previousTimeUSec = now;
}
Serial.println("\n<<<");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment