Last active
December 11, 2015 03:29
-
-
Save nayutaya/4538468 to your computer and use it in GitHub Desktop.
赤外線投光器を制御するArduinoスケッチ。20秒でタイムアウト。
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
const int IRLED_PIN = 13; | |
const unsigned long IRLED_TIMEOUT = 20 * 1000; // ms | |
char buffer[16] = {0}; | |
unsigned char buffer_counter = 0; | |
unsigned long irled_expire = 0; | |
void setup() | |
{ | |
pinMode(IRLED_PIN, OUTPUT); | |
Serial.begin(9600); | |
Serial.println("SETUP"); | |
} | |
void parse_command(String command) | |
{ | |
if ( command.equals("IRLED") ) | |
{ | |
Serial.println(digitalRead(IRLED_PIN) == HIGH ? "IRLED:ON" : "IRLED:OFF"); | |
} | |
else if ( command.equals("IRLED:ON") ) | |
{ | |
irled_expire = millis() + IRLED_TIMEOUT; | |
digitalWrite(IRLED_PIN, HIGH); | |
Serial.println("IRLED:ON"); | |
} | |
else if ( command.equals("IRLED:OFF") ) | |
{ | |
digitalWrite(IRLED_PIN, LOW); | |
Serial.println("IRLED:OFF"); | |
} | |
} | |
void read_serial() | |
{ | |
if ( Serial.available() > 0 ) | |
{ | |
buffer[buffer_counter] = (char)Serial.read(); | |
buffer_counter++; | |
if ( buffer_counter >= sizeof(buffer) ) | |
{ | |
buffer_counter = 0; | |
} | |
if ( buffer[buffer_counter - 1] == '\n' ) | |
{ | |
buffer[buffer_counter - 1] = '\0'; | |
parse_command(String(buffer)); | |
buffer_counter = 0; | |
} | |
} | |
} | |
void irled_timeout() | |
{ | |
if ( millis() > irled_expire && digitalRead(IRLED_PIN) == HIGH ) | |
{ | |
digitalWrite(IRLED_PIN, LOW); | |
Serial.println("IRLED:OFF"); | |
} | |
} | |
void loop() | |
{ | |
read_serial(); | |
irled_timeout(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment