 const int gate_pin = 5;
 String light_state;
+int light_brightness;
 
 void serialEvent();
-boolean dim(unsigned long, boolean);
+boolean dim(long);
 void alarm(unsigned long);
 void blink(unsigned long);
 
 void alarm(unsigned long duration) {
   unsigned long startMillis = millis();
   unsigned long dimDuration = 2ul * duration / 3ul;
-  if (dim(dimDuration, true)) {
+  if (dim(dimDuration)) {
     light_state = "alarm";
     while (millis() - startMillis < duration) {
       loop();

  } else {
    light_state = "off";
  }
  return true;
}

void serialEvent() {
  String serial_data = Serial.readString();
  Serial.flush();
  if (serial_data == "i") {
    Serial.println(light_state);
  } else if (light_state == "alarm" && serial_data == "o") {
    light_state = "on";
    Serial.println(light_state);
  } else if (light_state != "on" && serial_data == "o") {
    light_state = "dimming";
    dim(500, true);
    Serial.println(light_state);
  } else if (serial_data == "o") {
    light_state = "dimming";
    dim(500, false);
    Serial.println(light_state);
  } else if (serial_data == "a" && light_state == "off") {
    light_state = "dimming";
    //alarm(1800000);
    alarm(1800000);
  }
}


  }
}

boolean dim(long duration) {
  boolean brighten;
  light_state = "dimming";
  if (duration > 0) {
    brighten = true;
  } else {
    brighten = false;
    duration = -(duration);
  }
  for (double i = 1; i < 256; i++) {
    if (light_state != "dimming") {
      return false;
    }
    unsigned long interval;
    if (brighten) {
      analogWrite(gate_pin, i);
      interval = (unsigned long) round(duration * (sqrt(i / 254) - sqrt((i - 1) / 254)));
    } else {
      double x = 256 - i;
      analogWrite(gate_pin, (int) x - 1);
      interval = (unsigned long) round(duration * (sqrt(x / 254) - sqrt((x - 1) / 254)));
    }
    unsigned long previousMillis = millis();
    if (i < 255) {
