0

Code below is to read 4 thermometers and use values to turn on or off the heaters (I'm stuck at reading).

I'm reading and displaying temperature very well, but it comes to the problem, where I want to set the limit of heating, a value of 37 degrees. It comes to the problem, when I want to read the value of "tempa", but the Arduino doesn't respond properly: it somehow reads the value as char or I get the output in hex instead of integer (although I never declared a character in whole program).

It can be seen in last 2 lines before "CHANNEL_2".

Lines are:

lcd.setCursor(10, 0);    //termometer
lcd.write(tempa);    //termometer

absoluta, absolutb, absolutc and absolutd are variables, which are technically values of temperature in Kelvin.

tempa, temb, tempc and tempd are variables, the temperature in celsius.

#include <LiquidCrystal.h>
#define  LM335_pin  0                          // LM335 output pin is connected to Arduino A0 pin
#define  LM335_pio  1                          // LM335 output pin is connected to Arduino A0 pin
#define  LM335_pip  2                          // LM335 output pin is connected to Arduino A0 pin
#define  LM335_pir  3                          // LM335 output pin is connected to Arduino A0 pin

const int rs = 7, en = 8, d4 = 5, d5 = 4, d6 = 3, d7 = 2; // LCD module connections (RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);


  //-----------------------HEATERS----------------------------------------
//int grelea = 11;  //heater_1
//int greleb = 10;  //heater_2
//int grelec = 9;   //heater_3

void setup() {
  lcd.begin(20, 4);

//  pinMode(grelea, OUTPUT);
//  pinMode(greleb, OUTPUT);
//  pinMode(grelec, OUTPUT);

} 
int  absoluta, tempa; //sensor_1
int  absolutb, tempb; //sensor_2
int  absolutc, tempc; //sensor_3
int  absolutd, tempd; //sensor_4

void loop() {

  delay(1000);                                 

  //-----------------------CHANNEL_1----------------------------------------
  absoluta = analogRead(LM335_pin) * 0.489;
  tempa = absoluta - 273;

  lcd.setCursor(0, 0);    //termometer

  lcd.setCursor(0, 0);    //termometer
  lcd.write("Temp1=");    //termometer  

  lcd.setCursor(8, 0);    //termometer
  lcd.write((tempa / 10)% 10  + 48);    //termometer

  lcd.setCursor(9, 0);    //termometer
  lcd.write(tempa % 10  + 48);    //termometer

  lcd.setCursor(10, 0);    //termometer
  lcd.write(char(223));    //termometer

  lcd.setCursor(11, 0);    //termometer
  lcd.write("C");    //termometer

  lcd.setCursor(15, 0);    //termometer
  lcd.write(tempa);    //termometer

  //---------------------------------------------------------------


  //-----------------------CHANNEL_2----------------------------------------
  absolutb = analogRead(LM335_pio) * 0.489;
  tempb = absolutb - 273;

  lcd.setCursor(0, 1);    //termometer
  lcd.write("Temp2=");    //termometer

  lcd.setCursor(8, 1);    //termometer
  lcd.write((tempb / 10) % 10  + 48);    //termometer

  lcd.setCursor(9, 1);    //termometer
  lcd.write(tempb % 10  + 48);    //termometer

  lcd.setCursor(10, 1);    //termometer
  lcd.write(char(223));    //termometer

  lcd.setCursor(11, 1);    //termometer
  lcd.write("C");    //termometer
  //---------------------------------------------------------------


  //-----------------------CHANNEL_3----------------------------------------
  absolutc = analogRead(LM335_pip) * 0.489;
  tempc = absolutc - 273;

  lcd.setCursor(0, 2);    //termometer
  lcd.write("Temp3=");    //termometer

  lcd.setCursor(8, 2);    //termometer
  lcd.write((tempc / 10) % 10  + 48);    //termometer

  lcd.setCursor(9, 2);    //termometer
  lcd.write(tempc % 10  + 48);    //termometer

  lcd.setCursor(10, 2);    //termometer
  lcd.write(char(223));    //termometer

  lcd.setCursor(11, 2);    //termometer
  lcd.write("C");    //termometer
  //---------------------------------------------------------------


  //-----------------------CHANNEL_4----------------------------------------
  absolutd = analogRead(LM335_pir) * 0.489;
  tempd = absolutd - 273;

  lcd.setCursor(0, 3);    //termometer
  lcd.write("Temp4=");    //termometer

  lcd.setCursor(8, 3);    //termometer
  lcd.write((tempd / 10) % 10  + 48);    //termometer

  lcd.setCursor(9, 3);    //termometer
  lcd.write(tempd % 10  + 48);    //termometer

  lcd.setCursor(10, 3);    //termometer
  lcd.write(char(223));    //termometer

  lcd.setCursor(11, 3);    //termometer
  lcd.write("C");    //termometer
  //--------------------------------------------------------------- 
}
  • 3
    try lcd.print(tempa); – Juraj Sep 17 '18 at 14:03
  • btw: it is not hex, only your calculated ascii values run out of digits section of ascii codes – Juraj Sep 17 '18 at 14:18
1

lcd.write() will write one byte to the LCD. It makes no attempt to try to interpret the data type of the input parameter. You can think of it like sending "raw data" to the LCD.

lcd.print() can be called with many types as input and will interpret that the user wishes to print a human-readable expression of the data. Also, it can print more than one byte.

As an example, the char variable with content 'A' is represented in RAM by the byte with value 65 (the ASCII code for 'A'). If you call lcd.print('A'), it will see that 'A' is a char and will send whatever raw data to the LCD to get an "A" on the screen. It just so happens that sending a raw byte 65 will accomplish this, but that is a coincidence; the LCD driver understands ASCII for getting characters to print on the screen.

If you call lcd.write(125), then the actual byte 125 (in binary, 01111101) is sent to the LCD. If you wanted to actually print the 3-digit number 125, lcd.print(125) would end up sending 3 bytes to the LCD: The ASCII values for '1', '2', and '5' (which are 49, 50, and 53). The print() function takes care of this interpretation for you.

  • she has v / 10 + 48 in code. I don"t think she needs to explain binary and ascii – Juraj Sep 17 '18 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.