box-o-sand/oldstuff/arduino/msaqsg/ch02/LEDSwitch/LEDSwitch.ino

55 lines
875 B
Arduino
Raw Normal View History

2013-12-29 20:12:21 +00:00
const unsigned int LED_PIN = 13;
const unsigned int BAUD_RATE = 9600;
2013-12-29 20:24:31 +00:00
void ledOn() {
digitalWrite(LED_PIN, HIGH);
Serial.println("LED on");
2013-12-29 20:24:31 +00:00
}
void ledOff() {
digitalWrite(LED_PIN, LOW);
Serial.println("LED off");
2013-12-29 20:24:31 +00:00
}
void ledBlink() {
Serial.println("BLINKY TIME");
for (int i = 0; i < 5; i++) {
ledOn();
delay(100);
ledOff();
delay(100);
}
2013-12-29 20:24:31 +00:00
}
void handleInput() {
int c = Serial.read();
switch (c) {
case '1':
ledOn();
break;
case '2':
ledOff();
break;
case '3':
ledBlink();
break;
default:
Serial.print("Unknown command: ");
Serial.println(c);
}
}
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(BAUD_RATE);
}
void loop() {
if (Serial.available() <= 0) {
return;
}
handleInput();
2013-12-29 20:12:21 +00:00
}