Hi Gang,
I’m working on a project to that involves the 16x2 LCD and the Spark Fun Max31855k breakout for the thermocouple. Everything works fine except that there seems to be some interference on the LCD. However, the interference is subject to only reading the temp off the thermocouple. Essentially, I get my temp displayed on the LCD, however, there is a random string after it every other iteration of the loop(). Is it possible there is a SPI conflict? If so, I’ve tried changing some of the parameters in the SparkFun lib but to no avail. Check the comment in the code below.
#include <SparkFunMAX31855k.h>
#include <SPI.h>
#include <LiquidCrystal.h>
const uint8_t CHIP_SELECT_PIN = 10; // Using standard CS line (SS)
float temperature = 1;
SparkFunMAX31855k probe(CHIP_SELECT_PIN);
LiquidCrystal lcd(7, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2);
Serial.begin(9600);
}
void loop() {
// If I comment this out and hard-code the value for temperature, it works fine. However, this way
// will result in something like 71.00 #R#2 or some other random add on.
temperature = probe.readTempF();
// temperature = 123;
delay(1000);
if (!isnan(temperature)) {
lcd.clear();
lcd.home();
lcd.print(temperature);
Serial.println(temperature);
} else {
lcd.print("No dice");
}
}
lcd.home() after lcd.clear() does not make sense as the clear() already positions the cursor in the left top.
I have no option to test the below but you can try it.
void setup() {
lcd.begin(16, 2);
lcd.clear();
Serial.begin(9600);
}
void loop() {
temperature = probe.readTempF();
//temperature = 123.456;
delay(1000);
char textbuffer[11];
snprintf(textbuffer, sizeof(textbuffer), "%-10.2f", temperature);
if (!isnan(temperature)) {
lcd.home();
lcd.print(textbuffer);
Serial.println(temperature);
} else {
lcd.print("No dice");
}
}
This uses a small buffer to store a formatted number and writes that buffer to the LCD. The minimum width of the formatted number is 10 characters and it has two digits after the decimal dot. Adjust the buffer and the number of characters / digits to your needs.
Note that the lcd.clear() has been moved to the setup() function.
Reference: [man snprintf
More info (google): [c format floating point numbers](c format floating point numbers - Google Search)](http://linux.die.net/man/3/snprintf)
Since loop() runs over and over again, lcd.clear() needs to be in the loop() to clear it for the next reading.
Ross Robotics:
Since loop() runs over and over again, lcd.clear() needs to be in the loop() to clear it for the next reading.
I assume you're addressing my reply.
If so, by just moving the cursor to the home position and writing a right-padded string (more-or-less fixed width) should remove the need to have the lcd.clear() in the loop.
If I’m wrong, please let me know (as said, no option to test it at this stage).
Hi all,
Thank you very much for your replies. I figured out a solution without understanding my problem. I ended up switching the pin assigned to the “enabled” parameter in the LCD constructor from D11 to D6 and the crazy thing is now working perfectly. I really don’t understand what would be the difference between the two pins, perhaps 11 is of a different type than 6 (I forgot to specify I was on an Uno) and would be super interested in any insight that anyone has on this.
Thanks again for the help,
Jeremy