/*
FLASHER - PINOUT
Flasher 12
Green Indicator 5
Red Indicator 3
*/
#define FLASHER_PIN 12 // the pin that the flasher mosfet gate is attached to
#define ALERT_GRN_PIN 5
#define ALERT_RED_PIN 3
#define NUMBER_OF_FIELDS = 2
#define BUFFER_MAX 255
#define MODE_LOADING_BUFFER 0
#define MODE_BUFFER_READY 1
boolean isOnAlert = false;
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
int fieldIndex = 0; // The field we are currently reading into.
int bufferIndex = 0;
char buffer[BUFFER_MAX];
char mode = MODE_LOADING_BUFFER;
void readData();
void alert();
void clear_alert();
void selfTest();
void setup() {
// reserve 200 bytes for the inputString:
inputString.reserve(200);
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(FLASHER_PIN, OUTPUT);
pinMode(ALERT_GRN_PIN, OUTPUT);
pinMode(ALERT_RED_PIN, OUTPUT);
selfTest();
clear_alert();
}
void loop() {
if (stringComplete) {
Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
}
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
if(inputString.length() <= 0){
if(inChar == '!'){
alert();
Serial.println("ALERT!");
continue;
} else if(inChar == '?'){
clear_alert();
Serial.println("All Clear");
continue;
}
}
Serial.println(inputString.length());
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
void alert(){
digitalWrite(FLASHER_PIN, HIGH);
isOnAlert = true;
digitalWrite(ALERT_GRN_PIN, HIGH);
digitalWrite(ALERT_RED_PIN, LOW);
}
void clear_alert(){
digitalWrite(FLASHER_PIN, LOW);
isOnAlert = false;
digitalWrite(ALERT_GRN_PIN, LOW);
digitalWrite(ALERT_RED_PIN, HIGH);
}
void readData() {
while(Serial.available()) {
char ch = Serial.read();
if(bufferIndex <= 0) {
if(ch == '!'){
alert();
Serial.println("ALERT!");
} else {
clear_alert();
Serial.println("All Clear");
}
bufferIndex++;
} else if(ch == '\0' || ch == '\n' || ch == '\r' || bufferIndex >= BUFFER_MAX -1){
buffer[++bufferIndex] = '\0';
bufferIndex = 0;
mode = MODE_BUFFER_READY;
} else {
mode = MODE_LOADING_BUFFER;
buffer[bufferIndex] = ch;
bufferIndex++;
}
delay(10);
}
}
void selfTest() {
digitalWrite(FLASHER_PIN, HIGH);
delay(1500);
digitalWrite(FLASHER_PIN, LOW);
boolean flip = false;
for(int i = 0; i < 20; i++) {
digitalWrite(ALERT_GRN_PIN, flip ? HIGH : LOW);
digitalWrite(ALERT_RED_PIN, flip ? LOW : HIGH);
flip = !flip;
delay(75);
}
for(int i=0; i < 5; i++){
digitalWrite(ALERT_GRN_PIN, LOW);
digitalWrite(ALERT_RED_PIN, LOW);
delay(250);
digitalWrite(ALERT_GRN_PIN, HIGH);
digitalWrite(ALERT_RED_PIN, HIGH);
delay(250);
}
}