     buttons_list[1].last_button_state = buttons_list[1].button_state;
 }
 
+int check_button_pressed_and_sendCode(){
+    int button_index = what_button_index_was_pressed();
+    if (button_index != -1)
+    {
+        last_pressed_button = button_index; //marks the correct button_index as the last pressed
+        Serial.print("Pressed Button ");
+        Serial.print(last_pressed_button);
+        Serial.println(", sending");
+
+        digitalWrite(STATUS_PIN, HIGH);
+
+        int is_repeating = buttons_list[button_index].last_button_state == buttons_list[button_index].button_state;
+        sendCode(is_repeating, last_pressed_button);
+
+        digitalWrite(STATUS_PIN, LOW);
+
+        //return true
+        return 1;
+    }
+    //return false
+    return 0;
+}
+
 void loop()
 {
+    update_state_of_buttons();
+
+    re_enable_IRIn_if_button_released();
+
     // If button pressed, send the code.
-    update_state_of_buttons();
-    //========================================================
-
-    // int secondButtonState = digitalRead(SECOND_BUTTON_PIN);
-    // int thirdButtonState = digitalRead(THIRD_BUTTON_PIN);
-    //========================================================
-
-    re_enable_IRIn_if_button_released();
-
-    if (check_button_pressed(0))
-    {
-        last_pressed_button = 0; //marca o primeiro botao como sendo o pressionado
-        Serial.println("Pressed, sending");
-        digitalWrite(STATUS_PIN, HIGH);
-        sendCode(buttons_list[0].last_button_state == buttons_list[0].button_state, last_pressed_button);
-        digitalWrite(STATUS_PIN, LOW);
+    if (check_button_pressed_and_sendCode())
+    {
         delay(50); // Wait a bit between retransmissions
     }
     else if (irrecv.decode(&results))
     {
-        printDadosBotao(0);
+        printDadosBotao(last_pressed_button);
         digitalWrite(STATUS_PIN, HIGH);
         storeCode(&results, last_pressed_button);
         irrecv.resume(); // resume receiver

    return buttons_list[button_index].button_state;
}


void re_enable_IRIn_if_button_released(){

    if (check_button_released(0) | check_button_released(1))
    {
        Serial.println("Released a button");
        irrecv.enableIRIn(); // Re-enable receiver
    }
    return;

    Serial.println(buttons_list[pressed_button].codeLen);

}

int check_button_released(int button_index){
    return buttons_list[button_index].last_button_state == HIGH && buttons_list[button_index].button_state == LOW;
