   startBtnPrev = LOW;
   stopBtnPrev = 0;
   timerValuePrev = 0;
+  isCloudBitInputHigh = false;
   melodyCount = 0;
   noteIndex = 0;
   nextToneTime = 0;
   nextNoToneTime = 0;
-  noTone(SPEAKER_OUT);
 }
 
 // if start button is pressed, increase timerValue by START_VAL
 void checkStartButton() {
-  boolean startBtn = digitalRead(START_BTN);
+  int startBtn = digitalRead(START_BTN);
   if (startBtn == HIGH && startBtnPrev == LOW) {
     if (isStopped) {
       isStopped = false;
     } else {
-      timerValue = min(timerValue + START_VAL, MAX_VAL);
+      timerValue = min(timerValue + START_TIMER_VAL, MAX_TIMER_VAL);
     }
   }
   startBtnPrev = startBtn;
   displayCounter();
+}
+
+// output to counter 7seg LED
+void displayCounter() {
+  // maps 6000 sec to 100 minutes on littleBits LED
+  analogWrite(COUNTER_LED, map(timerValue, 0, 59, 0, 255));
+//  analogWrite(COUNTER_LED, map(timerValue, 0, 5999, 0, 255));
+}
+
+// output to cloudBit when timer counting is finished
+void checkTimerTimeout() {
+  if (false && timerValue == 0 && timerValuePrev == 1) {
+    analogWrite(CLOUDBIT_OUT, 255);
+    delay(100);
+    analogWrite(CLOUDBIT_OUT, 0);
+    melodyCount = MAX_MELODY_COUNT;
+  }
+}
+
+// check input from cloudBit to play alarm
+void checkCloudBitInput() {
+  int cloudBitInput = analogRead(CLOUDBIT_IN);
+  if (cloudBitInput >= ANALOG_HIGH_THRETHOLD && !isCloudBitInputHigh) {
+    isCloudBitInputHigh = true;
+    melodyCount = MAX_MELODY_COUNT;
+  }
+  if (cloudBitInput < ANALOG_HIGH_THRETHOLD) {
+    isCloudBitInputHigh = false;
+  }
 }
 
 // output click sound to speaker

const int SPEAKER_OUT = 1; // blinker LED and speaker output at D1
const int COUNTER_LED = 5; // counter 7-seg LED output at D5
const int CLOUDBIT_OUT = 9; // cloudBit output at D9

const int START_VAL = 20 * 60; // the unit amount for starting timer in seconds
const int MAX_VAL = 99 * 60; // max amount of timer in seconds

const int MAX_MELODY_COUNT = 1; // how many times melody will be played at alarming

// notes of "winny the pooh"

  ticking();
  checkStartButton();
  checkStopButton();
  checkTimerTimeout();
  checkCloudBitInput();
  playClick();
  playMelody();
}
