Hello guys,
I am working on a project where I need to compare the current consumed by 4 loads and turn off the two loads which are consuming the least current, i.e. least and second least current consuming loads should be turned off.
I have written a logic for determining the least current, How do I determining the second least value?
here is the code
if (CTA < CTB && CTA < CTC && CTA < CTD)
{
Serial.println("CTA in the lowest, turning off room1");
digitalWrite(room1, LOW);
}
else if (CTB < CTA && CTB < CTC && CTB < CTD)
{
Serial.println("CTB is the lowest, turning off room2");
digitalWrite(room2, LOW);
}
else if (CTC < CTA && CTC < CTB && CTC < CTD)
{
Serial.println("CTC is the lowest, turning off room3");
digitalWrite(room3, LOW);
}
else if (CTD < CTA && CTD < CTB && CTD < CTC)
{
Serial.println("CTB is the lowest, turning off room2");
digitalWrite(room4, LOW);
}
please suggest a way I can accomplish the above task.
If you were using arrays, you'd have the indices for the lowest two values. Those same indices would work in the other arrays.
Otherwise, you need to find the smallest value. Then, find the smallest value in the set that does not contain the value/variable that you already found. And THAT is a lot more difficult.