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

55 lines
875 B
C++

const unsigned int LED_PIN = 13;
const unsigned int BAUD_RATE = 9600;
void ledOn() {
digitalWrite(LED_PIN, HIGH);
Serial.println("LED on");
}
void ledOff() {
digitalWrite(LED_PIN, LOW);
Serial.println("LED off");
}
void ledBlink() {
Serial.println("BLINKY TIME");
for (int i = 0; i < 5; i++) {
ledOn();
delay(100);
ledOff();
delay(100);
}
}
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();
}