There are a number of unknowns, such as, will my LM35 temperature sensor (while installed in the module that I salvaged from a burned out UPS) still produce 0-1.5 volts over the range 0-150C, or does that op-amp chip actually change the voltage? Will it even work at all? Will the LCD library I found work with the LCD I purchased (I've seen dozen's of forum messages from people unable to get their LCDs to function - which usually comes down to addressing or library compatibility problems).
The sketch has gone through a number of iterations, and evolved somewhat from the original... but the nuts and bolts of the original concept were...
- Include LCD library, and declare variables.
- Setup
- Initialise LCD
- Write static information to LCD
- Loop
- Check temperature - write it to LCD
- If it is higher than Max, then replace the value in Max and write to LCD
- If it is lower than Min, then replace the value in Min and write to LCD
- Wait a few seconds, then go back to start of Loop
Hopefully, this is not too difficult to follow. Arduino sketches all have this basic structure of three segments.
The first is where any external code libraries are identified to the sketch. A code library handles all the complex instructions for a device we want to use (such as the LCD), and breaks them out into simpler functions (like 'turn the backlight on' or 'clear the display') that we can use more easily in our own code.
Variables can also be declared here. Variables are just named buckets to hold a particular piece of information, like the current temperature, or the highest temperature that has been seen so far. If you declare a variable here, it can be used by code anywhere in the sketch.
Variables can also be declared here. Variables are just named buckets to hold a particular piece of information, like the current temperature, or the highest temperature that has been seen so far. If you declare a variable here, it can be used by code anywhere in the sketch.
The next two segments handle the setup of everything that is needed to make your sketch work, and then a repeating loop that runs the actual process itself. Arduino sketches use a code construct called a 'function' for these two segments. A function is a named block of code, designed for a specific purpose, that can be called from other sections of code. The block of code can receive input parameters, and/or return an output value at the end - but both of these are optional, and for these two special functions in every Arduino sketch, neither option is used.
This is how you start to define a function...
1 2 3 | output_value function_name(input_parameters) { // Your code goes here } |
Because these two special functions have no input parameters, there is nothing between the parentheses, and as there is no output value, then this is noted by using the word 'void'. So, the two special functions look like this when you first open a new sketch... (in case you hadn't figured it out, the lines starting with // are comments).
Here you can see two functions - one called 'setup' and one called 'loop'. An Arduino sketch always expects these two functions, even if they have nothing in them! Setup is run just once at the beginning, and then loop is run. At the end of all the code in loop, it will 'loop' back round to the start and run all the code again, and again, and... well you get the picture. Pretty much the only way to stop it, is to pull the plug!
You can write your own functions in addition to setup and loop, but we'll get to that concept a bit later. Note that variables can also be declared inside a function, but if they are, then they are only available for use within that particular function.
My original code was written with a 16 character by 2 line LCD in mind. But when trying to fit the current, minimum, and maximum temperatures on the display, each with meaningful labels, I realised that 16 by 2 provides a very limited amount of display real-estate. That was what triggered me to opt for the larger 20 by 4 LCD in my shopping list instead, and that was the first of my many code changes.
I then found how I could improve the accuracy of the mechanism to read the temperature value; how to format the output to 1 decimal place (and include a leading 0 if required); added a button press trigger to turn on the backlight for 10 seconds without interrupting the real-time reading of the sensor; and finally, added a function to calculate an average value over 5 minutes. This last improvement was done ready for the next phase of my experimentation, which is to use this as a data-logger and either write data to an SD card, or directly back to my PC (where it could be displayed graphically, for example). I've also found an alternate mechanism to turn the backlight on and off by degrees of brightness - but I don't know if my display will actually support it.
Well - this post has rambled on rather, so I'm going to call it a day for now. I'll post part 2 in the next day or two, which will include the whole sketch as it exists at his point (remember - it is untested!!), and will break the code down with some explanations of what each bit does - or is intended to do.
My original code was written with a 16 character by 2 line LCD in mind. But when trying to fit the current, minimum, and maximum temperatures on the display, each with meaningful labels, I realised that 16 by 2 provides a very limited amount of display real-estate. That was what triggered me to opt for the larger 20 by 4 LCD in my shopping list instead, and that was the first of my many code changes.
I then found how I could improve the accuracy of the mechanism to read the temperature value; how to format the output to 1 decimal place (and include a leading 0 if required); added a button press trigger to turn on the backlight for 10 seconds without interrupting the real-time reading of the sensor; and finally, added a function to calculate an average value over 5 minutes. This last improvement was done ready for the next phase of my experimentation, which is to use this as a data-logger and either write data to an SD card, or directly back to my PC (where it could be displayed graphically, for example). I've also found an alternate mechanism to turn the backlight on and off by degrees of brightness - but I don't know if my display will actually support it.
Well - this post has rambled on rather, so I'm going to call it a day for now. I'll post part 2 in the next day or two, which will include the whole sketch as it exists at his point (remember - it is untested!!), and will break the code down with some explanations of what each bit does - or is intended to do.

No comments:
Post a Comment