I'm trying to wrap my head around some example code, that I'm implementing into a sketch.
This all started trying to get rid of using lcd.clear() and horrible screen flickering. Starting looking into a LCD buffer.
Found a great tutorial for getting it going, but not much explanation on what each bit of code is doing. Trying to read about sprintf has led to more questions and confusion than answers.
My questions are:
Is there a resource you can point me to, to understand the padding characters/methodology for left justified, right justified, what characters I can pad with, etc.
And as a side note (I'm just realizing) is why my output is printing to line 0 and line 2, not line 0 and line 1.
There also seems to be a harsh relationship to the "outputTemp[]" array (the size of the array) and the number in the "&-7s" that I don't understand. IE: if that array is set to less than 10, it breaks the whole sketch and for some reason pin 13 starts blinking... I'm assuming that's some kind of overrun error or something. Not that concerned with it, but AM trying to figure out the relationship to that outputTemp array size, combined with the padding.
I hope I explained that well enough. I've tried truncating the sketch to only the relevant parts.
#include <LiquidCrystal.h>
// LCD Setup
const int rs = 22, en = 23, d4 = 24, d5 = 25, d6 = 26, d7 = 27;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// LCD Buffer
char line0[21];
char line1[21];
char line2[21];
char line3[21];
// Thermistor Pin - 10k pullup down
const int thermistorPin = A2;
const int pullUpResistor = 10000;
void setup() {
lcd.begin(20, 4);
}
void loop() {
float testTemp = getTemp(thermistorPin, pullUpResistor);
char outputTemp[10];
dtostrf(testTemp,14,2,outputTemp);
sprintf(line0, "___[Title Screen]___");
sprintf(line1, "Temp:%-7sF", outputTemp);
refreshLCD();
}
// Output LCD buffers
void refreshLCD() {
lcd.setCursor(0, 0);
lcd.print(line0);
lcd.print(line1);
lcd.print(line2);
lcd.print(line3);
}
// Read Analog Thermistor
float getTemp(int analogPin, int pullupValue) {
int Vo;
float logR2, R2, Temp;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
Vo = analogRead(analogPin);
R2 = pullupValue * (1023.0 / (float)Vo - 1.0);
logR2 = log(R2);
Temp = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
Temp = Temp - 273.15;
Temp = (Temp * 9.0)/ 5.0 + 32.0;
return Temp;
}