 
 void loop()
 {
+  // Hvis GZLL har kjrt lengre enn timeout, avslutt GZLL-stacken.
+  if(gzllLastCommandTime != 0 && (millis() - gzllLastCommandTime) > gzllTimeout){
+    stopGzll();
+  }
+  
   tick();
+  
   if (shouldSleep) {
-      delay(10); // Ekstra delay for aa forsikre seg at data blir sendt.
-      RFduino_ULPDelay(INFINITE);
-      shouldSleep = false;
+      // Hvis GZLL-stacken er paa, sov i 1 sek og sjekk s p nytt om stacken kan slaas av.
+      if(gzllRunning) {
+        RFduino_ULPDelay(1000);
+      }
+      else {
+        RFduino_ULPDelay(INFINITE);
+        shouldSleep = false;
+      }
   }
+  
   delay(10); // Kan ikke kalle tick for ofte... 
 }
 

  buttonB.attachDoubleClick(onDoubleClickB);
  buttonA.attachLongPressStop(onLongPressA);
  buttonB.attachLongPressStop(onLongPressB);

  // start the GZLL stack
  RFduinoGZLL.begin(role);

  resetAndSleep();
}


void sendToHost(int command)
{
  startGzll();
  RFduinoGZLL.sendToHost(command);
  // Serial.println("sent " + String(command));
  gzllLastCommandTime = millis();
  delay(10);
}

void startGzll()
{
  if(!gzllRunning){
    gzllRunning = true;
    RFduinoGZLL.begin(role);
  }
}

void stopGzll()
{
  if(gzllRunning){
    RFduinoGZLL.end();
    gzllRunning = false;  
  }
}

void tick()
