Created
October 19, 2014 13:26
-
-
Save sp-ice/ad86dcf66ea15b2153ef to your computer and use it in GitHub Desktop.
Arduinoでリモコン信号取得
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define READ_PIN 7 | |
#define LOW_STATE 0 | |
#define HIGH_STATE 1 | |
void setup(){ | |
Serial.begin(57600); | |
pinMode(READ_PIN,INPUT); | |
Serial.println("Ready to receive"); | |
} | |
void waitLow() { | |
while (digitalRead(READ_PIN)==LOW) { | |
; | |
} | |
} | |
int waitHigh() { | |
unsigned long start = micros(); | |
while (digitalRead(READ_PIN)==HIGH) { | |
if (micros() - start > 5000000) { | |
return 1; | |
} | |
} | |
return 0; | |
} | |
unsigned long now = micros(); | |
unsigned long lastStateChangedMicros = micros(); | |
int state = HIGH_STATE; | |
void loop() { | |
if (state == LOW_STATE) { | |
waitLow(); | |
} else { | |
int ret = waitHigh(); | |
if (ret == 1) { | |
Serial.print("\n"); | |
return; | |
} | |
} | |
now = micros(); | |
Serial.print((now - lastStateChangedMicros) / 10, DEC); | |
Serial.print(","); | |
lastStateChangedMicros = now; | |
if (state == HIGH_STATE) { | |
state = LOW_STATE; | |
} else { | |
state = HIGH_STATE; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment