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.
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....
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.
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.
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?
@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.