Created
December 14, 2013 01:16
-
-
Save vermiculus/7954416 to your computer and use it in GitHub Desktop.
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
/* | |
Blinkier | |
Based on `Blink' | |
Toggles the state for two LEDs. Also practice for the Garduino as | |
a fully-realized state machine. | |
This code is in the public domain. | |
*/ | |
const int ERROR_INVALID_STATE = 1; | |
const int PIN_GREEN_LED = 12; | |
const int PIN_RED_LED = 11; | |
const int STATE_A = 0; | |
const int STATE_B = 1; | |
void throw_exception(int); | |
void exec_state(int); | |
byte current_state; | |
long previous_time; | |
int interval; | |
void setup() { | |
pinMode(PIN_GREEN_LED, OUTPUT); | |
pinMode(PIN_RED_LED, OUTPUT); | |
current_state = STATE_A; | |
interval = 500; | |
} | |
void loop() { | |
long current_time = millis(); | |
if (current_time - previous_time > interval) { | |
previous_time = current_time; | |
switch (current_state) { | |
case STATE_A: | |
current_state = STATE_B; | |
break; | |
case STATE_B: | |
current_state = STATE_A; | |
break; | |
default: | |
throw_exception(ERROR_INVALID_STATE); | |
} | |
exec_state(current_state); | |
} | |
} | |
void exec_state(byte state) { | |
switch (state) { | |
case STATE_A: | |
digitalWrite(PIN_GREEN_LED, HIGH); | |
digitalWrite(PIN_RED_LED, LOW); | |
break; | |
case STATE_B: | |
digitalWrite(PIN_GREEN_LED, LOW); | |
digitalWrite(PIN_RED_LED, HIGH); | |
break; | |
default: | |
throw_exception(ERROR_INVALID_STATE); | |
} | |
} | |
void throw_exception(int error) { | |
if (error) { | |
switch (error) { | |
case ERROR_INVALID_STATE: | |
Serial.print("Invalid State! :: "); | |
Serial.println(current_state); | |
} | |
} | |
} | |
/* Local Variables: */ | |
/* mode: c */ | |
/* truncate-lines: t */ | |
/* End: */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment