You may not understand every single line of code, but that's OK - you can look up individual commands in the Arduino Programming Language Reference, or get in touch with me and I'll do my best to explain to the best of my understanding. So, let's dive in...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // include I2C library for LiquidCrystal (www.sunfounder.com) // check the specific display against http://playground.arduino.cc/Code/LCDi2c for correct I2C library #include <LiquidCrystal_I2C.h> // declare variables - pin connections int LM35out=0; // LM35 output -> pin A0 int lightSwitch=8; // pin D8 for backlight pushbutton // declare variables - Min/Max float minTemp=99.9; // initially set very high float maxTemp=0.0; // initially set very low // declare variables - Backlight unsigned long lightOnTime; // to hold millis() value when pushbutton pressed char lightState = "off"; // holds current state of backlight (on/off) // declare variables - Five Min Averages const int loopPeriod=3000; // set period as a constant for use in average calcs as well as controlling loop float fiveMinAccum=0; // set accumulator to 0 int fiveMinCount=0; // set counter to 0 float fiveMinAve=0; // set average to 0 // declare the LCD device variable (7-bit address, chars, lines) LiquidCrystal_I2C lcd(0x27,20,4); |
So, remember that all the lines starting with // are simply comments, and comments can also follow the line of code - on the same line. I have a terrible memory, so I put LOTS of comments in my sketches. You might find them helpful to read - I know I do! Notice that each line of code ends with a semi-colon (;) If you forget to type them in when you start your own coding, be ready for all sorts of strange problems!
Line 3 is the one that gets the external code library for the LCD display.
The library is called LiquidCrystal_I2C.h
Lines 6 and 7 declare a couple of variables that each hold an integer type of number. These are actually just to identify 2 of the I/O pins on the Arduino Uno's chip. It is easier in the program to refer to these pins using a name with some context, rather than having to remember what 'pin 8' is used for, for example.
Lines 10 and 11 set up 'float' type variables. A 'float' can store a number with decimal places, rather than just an integer. I have initially set the minimum temperature to a high value and the maximum to a low value. The first time I read the actual temperature it is likely to be somewhere between these two, and so both will get overwritten with the actual temperature then.
Line 14 is setting a 'long' numeric variable. Note that it is also marked as being unsigned. By default, all numeric variables are signed, which means they can hold negative numbers. Every numeric type (float, integer, long, and double) has a specific range of numbers it can count to - and for signed types, 0 comes in the middle with numbers spreading out equally on the positive and negative sides. For example, an 'int' type number can hold the values -32,768 to +32,767. However, an unsigned int cannot hold negative numbers, but it does still have the same number of numbers, just starting at 0. So an unsigned int can hold any number from 0 to 65,535. OK, back to line 14 again - this variable is going to hold the count of milliseconds since the sketch started running, when the button is pressed to turn on the backlight.
Line 15 is a 'string' variable. Instead of numbers, this holds a text value - in this case, it will be simply 'ON' or 'OFF' to indicate to the program if the backlight is on or off.
Line 18 is another integer value, but notice that this one has the word 'const' in front of it. This turns the variable into a 'read-only' constant value. Once it is declared here, the value in it cannot be changed elsewhere in the sketch. In this case, it is the delay to be used at the end of the loop function, before it goes back and starts again.
Lines 19-21 declare some variable to be used when calculating the 5 minute average temperature, which I will explain when we get to that part of the code.
Finally - Line 24..! What the heck is this doing?
Firstly, note that it is somehow linked to the external code library we pulled in (line 3) - notice how the line starts with the name of that library (minus the dot-h suffix)? Next comes a name lcd followed by parentheses with some strange looking numbers separated by commas. A bit like the parentheses on the function definition we saw earlier, these ones are also holding some parameters.
Remember that the external code library takes lots of complex coding and breaks it out into easier functions for us to use (like my examples 'turn on the backlight' or 'clear the screen')? OK, now what this command is doing is creating a way of linking all those functions to something in the sketch (let's call it an 'object') that we will call lcd, and then telling lcd where my actual lcd is (0x27 is the address of the lcd controller), and how big it is (20 characters, 4 rows).
So now the object we have called lcd has links to all those functions in the library (we call them methods), and also has some parameters that link it to the physical device (we call these attributes). Once this line has been run, then we can call any of the methods in the external code library, and send them to the lcd like this...
1 2 3 4 5 | // turn backlight on lcd.backlight(); // turn backlight off lcd.nobacklight(); |
Phew - that's been quite a bit for me to type, and you to take in - so let's take a break here, and I will post the setup() function in part 3, very soon.
No comments:
Post a Comment