Dear Groove,
As the script is to large for one post i have to post it at maybe three posts.
sec. one
char sMeasure (unsigned char pValue, unsigned char command,
unsigned char singleFlag, unsigned int dataport)
{
unsigned char error; / holds return value from routine /
unsigned int ix; / used for 'wait for data' loop */
unsigned char ch, crc, revCRC;
crc = 0; /* Initialize CRC to zero /
sTransmitStart (dataport); / Start transmission of command /
error = sWriteByte (command, dataport); / Send the requested command /
/ Note that sWriteByte leaves DATA in input mode /
doCRC (command, &crc); / Include command in CRC /
if (debug) {
Serial.print("After 'command': CRC is 0x");
Serial.println(crc, HEX);
}
for (ix = 0; ix < 240; ix++){
delay(1); / This delay is needed for long cables /
if (!digitalRead (dataport))
break;
}
if (digitalRead (dataport))
Serial.println("DATA did not go low after writing command");
if (!singleFlag) { / If a 2-byte reply /
ch = sReadByte (ACK, dataport); / Read MSB of data /
doCRC (ch, &crc); / Include in CRC */
if (debug) {
Serial.print("After MSB: CRC is 0x");
Serial.println(crc, HEX);
}
(pValue + 1) = ch; / Store MSB byte /
}
ch = sReadByte (ACK, dataport); / Read LSB of data /
doCRC (ch, &crc); / Include in CRC */
pValue = ch; / Store LSB byte /
if (debug) {
Serial.print("After LSB: CRC is 0x");
Serial.println(crc, HEX);
}
ch = sReadByte (NOACK, dataport); / Read msg CRC, don't send ACK */
revCRC = 0;
for (ix = 0; ix < 8; ix++) {
if ((0x80 >> ix) & ch)
revCRC |= (1 << ix);
}
if (debug) {
Serial.print("After Checksum: CRC is 0x");
Serial.print(crc, HEX);
Serial.print(", received value was 0x");
Serial.println(revCRC, HEX);
}
if (crc != revCRC) {
Serial.print("CRC error in reply (command was 0x");
Serial.print(command, HEX);
Serial.print("CRC is 0x");
Serial.print(crc, HEX);
Serial.print(", received value was 0x");
Serial.println(revCRC, HEX);
Serial.println(") - resetting SHT1x connection");
sConnectionReset(dataport);
}
return error;
}
/**
- calcTempHumid
-
- Routine to calculate the "true" temperature and humidity based upon
- the "tick" values read from the SHT1x. The SHT1x is set to operate in
- 12-bit mode for humidity, and 14-bit mode for temperature. The
- conversion constants are taken from the SHT1x datasheet, assuming a
- supply voltage of 5.0V.
-
- Parameters:
-
@pHumidity pointer to humidity value
-
@pTemperature pointer to temperature value
-
- Returns:
- Input values of temperature and humidity are overwritten with their
- calculated "true" values.
*/
void calcTempHumid (float *pHumidity, float pTemperature)
{
/ Constants for conversion of reading to relative humidity /
#define C1 -4.0
#define C2 +0.0405
#define C3 -0.0000028
/ Constants for temperature-compensated relative humidity /
#define T1 +0.01
#define T2 +0.00008
/ Constants for conversion of temperature reading to Centigrade */
//#define D1 -40.10 //5.0V operation
#define D1 -39.651 //3.3V operation
#define D2 +0.01
float rh = pHumidity; / relative humidity (input value) /
float rhLin; / Linear value of humidity /
float rhTrue; / Temperature-compensated humidity value */
float t = pTemperature; / input value for temperature /
float tC; / Temperature converted to Celsius */
tC = D1 + (t * D2); /* Linear conversion of temperature /
rhLin = (C3 * rh * rh) + (C2 * rh) + C1; / "ticks" to relative H /
rhTrue = (tC - 25) * (T1 + (T2 * rh)) + rhLin;
/ Assure our relative humidity isn't out of range (> 100% or < 0.1%) /
if (rhTrue > 100.0)
rhTrue = 100.0;
else if (rhTrue < 0.1)
rhTrue = 0.1;
/ Finally, return the calculated values */
*pTemperature = tC;
*pHumidity = rhTrue;
}
/**
- calcDewPoint
-
- Routine to calculate the dew point based upon relative humidity
- and temperature. I have no idea what it's doing, but since it
- comes from the Sensirion literature, it's probably correct :-).
-
- Parameters:
-
@humidity value of relative humidity
-
@temperature value of temperature
-
- Returns:
- Calculated dew point
*/
float calcDewPoint (float humidity, float temperature)
{
float logEx;
logEx = 0.66077 + (7.5 * temperature) / (237.3 + temperature)
- (log10(humidity) - 2);
return (logEx - 0.66077) * 237.3 / (0.66077 + 7.5 - logEx);
}
/**
- splitFloat
-
- This routine takes a float as input and returns the integer part and
- the fractional part, to the number of decimal places specified. The
- only reason I wrote it is because I couldn't find any existing routine
- to print out a float in a reasonable format (with decimal places)
-
- Parameters:
-
@fNum The floating point number to be dissected
-
@pInt Pointer to an integer to contain the integer part
-
@pFrac Pointer to a string to contain the fraction
- Note: the caller must assure the string is large enough
-
@decPlaces Number of decimal places for the operation
-
- Returns:
- The values calculated are placed in the locations specified.
*/
void splitFloat (float *fNum, int *pInt, char *pFrac, int decPlaces) {
int ix;
int frac;
float fVal;
/* Round the input according to precision, plus fudge for noise */
fVal = *fNum + (0.5 * pow(10.0, (float)(-decPlaces))) + 0.00001;
pInt = fVal; / Return truncated integer value /
/
- Now isolate just the fractional part of the original number
*/
fVal = fVal - (float)(pInt); / Remove the integral part /
/ Convert the fraction into a simple integer /
frac = fVal * pow (10.0, (float)decPlaces);
/ Now format it as a leading-zero string of digits /
pFrac += decPlaces; / point to string terminator position */
pFrac-- = 0; / put in terminator */
for (ix = 0; ix < decPlaces; ix++) {
pFrac-- = (frac % 10) | 0x30; / put in digits in reverse order */
frac /= 10;
}
}
/**
- printReading
-
- Routine to print out the value of caculated data, using a common format
- of {label} {int value}.{single digit fraction}{suffix}
-
- Parameters:
-
@label string for starting label
-
@pVal pointer to float value to display
-
@suffix string to append to value
-
- Returns:
- nothing
*/
void printReading (char *label, float *pVal, char *suffix) {
int num;
char str[10];
splitFloat (pVal, &num, str, 2);
Serial.print(label);
Serial.print(num, DEC);
Serial.print(".");
Serial.print(str);
Serial.println(suffix);
}
/**
- setup
-
- This is the routine required by the Arduino software to initialize
- anything needed by the main loop. We set up the DATA and CLOCK ports
- to a beginning state.
*/
void setup()
{
int fTemperature;
lcd.begin(4,16); // LCD rows and colomns
// lcd.init();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temperatuur:");
lcd.setCursor(0,1);
// 1234567890
lcd.print("Vochtigheid:");
lcd.setCursor(-4,2);
// 1234567890
lcd.print("Dauwpunt:");
lcd.setCursor(-4,3);
lcd.print("Testprint:");
Serial.begin(4800); /* Open Arduino serial communications was 9600 veranderd dus */
pinMode (CLOCK, OUTPUT);
pinMode (DATA, OUTPUT); // aan gezet
for (int i = 3; i < 10; i++){
sConnectionReset(i);
}
Serial.println("Acceptable commands:");
Serial.println("d or D: Toggle debug flag for printing intermediate data");
Serial.println("s or S: Read status register");
Serial.println("3 - 9 : Read in temperature and humidity\n");
}
void loop()
{
char cmd; /* command input by user /
int humidVal; / humidity value read from SHT75 /
int tempVal; / temperature value from SHT75 /
unsigned char statusVal; / contents of status register /
float fHumidity; / working value for humidity calculation /
float fTemperature; / working value for temperature calculation /
float dewPoint; / calculated Dew Point value /
unsigned char error; / return value for routi