I have a device that outputs multiple temperatures simultaneously. I have them all in a controlbox with various textboxes each one displaying a different temperature.
Every time I receive a new reading from the Arduino BLE I call this method:
private void parseReadings()
{
double max = probeArray[0].Temp;
double min = probeArray[0].Temp;
for(int i = 1; i < probeArray.Length; i )
{
if (probeArray[i].Temp > max)
{
max = probeArray[i].Temp;
probeArray[i].isHigh = true;
} else
{
probeArray[i].isHigh = false;
}
if (probeArray[i].Temp < min)
{
min = probeArray[i].Temp;
probeArray[i].isLow = true;
} else
{
probeArray[i].isLow = false;
}
}
this.Invoke((MethodInvoker)delegate { maxDisplay.Text = String.Format("{0:0.000}", max); });
this.Invoke((MethodInvoker)delegate { minDisplay.Text = String.Format("{0:0.000}", min); });
}
After that another code that updates the temperature on the control box:
for (int i = 0; i < probeArray.Length; i )
{
foreach (Control c in sensorTempPanel.Controls)
{
if (probeArray[i].isHigh)
{
Invoke(new Action(() => c.ForeColor = Color.Crimson)); //Will change the "highest" value text to Red
Invoke(new Action(() => c.Font = new Font(c.Font, FontStyle.Bold)));
}
else
{
Invoke(new Action(() => c.ForeColor = Color.Black));
Invoke(new Action(() => c.Font = new Font(c.Font, FontStyle.Regular)));
}
if (probeArray[i].isLow)
{
Invoke(new Action(() => c.ForeColor = Color.Blue)); //Will change the "lowest" value text to Blue
Invoke(new Action(() => c.Font = new Font(c.Font, FontStyle.Bold)));
}
else
{
Invoke(new Action(() => c.ForeColor = Color.Black));
Invoke(new Action(() => c.Font = new Font(c.Font, FontStyle.Regular)));
}
}
}
Now for my output it's GUI-based but basically the blue part kind of works (sometimes it shows 2 or 3 in blue but they are usually the lowest values so I'm okay with that).
However, the red (crimson) one doesn't work at all. not a single number is colored red.
CodePudding user response:
Your max and min are all wrong
First you decide that sensor 0 is both the high and the low, but you do not mark it with isHigh or isLow. So if no other sensor is higher than sensor 0 you will not show red.
Second you progressively mark higher and lower ones as high or low before you have exhausted the whole array. So if you have
0 = 100
1 = 120
2 = 140
you will get 1 and 2 both marked isHigh.
I would do
double max = -1e6;
double min = 1e6;
int highInd = -1;
int lowInd = -1;
for(int i = 1; i < probeArray.Length; i )
{
if (probeArray[i].Temp > max)
{
max = probeArray[i].Temp;
highInd = i;
}
if (probeArray[i].Temp < min)
{
min = probeArray[i].Temp;
lowInd = i;
}
}
// here I have min,max the index of the high and the index of the low
CodePudding user response:
There is problem with your second function. The first "if" might set the color to crimson, but the second "if" will change it to blue or black, so it will never stay crimson. You should change it to, for example:
for (int i = 0; i < probeArray.Length; i )
{
foreach (Control c in sensorTempPanel.Controls)
{
if (probeArray[i].isHigh)
{
Invoke(new Action(() => c.ForeColor = Color.Crimson)); //Will change the "highest" value text to Red
Invoke(new Action(() => c.Font = new Font(c.Font, FontStyle.Bold)));
}
else if (probeArray[i].isLow)
{
Invoke(new Action(() => c.ForeColor = Color.Blue)); //Will change the "lowest" value text to Blue
Invoke(new Action(() => c.Font = new Font(c.Font, FontStyle.Bold)));
} else
{
Invoke(new Action(() => c.ForeColor = Color.Black));
Invoke(new Action(() => c.Font = new Font(c.Font, FontStyle.Regular)));
}
}
}