From 6b45e9f1675c304ea941294777f4d008f591f411 Mon Sep 17 00:00:00 2001
From: Dabble63 <danny.bud@gmail.com>
Date: Sat, 11 Aug 2012 09:17:47 +0300
Subject: [PATCH] Add Duty Cycling to the Heater Bed

---
 Marlin/Configuration.h |  4 ++++
 Marlin/temperature.cpp | 25 +++++++++++++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/Marlin/Configuration.h b/Marlin/Configuration.h
index 1248029eda..3668db4907 100644
--- a/Marlin/Configuration.h
+++ b/Marlin/Configuration.h
@@ -87,6 +87,10 @@
 #define HEATER_2_MAXTEMP 275
 #define BED_MAXTEMP 150
 
+// If your bed has low resistance e.g. .6 ohm and throws the fuse you can duty cycle it to reduce the
+// average current. The value should be an integer and the heat bed will be turned on for 1 interval of
+// HEATER_BED_DUTY_CYCLE_DIVIDER intervals.
+//#define HEATER_BED_DUTY_CYCLE_DIVIDER 4
 
 // PID settings:
 // Comment the following line to disable PID and enable bang-bang.
diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp
index 395061cc21..7307b24aa8 100644
--- a/Marlin/temperature.cpp
+++ b/Marlin/temperature.cpp
@@ -254,6 +254,11 @@ int getHeaterPower(int heater) {
 
 void manage_heater()
 {
+#ifdef HEATER_BED_DUTY_CYCLE_DIVIDER
+  static int bed_needs_heating=0;
+  static int bed_is_on=0;
+#endif
+
   #ifdef USE_WATCHDOG
     wd_reset();
   #endif
@@ -333,12 +338,26 @@ void manage_heater()
     }
   #endif
   
+#ifdef HEATER_BED_DUTY_CYCLE_DIVIDER
+  if (bed_needs_heating){
+    if (bed_is_on==0)
+        WRITE(HEATER_BED_PIN,HIGH);
+    if (bed_is_on==1)
+        WRITE(HEATER_BED_PIN,LOW);
+    bed_is_on=(bed_is_on+1) % HEATER_BED_DUTY_CYCLE_DIVIDER;
+  }
+#endif
+
   if(millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
     return;
   previous_millis_bed_heater = millis();
   
   #if TEMP_BED_PIN > -1
   
+    #ifdef HEATER_BED_DUTY_CYCLE_DIVIDER
+    bed_needs_heating=0;
+    #endif
+
     #ifndef BED_LIMIT_SWITCHING
       // Check if temperature is within the correct range
       if((current_raw_bed > bed_minttemp) && (current_raw_bed < bed_maxttemp)) {
@@ -348,6 +367,9 @@ void manage_heater()
         }
         else 
         {
+          #ifdef HEATER_BED_DUTY_CYCLE_DIVIDER
+          bed_needs_heating=1;
+          #endif
           WRITE(HEATER_BED_PIN,HIGH);
         }
       }
@@ -364,6 +386,9 @@ void manage_heater()
         else 
           if(current_raw_bed <= target_bed_low_temp)
         {
+          #ifdef HEATER_BED_DUTY_CYCLE_DIVIDER
+          bed_needs_heating=1;
+          #endif
           WRITE(HEATER_BED_PIN,HIGH);
         }
       }