 String light_state;
 
 void serialEvent();
-void dim(unsigned long, boolean);
+boolean dim(unsigned long, boolean);
+void alarm(unsigned long);
+void blink(unsigned long);
 
-void dim(unsigned long duration, boolean brighten) {
+void alarm(unsigned long duration) {
+  unsigned long startMillis = millis();
+  unsigned long dimDuration = 2ul * duration / 3ul;
+  if (dim(dimDuration, true)) {
+    light_state = "alarm";
+    while (millis() - startMillis < duration) {
+      loop();
+      if (light_state != "alarm") {
+        return;
+      }
+    }
+    blink(500);
+  }
+}
+
+void blink(unsigned long interval) {
+  unsigned int previousMillis;
+  light_state = "blink";
+  while(light_state == "blink") {
+    digitalWrite(gate_pin, LOW);
+    previousMillis = millis();
+    while(millis() - previousMillis < interval) {
+      loop();
+    }
+    if (light_state != "blink") break;
+    digitalWrite(gate_pin, HIGH);
+    previousMillis = millis();
+    while(millis() - previousMillis < interval) {
+      loop();
+    }
+  }
+}
+
+// Returns false if was interrupted, otherwise returns true
+boolean dim(unsigned long duration, boolean brighten) {
   double lnb = log(pow(256.0, 1.0 / (float) duration));
   for (int i = 1; i < 256; i++) {
     if (light_state != "dimming") {
-      return;
+      return false;
     }
     unsigned long interval;
     if (brighten) {

  } else {
    light_state = "off";
  }
}

void serialEvent() {
  String serial_data = Serial.readString();
  Serial.flush();
  if (serial_data == "i") {
    Serial.println(light_state);
  } else if (light_state != "on" && serial_data == "o") {
    light_state = "dimming";

    Serial.println(light_state);
  } else if (serial_data == "a" && light_state == "off") {
    light_state = "dimming";
    //alarm(1800000);
    alarm(1800000);
  }
}

