Storing 10 Temperature readings?

Im trying to work out how to store 10 temperature readings from my temperature sensor. I have the sensor linked to a LDC display and am able to get it to display the current temperature.

When button 1 is pressed, I need code so that the system records ten temperatures at one-second intervals, and the temperatures to then be able to be stored for later use.

When button 2 is pressed, If there are stored temperatures in memory, the system displays them.

I hope this is clear?

Thank you!

float temperature [10];//declare variable to store them


void takeReadings() { //call this from loop when you check the button and find it to be pressed. There are oodles of examples. 
for (byte i = 0;i<10;i++) {
temperature[i]=getOneReading(); // where getOneReading() returns the current temperature reading);
delay(1000); 
}

Then you can do the same thing when you display the temperatures, loop through them with a for loop.

The above code of course is blocking because of the delay - but you could do it with blink-without-delay techniques, where you record when you took the last reading....

byte readingsTaken;
unsigned long lastTookReading;

//in loop:

if (millis() - lastTookReading > 1000 && readingsTaken<10;) {
takeReading();
readingsTaken++;
}

void takeReading() {
   temperature[readingsTaken]=getOneReading()
}

There are lots of approaches to doing this........

Sounds like a job for an array of values.

When button 1 becomes pressed store a value and increment the array index at 1 second intervals timed with millis(). When button 2 becomes pressed play back the values in the array one at a time at a suitable interval.

MOderator: cross posts merged..

You have two variables called "simpleArray".

That's not a good thing.

Putting ten things into a nine element array isn't a good thing either.

First, defining an array with 9 elements and then trying to loop through 10 elements is probably not a good idea. Second, the statement:

   simpleArray[i] = tempC;

should be written:

   simpleArray[i] = (float) tempC;

While the cast probably won't solve your problem, it at least documents your intent.

Finally, why do you have multiple definitions of tempReading, tempVolts, and tempC in loop()?

Second, the statement:...should be written:

Why? There is no need to explicitly cast a float to a float. There is no reason to cast anything when storing a float in an int array.

Of course, using an int array to store floats is an odd idea anyway.

PaulS:
Why? There is no need to explicitly cast a float to a float. There is no reason to cast anything when storing a float in an int array.

Of course, using an int array to store floats is an odd idea anyway.

. . . especially when it's about to go out of scope anyway. See reply #1.

AWOL:
. . . especially when it's about to go out of scope anyway. See reply #1.

Well, I was assuming that OP would fix THAT problem.

@PaulS:

... it at least documents your intent.

I realize the cast is not necessary, but any time I have code that assigns a "larger" data type into a "smaller" data type, I cast it. I even use a cast where "silent casts" (e.g., smaller into larger) take place. I always taught my students to do that since it does not affect code generation, but calls attention to the fact that a mismatched assignment is taking place...that's why, and I see no harm in doing it.

any time I have code that assigns a "larger" data type into a "smaller" data type, I cast it.

A cast from one type to the same type documents something, all right. It documents that you are clueless or an idiot. It does NOTHING for the data truncation issue that is going to happen.

And, besides, casting from the larger type to the smaller type is NOT what you suggested.

A cast from one type to the same type documents something, all right. It documents that you are clueless or an idiot. It does NOTHING for the data truncation issue that is going to happen.

"...clueless or an idiot." Really? I already said it doesn't affect code generation and "does NOTHING for the data truncation issue". However, I find such casts very useful when debugging and, if I sense a truncation issue, those casts mark the first place(s) that should be examined, which is why I made the "documentation" comment.

My guess is that you've never taught incoming Freshmen a programming class and anything one can do to make any type of programming error easier to detect is a good thing. I've taught such people for almost 30 years, including ten years at a Big Ten university, and written 18 programming texts, so I would shit-can the holier-than-thou attitude and quit the name-calling...not just to me, but to all the other people you've insulted over the years. Your experience and comments will carry weight independently of the denigrating insults.

My guess is that you've never taught incoming Freshmen a programming class

No, I haven't. Individuals, yes. A group, no.

and anything one can do to make any type of programming error easier to detect is a good thing.

Well, I can NOT see that casting a float to a float does anything to make programming errors easier to detect.

@PaulS:

Well, I can NOT see that casting a float to a float does anything to make programming errors easier to detect.

I agree, but my statement:

  simpleArray[i] = (float) tempC;

was based on his original code definition of the array:

int simpleArray[9];

which makes me wonder about your quote.

You have two arrays with the same name.
Can the second one and see how you get on.

On the eight bit Arduinos, float and double are the same thing - same size, same range, same precision.

I agree, but my statement:...was based on his original code definition of the array:

What type is tempC? Why do you recommend casting a float to a float? That accomplishes nothing.

With the type on the left being int, regardless of the actual type on the right, why would you cast to some type other than what is to be stored on the left?

which makes me wonder about your quote.

Which quote?

i think that it thinks they're two different arrays?

That would be because they ARE two different arrays.

@PaulS: read my last post. His original code had simpleArray[] defined as an int while tempC was a float, so the statement:

simpleArray[i] = (float) tempC;

was assigning a float into an int, which will result in truncation, and not the float-to-float assignment you're talking about. Your post about the float-to-float assignment was before the OP changed the array to a double, which is either prescient or misreading the code in post #1.

His original code had simpleArray[] defined as an int

Yes, it did.

while tempC was a float, so the statement:

simpleArray[i] = (float) tempC;

was assigning a float into an int,

Yes, that is true. But, tempC IS a float, so casting it to a float is pointless. How the cast value is to be used is beside the point.

Now, if you were to cast tempC to an int, explicitly declaring that you KNOW that truncation will happen, that would be a completely different story.

Your post about the float-to-float assignment was before the OP changed the array to a double

Specifically which post would that be? In reply #8, I talked about the assignment of a float to an int array.

Got it. I should have written:

simpleArray[i] = (int) tempC;