Serial.begin and LCD initialization (+SHT11)
i made sketch sht11 , dump reading on 16x2 lcd, , works fine.
however enable possibility read same values usb/serial. add serial.begin() setup rutine lcd refuse initialize.
am overlooking timing or wrong?
thanks in advance.
however enable possibility read same values usb/serial. add serial.begin() setup rutine lcd refuse initialize.
am overlooking timing or wrong?
code: [select]
#include <lcd4bit.h>
lcd4bit lcd = lcd4bit(2); //create 2-line display.
#include <stdlib.h>
#include <math.h>
/*
* sht11 usb (serial)
* anders s. nielsen
*
* example of using arduino board receive data
* sensirion sht11 humidity , temperature sensor.
* arduino board relays data sensor usb port (serial port).
* based on code http://dmgaming.com/
*
*/
// pin assignments
int shtclk=3; // sht11 clock pin connected pin
int shtdata=2; // sht11 data pin connected pin
int boardled=13; // led connected pin (on diecimila) , used signal user
int baudrate=9600; // speed used serial communication
// misc variables
long logtime;
int adccount;
int iobyte;
int ackbit;
unsigned int retval; // return value sht11
int dly;
long mstimer;
uint8_t bitmask;
double temp; // changed float double
double humi; // changed float double
double dewpoint;
// calculation constants based on sht11 data sheet
int d1 = -40; // sht11 data sheet @ 5v vdd , celcius operation
float d2 = 0.01; // sht11 data sheet @ 14bit , celcius operation
int c1 = -4; // sht11 data sheet @ 12bit operation
float c2 = 0.0405; // sht11 data sheet @ 12bit operation
float c3 = -2.8e-6; // sht11 data sheet @ 12bit operation
// function writes byte sht11
void sht_write_byte(void)
{
pinmode(shtdata, output);
shiftout(shtdata, shtclk, msbfirst, iobyte);
pinmode(shtdata, input);
digitalwrite(shtdata, low);
digitalwrite(shtclk, low);
digitalwrite(shtclk, high);
ackbit = digitalread(shtdata);
digitalwrite(shtclk, low);
}
int shiftin()
{
int cwt;
cwt=0;
bitmask=128;
while (bitmask >= 1)
{
digitalwrite(shtclk, high);
cwt = cwt + bitmask * digitalread(shtdata);
digitalwrite(shtclk, low);
bitmask=bitmask/2;
}
return(cwt);
}
// function reads byte sht11
void sht_read_byte(void)
{
iobyte = shiftin();
digitalwrite(shtdata, ackbit);
pinmode(shtdata, output);
digitalwrite(shtclk, high);
digitalwrite(shtclk, low);
pinmode(shtdata, input);
digitalwrite(shtdata, low);
}
// function reset sht11
void sht_connection_reset(void)
{
shiftout(shtdata, shtclk, lsbfirst, 255);
shiftout(shtdata, shtclk, lsbfirst, 255);
}
void sht_soft_reset(void)
{
sht_connection_reset();
iobyte = 30;
ackbit = 1;
sht_write_byte();
delay(15);
}
void sht_wait(void)
{
delay(5);
dly = 0;
while (dly < 600)
{
if (digitalread(shtdata) == 0) dly=2600;
delay(1);
dly=dly+1;
}
}
void sht_start(void)
{
digitalwrite(shtdata, high);
pinmode(shtdata, output);
digitalwrite(shtclk, high);
digitalwrite(shtdata, low);
digitalwrite(shtclk, low);
digitalwrite(shtclk, high);
digitalwrite(shtdata, high);
digitalwrite(shtclk, low);
}
// function reads data sensor
void sht_measure(int vsvc)
{
sht_soft_reset();
sht_start();
iobyte = vsvc;
sht_write_byte();
sht_wait();
ackbit = 0;
sht_read_byte();
int msby;
msby = iobyte;
ackbit = 1;
sht_read_byte();
retval = msby;
retval = retval * 0x100;
retval = retval + iobyte;
if (retval <= 0) retval = 1;
}
// function can current status of sensor
int sht_get_status(void)
{
sht_soft_reset();
sht_start();
iobyte = 7;
sht_write_byte();
sht_wait();
ackbit = 1;
sht_read_byte();
return(iobyte);
}
// heater function can used sensor calibration
void sht_heater(void)
{
sht_soft_reset();
sht_start();
iobyte = 6;
sht_write_byte();
iobyte = 4;
sht_write_byte();
ackbit = 1;
sht_read_byte();
delay(500);
sht_soft_reset();
sht_start();
iobyte = 6;
sht_write_byte();
iobyte = 0;
sht_write_byte();
ackbit = 1;
sht_read_byte();
}
//--------------------------------------------------------------------
double calc_dewpoint( double h,double t){
//--------------------------------------------------------------------
// calculates dew point
// input: humidity [%rh], temperature [[ch65533]c]
// output: dew point [[ch65533]c]
float k,dew_point; // local variables
k = (log10(h)-2)/0.4343 + (17.62*t)/(243.12+t);
dew_point = 243.12*k/(17.62-k);
return(dew_point);
}
void printint(long n){
byte buf[10]; // prints 10 digits
byte i=0;
if(n==0)
lcd.print('0');
else{
if(n < 0){
lcd.print('-');
n = -n;
}
while(n>0 && <= 10){
buf[i++] = n % 10; // n % base
n /= 10; // n/= base
}
for(; >0; i--)
lcd.print((char) (buf[i-1] < 10 ? '0' + buf[i-1] : 'a' + buf[i-1] - 10));
}
}
void printdouble( double val, byte precision){
// prints val number of decimal places determine precision
// precision number 0 6 indicating desired decimial places
// example: printdouble( 3.1415, 2); // prints 3.14 (two decimal places)
printint( (long)val); //prints int part
if( precision > 0) {
lcd.print('.'); // print decimal point
unsigned long frac;
unsigned long mult = 1;
byte padding = precision -1;
while(precision--)
mult *=10;
if(val >= 0)
frac = (val - int(val)) * mult;
else
frac = (int(val)- val ) * mult;
unsigned long frac1 = frac;
while( frac1 /= 10 )
padding--;
while( padding--)
lcd.print('0');
printint(frac) ;
}
}
void setup()
{
//setup serial port
serial.begin(baudrate); // open serial
// initialize lcd
lcd.init();
lcd.clear();
//digitalwrite(boardled, low);
//delay(1000);
lcd.cursorto(1,0);
delay(50);
// initialize sht11
lcd.printin("initialize sht11");
pinmode(shtclk, output); // set pin output
digitalwrite(shtclk, high); // set pin high
pinmode(shtdata, output); // set pin output
pinmode(boardled, output); // set pin output
// initialize sensor
lcd.printin("resetting sht11");
sht_connection_reset(); // reset sht11 sensor
lcd.cursorto(2,0);
lcd.printin("reset done");
digitalwrite(boardled, high); // light pin13 led
//delay(1000); // wait 1000 miliseconds
lcd.cursorto(2,0); // move cursor first space on second line
lcd.printin("ready start!");
delay(1000); // wait 1000 miliseconds
digitalwrite(boardled, low); // turn pin13 led off (we ready)
lcd.clear(); // remove visible on display
lcd.cursorto(1,0);
lcd.printin(" humitemp v0.2a");
delay(3000);
}
void loop()
{
mstimer=millis(); // load number of milliseconds since arduino board began running current program mstimer
if (mstimer <= logtime) // if mstimer lower the last time checked run time reset logtime.
{
logtime = 0;
}
if (mstimer > (logtime + 1000)) // if program has been running in more 5 sec fetch new readings sensor
{
logtime = millis(); // recored current program run time
digitalwrite(boardled, high); // light pin13 led
sht_measure(3); // sht11 temperature
digitalwrite(boardled, low); // turn pin13 led off
temp = d1+d2*retval; // temperature conversion according sht11 data sheet
lcd.cursorto(1,0); // move lcd cursor first space on first line
lcd.printin("t:"); // preload lcd text
printdouble(temp,1); // display temp in ascii on lcd
lcd.print(223); // print degree character (223d = 11011111b = cfh)
lcd.printin("c "); // printed right after temperature (no need calculate position)
digitalwrite(boardled, high); // light pin13 led
sht_measure(5); // sht11 humidity
digitalwrite(boardled, low); // turn pin13 led off
humi = c1+c2*retval+c3*(retval*retval); // humidity conversion according sht11 data sheet
lcd.printin("h:"); // preload lcd text
printdouble(humi,1); // display humi in ascii on lcd
lcd.printin("%rh "); // printed right after humidity (no need calculate position)
dewpoint = calc_dewpoint(temp,humi);
lcd.cursorto(2,0); // move lcd cursor first space on second line
lcd.printin("dp:"); //
printdouble(dewpoint,1);
lcd.print(223); // print degree character (223d = 11011111b = cfh)
lcd.printin("c "); // printed right after temperature (no need calculate position)
}
}
thanks in advance.
spelle:
lcd4bit written while ago. there possibility subtle changes in later arduino code causing problem. there similar problem between liquidcrystal library , arduino prior v0018. why don't use arduino v0018 , version of liquidcrystal comes , see if have better luck.
don
lcd4bit written while ago. there possibility subtle changes in later arduino code causing problem. there similar problem between liquidcrystal library , arduino prior v0018. why don't use arduino v0018 , version of liquidcrystal comes , see if have better luck.
don
Arduino Forum > Forum 2005-2010 (read only) > Software > Interfacing > Serial.begin and LCD initialization (+SHT11)
arduino
Comments
Post a Comment