Of course, the first thing I was tempted to do after connecting it up, was to run my thermometer sketch, but I'm glad I didn't, as I would have been very upset when it didn't work. Thankfully, I remembered to run the I2C scanner sketch first, and yes - my I2C backpack has the 'AT' variant of the control chip I mentioned in the previous post, so the unit address is 0x3F, not 0x27. In the picture, you can also see the three sets of alternate address pads.
So, then I loaded up my sketch, changed the address in the LCD declaration to 0x3F, and fired it up. The backlight came on, but nothing else, so I twiddled the potentiometer on the backpack (the blue box thing in the image above, which controls the contrast of the screen), and up came my min/max display, just as I imagined it. Obviously, it had garbage figures, because in my impatience, I hadn't bothered to connect it up to the breadboard with the LM35, so the analogReads on pin A0 were getting random floating values - but it was enough just to prove the display works.
Now I have connected it up properly, and found a few bugs in my code, so I spent this evening debugging my theory, and adding a new feature.
The first thing I noticed was that the 5 minute average figure was always the same as the current reading. I added a few Serial.print lines to show all the values used in my calculations, and found that the counter was always 1. This led me to the conclusion that the problem must be in the reset area, which is only supposed to run every 5 minutes, but appeared to be running every loop.
The code looked fine except there was something niggling me about the IF statement having a calculation in it... The line read
if (fiveMinCount >= ((5 * 60 * 1000) / loopPeriod))
Now, I've come across this somewhere before, but can't remember why or where... I changed the (5 * 60 * 1000) to just read 300000 instead, and the average calculations started working properly - so it appears that IF statements don't like their mathematical comparisons to be too complicated.
The other little change I made was inspired by a similar kind of project I saw in a youtube tutorial by Martin Lorton, who made a very similar looking current/max/min type display for an Arduino based voltage meter. He added a function that showed an asterisk next to the max/min values when they changed, and also flashed an LED. He suggested you could also use a piezo transducer and the tone command, to make a little beep. I didn't go that far, but did add the asterisk, and flash the little onboard LED. Here is the code that does that...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | // if "Temp now" is a new high... if (celsius > maxTemp) { //store current value as new high maxTemp=celsius; // and change value on display lcd.setCursor(12,1); if (maxTemp < 10) lcd.print("0"); // Add leading 0 if < 10C lcd.print(maxTemp,1); lcd.setCursor(19,1); lcd.print("*"); digitalWrite(led,HIGH); } else { lcd.setCursor(19,1); lcd.print(" "); } |
We also need to declare an int variable called led and assign the number 13 to it, then declare the pinmode as output at the beginning of the sketch, and also turn the led OFF at the end of each loop.
So finally, after a couple of months of planning, theorising, pontificating, researching, and changing my mind countless times, here is the actual thermometer working as planned. Sorry I didn't get a picture with the asterisk and LED indicating a new Max or Min figure.
My next plan is to mount all this stuff onto a board, so I have a proper little test bed. Unfortunately, the display does not work from 3.3v, and the I2C interface pin breakout set on my Arduino only offers 3.3v, so I'll have to think how to connect the display to the Arduino semi-permanently once mounted on the board. But that's a problem for another day. I'm off to bed know, and will leave my thermometer running to see just how cold it gets in my lounge overnight...
...and I know what I said, but I can feel a fridge/freezer experiment coming on 😀


No comments:
Post a Comment