Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am having trouble on my mbed (c++) with getting a program to work properly. Ho

ID: 3729348 • Letter: I

Question

I am having trouble on my mbed (c++) with getting a program to work properly.

How would I take 1000 samples from a temperature sensor and have it display the highest temperature when it finds one? I will press a button and after i press it, I need it to display a high temperature on a serial display on my computer (i know how to get it to the display, but am not sure how to get the high out of 1000 samples).

Here is what I have so far and it keeps giving the high temperature as 0:

float temp[];
float highTemp = 0;
void record(void) { // displays the record high temperature
for (int i = 0; i < 1000; i++) {
temp[i] = ((sensor*3.3)-0.500)*100.0;
if (temp[i] > highTemp) {
temp[i] = highTemp;
}
}
pc.printf("Highest Temperature = %5.2f C ", highTemp);
wait(0.5);

}

sensor is the data coming from the temperature sensor, i am displaying it to Tera Term on my computer to see the high when i click the button. I cannot figure out how to click the button once and have it continue to display instead of me clicking it and only displaying while the button is pressed down. I also need to know how to set up the array correctly so it does not display 0.

Explanation / Answer

Your code is

float temp[];
float highTemp = 0;
void record(void) { // displays the record high temperature
for (int i = 0; i < 1000; i++) {
temp[i] = ((sensor*3.3)-0.500)*100.0;
if (temp[i] > highTemp) {
temp[i] = highTemp; //this step is wrong
}
}
pc.printf("Highest Temperature = %5.2f C ", highTemp);
wait(0.5);

}

In the code above you have written

if (temp[i] > highTemp) {
temp[i] = highTemp;
}

This will assign the value of highTemp to temp[i] when temp[i]>highTemp

But we need to assign the value of temp[i] to highTemp when temp[i] > highTemp

Give it some thought,we need to modify the value of highTemp not temp[i]. So highTemp should be on the left hand side of the = operator

To your next query about pressing the button once and to keep it running you can use infinite loop like while(1) or for( ; ; )