Reputation: 195
This is the code:
double value = Convert.ToDouble(sensor.Value);
if (value < minTemp) minTemp = value;
if (value > maxTemp) maxTemp = value;
label8.Text = maxTemp.ToString() + "c";
label8.Visible = true;
label9.Text = minTemp.ToString() + "c";
label9.Visible = true;
temperature_label.Text = sensor.Value.ToString() + "c";
int t = temperature_label.Text.Length;
if (t > 3)
{
temperature_label.Location = new Point(238, 200);
}
label8 display maximum temperature label9 display the minimum and temeprature_label display the current. I want to add a label that will display in real time the current average. How can i do it ?
And in this part of the code im writing the current temeprature to a logger(text) file if its above a temperature the user set:
if (sensor.Value > float.Parse(textbox3_value))
{
Logger.Write("The current temperature is ===> " + sensor.Value);
button1.Enabled = true;
}
The problem is that all this codes are in a timer tick that is updating every second. And its writing to the Logger file its adding each time a new line.
The logger file for example look like this:
1/1/2014--12:50 PM ==> The current temperature is ===> 71
1/1/2014--12:50 PM ==> The current temperature is ===> 71
1/1/2014--12:50 PM ==> The current temperature is ===> 71
1/1/2014--12:50 PM ==> The current temperature is ===> 71
1/1/2014--12:50 PM ==> The current temperature is ===> 71
1/1/2014--12:50 PM ==> The current temperature is ===> 72
1/1/2014--12:50 PM ==> The current temperature is ===> 72
1/1/2014--12:50 PM ==> The current temperature is ===> 73
1/1/2014--12:50 PM ==> The current temperature is ===> 74
1/1/2014--12:50 PM ==> The current temperature is ===> 73
1/1/2014--12:50 PM ==> The current temperature is ===> 73
And i want to make the logger file format to be something like this:
1/1/2014--12:50 PM ==> Session Started
1/1/2014--12:50 PM ==> The current temperature is ===> 71
1/1/2014--12:50 PM ==> The last highest temperature was ===> 80
1/1/2014--12:50 PM ==> The last minimum temperature was ===> 40
1/1/2014--12:50 PM ==> The average temperature is ===> 75
And when there is a new value of one of this lines it will update this line value and won't add a new one.
This is the Write method in the Logger class:
public static void Write(string str)
{
if (mut.WaitOne() == false)
{
return;
}
else
{
using (StreamWriter sw = new StreamWriter(full_path_log_file_name, true))
{
sw.Write(DateTime.Now.ToShortDateString() + "--" + DateTime.Now.ToShortTimeString() + " ==> " + str);
sw.WriteLine();
sw.Close();
}
}
mut.ReleaseMutex();
}
Upvotes: 0
Views: 634
Reputation: 101681
First define a list to store all values, and a variable to store average:
List<int> values = new List<int>();
double average = 0.0;
And in here :
double value = Convert.ToDouble(sensor.Value);
values.Add(value);
average = values.Average();
Logger.Write("The average temperature is ===> " + average);
Upvotes: 1