Index: lab3/Makefile
===================================================================
--- lab3/Makefile	(revision 11)
+++ lab3/Makefile	(revision 12)
@@ -2,7 +2,7 @@
 PROG=lab3 
 
 # source code files to be compiled
-SRCS = lab3.c keyboard.c
+SRCS = lab3.c keyboard.c timer.c
 
 # additional compilation flags
 # "-Wall -Wextra -Werror -I . -std=c11 -Wno-unused-parameter" are already set
Index: lab3/keyboard.c
===================================================================
--- lab3/keyboard.c	(revision 11)
+++ lab3/keyboard.c	(revision 12)
@@ -24,12 +24,17 @@
   uint8_t status_reg;
 
   if(util_sys_inb(STATUS_REG,&status_reg)!=0){ //checks if there is an error
+    printf("Error reading status register in the keyboard interruption\n");
     return;
   }
   if(((status_reg & STAT_REG_OBF)==0) ||((status_reg&(STAT_REG_PAR|STAT_REG_TIMEOUT))!=0)|| ((status_reg&STAT_REG_AUX)!=0)){ //checks if there is a parity or timeout error (mask -> 0xC0, bit 7 and 6 set), checks if output buffer is empty and checks if there is data coming from the mouse
+    printf("Parity/Timeout error or output buffer is empty or data coming from the mouse\n");
     return;
   }
-  util_sys_inb(OUTPUT_BUF,&scan_code);
+  if(util_sys_inb(OUTPUT_BUF,&scan_code)!=0){//checks if there is an error
+    printf("Error reading output buffer in the keyboard interruption\n");
+    return;
+  }
 
 }
 
@@ -36,7 +41,7 @@
 
 int (kbc_subscribe_int)(uint8_t *bit_no) {    //similar function to that of timer_subscribe_int
     *bit_no = BIT(keyboard_id);
-    if(sys_irqsetpolicy(KEYBOARD_IRQ,(IRQ_REENABLE|IRQ_EXCLUSIVE),&keyboard_id)==1){      //operation to subscribe int
+    if(sys_irqsetpolicy(KEYBOARD_IRQ,(IRQ_REENABLE|IRQ_EXCLUSIVE),&keyboard_id)==1){  //operation to subscribe int
         printf("Error subscribing int\n");
         return 1;
     }
@@ -51,7 +56,7 @@
   return 0;
 }
 
-int (enable_interrupt)()
+int (reenable_interrupt)()
 {
   uint8_t command;
 
@@ -61,7 +66,7 @@
   }
 
   if(util_sys_inb(OUTPUT_BUF, &command)!=0){
-    printf("Error reading output buffer\n");
+    printf("Error reading output buffer in enable_interrupt\n");
     return 1;
   }
 
Index: lab3/keyboard.h
===================================================================
--- lab3/keyboard.h	(revision 11)
+++ lab3/keyboard.h	(revision 12)
@@ -48,6 +48,6 @@
  *
  * @return Return 0 upon success and non-zero otherwise
  */
-int (enable_interrupt)();
+int (reenable_interrupt)();
 
 #endif /*_KEYBOARD_H */
Index: lab3/lab3.c
===================================================================
--- lab3/lab3.c	(revision 11)
+++ lab3/lab3.c	(revision 12)
@@ -3,7 +3,7 @@
 #include <lcom/lab3.h>
 #include <stdbool.h>
 #include <stdint.h>
-
+#include "timer.h"
 #include "keyboard.h"
 
 
@@ -33,27 +33,30 @@
 
 extern uint8_t scan_code;
 extern uint32_t cnt;
+extern int count;
 
 int(kbd_test_scan)() {
+
   int r, ipc_status;
   uint8_t keyboard_irq, size, bytes[2]; //size of scancode can be 2 byte long
   message msg;
   bool make=false; //to check if code is make or break
 
-  if(kbc_subscribe_int(&keyboard_irq)==1){
+  if(kbc_subscribe_int(&keyboard_irq)!=0){    //check if subscription worked
     printf("Error subscribing int\n");
     return 1;
   }
 
-  while(scan_code!=ESC_BREAK){      //looping until scan_code is the breakcode of ESC key
+  while(scan_code!=ESC_BREAK){            //looping until scan_code is the breakcode of ESC key
+
     if ((r = driver_receive(ANY, &msg, &ipc_status))==1){ 
       printf("driver_receive failed with: %d",r);
       continue;
     }
-    if (is_ipc_notify(ipc_status)){                             //received notification
+    if (is_ipc_notify(ipc_status)){                                //received notification
       switch (_ENDPOINT_P(msg.m_source)){
-        case HARDWARE:                                          //hardware interrupt notification
-                if (msg.m_notify.interrupts &keyboard_irq){          // subscribed interrupt
+        case HARDWARE:                                             //hardware interrupt notification
+                if (msg.m_notify.interrupts &keyboard_irq){        // subscribed interrupt
                   
                   kbc_ih();
                   bytes[0]=scan_code;
@@ -67,13 +70,13 @@
                   }
 
                   if (scan_code & MAKE_CODE){	//checks if code is make or break
-                      make = false;	//break code
+                      make = false;	          //is break code
                     }
                   else {
-                    make = true;  //make code
+                    make = true;              //is make code
                   }
 
-                  if(kbd_print_scancode(make, size, bytes)!=0){ //prints the code
+                  if(kbd_print_scancode(make, size, bytes)!=0){         //prints the code
                     printf("Error printing scan code\n");
                     return 1;
                   }
@@ -80,20 +83,20 @@
                       }
                break;
         default:
-              break;                                            //no other notifications expected: do nothing	
+              break;                             //no other notifications expected: do nothing	
         }
     } 
-    else {                                                      //received a standard message, not a notification
+    else {                                       //received a standard message, not a notification
       //no standard messages expected: do nothing
     }
   }
 
-   if (kbc_unsubscribe_int() != 0) { // Check if subscription worked
+   if (kbc_unsubscribe_int() != 0) {            //check if unsubscription worked
 		printf("Error unsubscribing int \n");
 		return 1;
 	}
 
-  if(kbd_print_no_sysinb(cnt)!=0){
+  if(kbd_print_no_sysinb(cnt)!=0){                  //prints the number of sys calls
     printf("Error printing number of sys used\n");
     return 1;
   }
@@ -105,18 +108,20 @@
 }
 
 int(kbd_test_poll)() {
+
   uint8_t status, size, bytes[2]; //size of scancode can be 2 byte long
   bool make=false; //to check if code is make or break
 
-  while(scan_code!=ESC_BREAK){      //looping until scan_code is the breakcode of ESC key, can't do interruptions
+  while(scan_code!=ESC_BREAK){      //looping until scan_code is the breakcode of ESC key, can't do                                     interruptions
+
     if(util_sys_inb(STATUS_REG, &status)!=0){     //checks the status register
       printf("Error reading status register\n");
       return 1;
     }					
 
-		if ((status & STAT_REG_OBF) && !(status & STAT_REG_AUX) && !(status & STAT_REG_PAR || status & STAT_REG_TIMEOUT)) {      //checks if there is no parity ot timeout error, if the output buffer is full and if there is mouse data
+		if ((status & STAT_REG_OBF) && !(status & STAT_REG_AUX) && !(status & STAT_REG_PAR || status & STAT_REG_TIMEOUT)) {      //checks if there is no parity ot timeout error, if the output buffer                             is full and if there is mouse data
 
-			if(util_sys_inb(OUTPUT_BUF, &scan_code)!=0){
+			if(util_sys_inb(OUTPUT_BUF, &scan_code)!=0){  //reads output buffer
         printf("Error reading output buffer\n");
       return 1;
       }
@@ -149,13 +154,13 @@
 		}
   }
 
-  if(kbd_print_no_sysinb(cnt)!=0){
+  if(kbd_print_no_sysinb(cnt)!=0){                      //prints the number of sys calls
     printf("Error printing number of sys used\n");
     return 1;
   }
 
-  if(enable_interrupt()!=0){
-    printf("Coulnd re-enable the interruptions\n");
+  if(reenable_interrupt()!=0){                          //re-enables interruptions
+    printf("Couldn't re-enable the interruptions\n");
     return 1;
 
   }
@@ -165,9 +170,93 @@
    return 0;
 }
 
-int(kbd_test_timed_scan)(uint8_t n) {
-  /* To be completed by the students */
-  printf("%s is not yet implemented!\n", __func__);
 
-  return 1;
+int(kbd_test_timed_scan)(uint8_t n) { //similar function to that of test_time_scan but with timer interruptions
+
+  if(n<0){                    //checks if n is a valid input
+    printf("Invalid time\n");
+    return 1;
+  }
+
+  int r, ipc_status;
+  uint8_t keyboard_irq, size, bytes[2], timer_irq;  //size of scancode can be 2 byte long
+  message msg;
+  bool make=false;                                  //to check if code is make or break
+
+  if(timer_subscribe_int(&timer_irq)!=0){
+    printf("Error subscribing int (timer)\n");
+    return 1;
+  }
+
+  if(kbc_subscribe_int(&keyboard_irq)!=0){
+    printf("Error subscribing int (keyboard)\n");
+    return 1;
+  }
+
+  while((scan_code!=ESC_BREAK)&&(count/sys_hz()<n)){      //looping until scan_code is the breakcode of ESC key or until a command is issued within the time interval
+    if ((r = driver_receive(ANY, &msg, &ipc_status))==1){ 
+      printf("driver_receive failed with: %d",r);
+      continue;
+    }
+    if (is_ipc_notify(ipc_status)){                             //received notification
+      switch (_ENDPOINT_P(msg.m_source)){
+        case HARDWARE:                                          //hardware interrupt notification
+                if (msg.m_notify.interrupts &keyboard_irq){     // subscribed interrupt (keyboard)
+                  
+                  kbc_ih();
+                  bytes[0]=scan_code;
+                  bytes[1]=scan_code;
+
+                  if(bytes[0]==TWO_BYTE_SCANCODE){
+                    size=2;
+                  }
+                  else{
+                    size=1;
+                  }
+
+                  if (scan_code & MAKE_CODE){	//checks if code is make or break
+                      make = false;	//break code
+                    }
+                  else {
+                    make = true;  //make code
+                  }
+
+                  if(kbd_print_scancode(make, size, bytes)!=0){  //prints the code
+                    printf("Error printing scan code\n");
+                    return 1;
+                  }
+                  count=0;                                      //resets counter, since a keyboard                                                  interruption has ocurred
+                }
+                else if (msg.m_notify.interrupts &timer_irq) {  // subscribed interrupt (timer)
+                  timer_int_handler();              //if a timer interruption occurs increment timer
+				        }
+               break;
+        default:
+              break;                                     //no other notifications expected: do nothing	
+        }
+    } 
+    else {                                         //received a standard message, not a notification
+      //no standard messages expected: do nothing
+    }
+  }
+
+   if (kbc_unsubscribe_int() != 0) {        //check if unsubscription worked
+		printf("Error unsubscribing int \n");
+		return 1;
+	}
+
+  if(timer_unsubscribe_int()!=0){           //check if unsubscription worked
+    printf("Error unsubscribing int\n");
+    return 1;
+  }
+
+  if(kbd_print_no_sysinb(cnt)!=0){                  //prints the number of sys calls
+    printf("Error printing number of sys used\n");
+    return 1;
+  }
+
+  cnt=0;
+  scan_code=0;
+
+  return 0;
 }
