   }
   /*alarm screen*/
   if (currentMode == ALARMSCREEN){
+    // Prevent double button presses between cancel and alarm settings
     SettingsScreenInit();
+    int tempECGMaxbias = biasECGMax;
+    int tempECGMinbias = biasECGMin;
+    int delay_touch_detection = 10000;
+    for (int i = 0; i < delay_touch_detection; i += 1) {
+        if (!ts.bufferEmpty()){
+            volatile TS_Point tossout = getFixedCoordinates();
+        }
+    }
     //throwAlarm();
     while(currentMode == ALARMSCREEN){
       TS_Point p = getFixedCoordinates();
+      int total_ecg_max = DEFAULT_ECG_MAX + tempECGMaxbias;
+      int total_ecg_min = DEFAULT_ECG_MIN + tempECGMinbias;
       if(ECGPlus.isTapped(p.x,p.y)){
-        biasECGMin += 1;
+        // Skip if you'll exceed the max value
+        if (total_ecg_min + 1 > total_ecg_max) { continue; }
+        tempECGMinbias += 1;
         /*for two digit numbers*/
-        if (DEFAULT_ECG_MIN + biasECGMin < 100){
+        if (DEFAULT_ECG_MIN + tempECGMinbias < 100){
           tft.fillRect(52, 50, 45, 25, ILI9341_LIGHTGREY);
           tft.setCursor(63, 55);
           tft.setTextSize(2);
           tft.setTextColor(ILI9341_BLACK);
-          tft.printf("%d", DEFAULT_ECG_MIN + biasECGMin);
+          tft.printf("%d", DEFAULT_ECG_MIN + tempECGMinbias);
           }
-        else if (DEFAULT_ECG_MIN + biasECGMin > 99){
+        else if (DEFAULT_ECG_MIN + tempECGMinbias > 99){
           /*for three digit numbers*/
           tft.fillRect(52, 50, 45, 25, ILI9341_LIGHTGREY);
           tft.setCursor(56, 55);
           tft.setTextSize(2);
           tft.setTextColor(ILI9341_BLACK);
-          tft.printf("%d", DEFAULT_ECG_MIN + biasECGMin);
+          tft.printf("%d", DEFAULT_ECG_MIN + tempECGMinbias);
         }
       }
       if(ECGMinus.isTapped(p.x,p.y)){
-        biasECGMin -= 1;
+        // Don't let the minimum go negative
+        if (total_ecg_min - 1 < 0) { continue; }
+        tempECGMinbias -= 1;
         /*for two digit numbers*/
-        if (DEFAULT_ECG_MIN + biasECGMin < 100){
+        if (DEFAULT_ECG_MIN + tempECGMinbias < 100){
           tft.fillRect(52, 50, 45, 25, ILI9341_LIGHTGREY);
           tft.setCursor(63, 55);
           tft.setTextSize(2);
           tft.setTextColor(ILI9341_BLACK);
-          tft.printf("%d", DEFAULT_ECG_MIN + biasECGMin);
+          tft.printf("%d", DEFAULT_ECG_MIN + tempECGMinbias);
           }
-        else if (DEFAULT_ECG_MIN + biasECGMin > 99){
+        else if (DEFAULT_ECG_MIN + tempECGMinbias > 99){
           /*for three digit numbers*/
           tft.fillRect(52, 50, 45, 25, ILI9341_LIGHTGREY);
           tft.setCursor(56, 55);
           tft.setTextSize(2);
           tft.setTextColor(ILI9341_BLACK);
-          tft.printf("%d", DEFAULT_ECG_MIN + biasECGMin);
-        }
-      }
-
-        if(confirm_button.isTapped(p.x,p.y)){
-            currentMode = HOMESCREEN;
-        }
+          tft.printf("%d", DEFAULT_ECG_MIN + tempECGMinbias);
+        }
+      }
+
+      if(ECGPlus1.isTapped(p.x,p.y)){
+        // Set reasonable upper limit on heartrate
+        if (total_ecg_max + 1 > 300) { continue; }
+        tempECGMaxbias += 1;
+        /*for two digit numbers*/
+        if (DEFAULT_ECG_MAX + tempECGMaxbias < 100){
+          tft.fillRect(52, 100, 45, 25, ILI9341_LIGHTGREY);
+          tft.setCursor(63, 105);
+          tft.setTextSize(2);
+          tft.setTextColor(ILI9341_BLACK);
+          tft.printf("%d", DEFAULT_ECG_MAX + tempECGMaxbias);
+          }
+        else if (DEFAULT_ECG_MAX + tempECGMaxbias > 99){
+          /*for three digit numbers*/
+          tft.fillRect(52, 100, 45, 25, ILI9341_LIGHTGREY);
+          tft.setCursor(56, 105);
+          tft.setTextSize(2);
+          tft.setTextColor(ILI9341_BLACK);
+          tft.printf("%d", DEFAULT_ECG_MAX + tempECGMaxbias);
+        }
+      }
+      if(ECGMinus1.isTapped(p.x,p.y)){
+        // Don't let the max go any lower than the min
+        if (total_ecg_max - 1 < total_ecg_min) { continue; }
+        tempECGMaxbias -= 1;
+        /*for two digit numbers*/
+        if (DEFAULT_ECG_MAX + tempECGMaxbias < 100){
+          tft.fillRect(52, 100, 45, 25, ILI9341_LIGHTGREY);
+          tft.setCursor(63, 105);
+          tft.setTextSize(2);
+          tft.setTextColor(ILI9341_BLACK);
+          tft.printf("%d", DEFAULT_ECG_MAX + tempECGMaxbias);
+          }
+        else if (DEFAULT_ECG_MAX + tempECGMaxbias > 99){
+          /*for three digit numbers*/
+          tft.fillRect(52, 100, 45, 25, ILI9341_LIGHTGREY);
+          tft.setCursor(56, 105);
+          tft.setTextSize(2);
+          tft.setTextColor(ILI9341_BLACK);
+          tft.printf("%d", DEFAULT_ECG_MAX + tempECGMaxbias);
+        }
+      }
+   
+      // Reset alarm parameters to the default values 
+      if (default_button.isTapped(p.x,p.y)) {
+        tempECGMaxbias = 0;
+          tft.fillRect(52, 100, 45, 25, ILI9341_LIGHTGREY);
+          tft.setCursor(63, 105);
+          tft.setTextSize(2);
+          tft.setTextColor(ILI9341_BLACK);
+          tft.printf("%d", DEFAULT_ECG_MAX + tempECGMaxbias);
+        tempECGMinbias = 0;
+          tft.fillRect(52, 50, 45, 25, ILI9341_LIGHTGREY);
+          tft.setCursor(63, 55);
+          tft.setTextSize(2);
+          tft.setTextColor(ILI9341_BLACK);
+          tft.printf("%d", DEFAULT_ECG_MIN + tempECGMinbias);
+      }
+
+      if(confirm_button.isTapped(p.x,p.y)){
+        biasECGMax = tempECGMaxbias;
+        biasECGMin = tempECGMinbias;
+        currentMode = HOMESCREEN;
+      }
+
+      if (cancel_button.isTapped(p.x,p.y)) {
+        currentMode = HOMESCREEN;
+      }
     }
   }  
 }


void cancel_button_setup(void){
  cancel_button.draw();
  createHLabel(BOXSIZE*3 + 14, 217, cancelLabel, 1, ILI9341_BLACK);
}

void ECGSettings_setup(void){
  /* MINIMUM VALUE */
  tft.fillRect(52, 50, 45, 25, ILI9341_LIGHTGREY);
  tft.setCursor(63, 55);
  tft.setTextSize(2);    
  tft.setTextColor(ILI9341_BLACK);
  tft.printf("%d", DEFAULT_ECG_MIN);

  if (currentMode == HOMESCREEN){
    MainScreenInit();
    stopAlarm();
    int delay_touch_detection = 10000;
    for (int i = 0; i < delay_touch_detection; i += 1) {
        if (!ts.bufferEmpty()){
            volatile TS_Point tossout = getFixedCoordinates();
        }
    }
    while (currentMode == HOMESCREEN){
  		if (display_count >= 10) {
    		ecg.display_signal();
