Index: trunk/FACT++/src/ratecontrol.cc
===================================================================
--- trunk/FACT++/src/ratecontrol.cc	(revision 16771)
+++ trunk/FACT++/src/ratecontrol.cc	(revision 16772)
@@ -130,4 +130,5 @@
     }
 
+    // RETUNR VALUE
     void Step(int idx, float step)
     {
@@ -239,4 +240,6 @@
                 Step(i*4+j, step);
             }
+            // SET_SELECTED_THRESHOLDS
+
         }
     }
@@ -371,4 +374,15 @@
         PrintThresholds(sdata);
 
+        if (GetCurrentState()==RateControl::State::kSettingGlobalThreshold && fCalibrateByCurrent)
+        {
+            if (fThresholds.empty())
+                return RateControl::State::kSettingGlobalThreshold;
+
+            if (!std::equal(sdata.fThreshold, sdata.fThreshold+160, fThresholds.begin()))
+                return RateControl::State::kSettingGlobalThreshold;
+
+            return RateControl::State::kGlobalThresholdSet;
+        }
+
         fThresholds.assign(sdata.fThreshold, sdata.fThreshold+160);
 
@@ -436,5 +450,10 @@
             return GetCurrentState();
 
+        // We are not setting thresholds at all
         if (GetCurrentState()!=RateControl::State::kSettingGlobalThreshold)
+            return GetCurrentState();
+
+        // Target thresholds have been assigned already
+        if (!fThresholds.empty())
             return GetCurrentState();
 
@@ -498,7 +517,4 @@
         int counter = 1;
 
-        const int32_t val[2] = { -1, fThresholdMin };
-        Dim::SendCommandNB("FTM_CONTROL/SET_THRESHOLD", val);
-
         double avg2 = 0;
         for (int i=0; i<160; i++)
@@ -511,14 +527,14 @@
                 fThresholds[i] = max(uint16_t(40.5*pow(vec[i], 0.642)+164), fThresholdReference);
 
-                const int32_t dat[2] = { i, fThresholds[i] };
-                Dim::SendCommandNB("FTM_CONTROL/SET_THRESHOLD", dat);
-
-                fBlock[i/4] = true;
-
                 counter++;
             }
         }
-
         avg2 /= 160;
+
+
+        vector<int32_t> val(160, fThresholdMin);
+        std::copy(fThresholds.begin(), fThresholds.end(), val.begin());
+        Dim::SendCommandNB("FTM_CONTROL/SET_ALL_THRESHOLDS", val.data(), val.size()*sizeof(int32_t));
+
 
         const RateControl::DimThreshold data = { fThresholdMin, fCalibrationTimeStart.Mjd(), Time().Mjd() };
@@ -526,5 +542,5 @@
         fDimThreshold.Update(data);
 
-        Info("Sent a total of "+to_string(counter)+" commands for threshold setting");
+        //Info("Sent a total of "+to_string(counter)+" commands for threshold setting");
 
         ostringstream out;
@@ -532,8 +548,10 @@
         out << "Measured average current " << avg << "uA +- " << rms << "uA [N=" << fCurrentsMed.size() << "]... mininum threshold set to " << fThresholdMin;
         Info(out);
+        Info("Set "+to_string(counter)+" individual thresholds.");
 
         fTriggerOn = false;
         fPhysTriggerEnabled = false;
-        return RateControl::State::kGlobalThresholdSet;
+
+        return RateControl::State::kSettingGlobalThreshold;
     }
 
@@ -587,4 +605,6 @@
         fCalibrationTimeStart = Time();
         fBlock.assign(160, false);
+
+        fThresholds.clear();
 
         ostringstream out;
