B4R Question Temperature question with adruino ide

tufanv

Expert
Licensed User
Longtime User
Hello,

I was trying the temperature example found on internet for adruino ide. ( I could not test with b4r yet ) but the code for adruino is:

B4X:
#include  <LiquidCrystal.h>         // Include the library to use a LCD display

#define          sensor           0        // Define the A0 pin as “sensor”


int  Vin;           //  Variable to read the value from the Arduino’s pin

double  Temperature; //  Variable that receives the converted voltage value to temperature

float     TF; // Variable to receive the converted value from  ºC to ºF


LiquidCrystal  lcd    (12, 11, 5, 4, 3, 2);

/* The function above declares which Arduino’s pins will be used for controlling the LCD */

void  setup()

{

  lcd.begin(16, 2);                            //  It tells the Arduino that the display is a 16x2 type

  lcd.print("Sicaklik: ");           //  Send the text to the screen of the display.

}

void  loop()

{

  Vin = analogRead (sensor);  /*   Tells the Arduino to read the pin and stores the value in “Vin” */

  Temperature=(500*Vin)/1023;  /* Converts the voltage value into temperature and stores it into the “Temperature”  variable  (in  ºC)*/

TF = ((9*Temperature)/5)+32; // Converts  ºC to ºF

  lcd.setCursor(0, 1);           // Moves  the cursor of the display to the next line

  lcd.print(Temperature);    // Exhibits the value of the temperature on the display

  lcd.print(" C");         // Writes “F” to indicate that it is in Fahrenheit scale.


  delay(1000);  //  Waits for a second to read the pin again

What i get on the lcd is rounded values like 24 Celcius or 25 Celcius. How can i get exact values , what part must i change ?

ty
 

canalrun

Well-Known Member
Licensed User
Longtime User
I'm no Arduino expert, but I would be suspicious of the line:
Temperature=(500*Vin)/1023

You've got an integer times an integer divided by an integer. This will give an integer result.

I would try making the 500 a floating-point value – maybe 500.0 will do the trick.

I'm willing to bet that changing the line of code to:
Temperature=(500.0*Vin)/1023
would give a floating-point result to Temperature.

If that doesn't work, the next thing I would try is multiplying by a floating-point value:
Temperature=(1.0*500*Vin)/1023

Barry.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…