#include // vim:set ft=arduino: // DS18S20 Temperature chip i/o #define ONEWIRE 8 #define OONEWIRE 9 #define LIGHT 0 float LMAX = 1023; float LMIN = 0; // Pressure sensor // define spi bus pins #define SLAVESELECT 10 // CSB #define SPICLOCK 13 // SCK #define DATAOUT 11 //MOSI #define DATAIN 12 //MISO #define UBLB(a,b) ( ( (a) << 8) | (b) ) #define UBLB19(a,b) ( ( (a) << 16 ) | (b) ) //Addresses #define REVID 0x01 //ASIC Revision Number #define OPSTATUS 0x04 //Operation Status #define STATUS 0x07 //ASIC Status #define START 0x0A //Constant Readings, high resolution #define START 0x0B // Low power mode #define PRESSURE 0x1F //Pressure 3 MSB #define PRESSURE_LSB 0x20 //Pressure 16 LSB #define TEMP 0x21 //16 bit temp // Light sensor #define TSL_FREQ_PIN 2 // output use digital pin2 for interrupt #define TSL_S0 3 #define TSL_S1 5 #define TSL_S2 4 #define TSL_S3 6 #define READ_TM 1000 // milleseconds between frequency calculations // Pressure variables char rev_in_byte; int temp_in; unsigned long pressure_lsb; unsigned long pressure_msb; unsigned long temp_pressure; unsigned long pressure; // Light variables int calcSensitivity; unsigned long sensitivityHighThresh = 2000; unsigned long sensitivityLowThresh = 100000; unsigned long pulseCount = 0; unsigned long currentTime = millis(); unsigned long startTime = currentTime; unsigned int tm_diff = 0; // freq will be modified by the interrupt handler so needs to be volatile // freq holds the latest frequency calculation unsigned long frequency; float uWattCm2; volatile unsigned long curPulseCount; unsigned int count = 0; unsigned int scale; // holds the TLS scale value, see below // Temperature variables OneWire ds(ONEWIRE); // pin 8, outdoor, 3 temps OneWire ds2(OONEWIRE); // pin 9, indoor, 2 temps int HighByte, LowByte, TReading, SignBit, Tc_100, Tf_100, Whole, Fract; int i; byte data[12]; void setup(void) { // Pressure setup byte clr; pinMode(DATAOUT, OUTPUT); pinMode(DATAIN, INPUT); pinMode(SPICLOCK,OUTPUT); pinMode(SLAVESELECT,OUTPUT); digitalWrite(SLAVESELECT,HIGH); //disable device SPCR = (1<= READ_TM ) { curPulseCount = pulseCount; // use curPulseCount for calculating freq/uW pulseCount = 0; startTime = millis(); } } long getUwattCm2() { // copy pulse counter and multiply. // the multiplication is necessary for the current // frequency scaling level. frequency = curPulseCount * scale; // get uW observed - assume 640nm wavelength // calc_sensitivity is our divide-by to map to a given signal strength // for a given sensitivity (each level of greater sensitivity reduces the signal // (uW) by a factor of 10) float uw_cm2 = (float) frequency / (float) calcSensitivity; // extrapolate into entire cm2 area uWattCm2 = uw_cm2 * ( (float) 1 / (float) 0.0136 ); return(uWattCm2); } void setSensitivity() { getUwattCm2(); if (uWattCm2 < sensitivityHighThresh) { sensitivity(3); return; } if (uWattCm2 > sensitivityLowThresh ) { sensitivity(1); return; } sensitivity(2); } void sensitivity(uint8_t level) { switch (level) { case 1: digitalWrite(TSL_S0, HIGH); // S0 HIGH and S1 LOW = 1x sensitivity digitalWrite(TSL_S1, LOW); calcSensitivity = 10; break; case 2: digitalWrite(TSL_S0, LOW); // S0 LOW and S1 HIGH = 10x sensitivity digitalWrite(TSL_S1, HIGH); calcSensitivity = 100; break; case 3: digitalWrite(TSL_S0, HIGH); // S0 HIGH and S1 HIGH = 100x sensitivity digitalWrite(TSL_S1, HIGH); calcSensitivity = 1000; break; } return; } // Temp functions void flash_led(int led, int dly) { digitalWrite(led, HIGH); delay(dly); digitalWrite(led, LOW); } float scale_light(float mn, float mx, int v) { // Returns a float between mn and mx // return ((v - mn) / (mx - mn)); return ((v - mn) / (mx - mn)); } int get_c(OneWire *d) { byte i; for ( i = 0; i < 9; i++) { // we need 9 bytes data[i] = d->read(); } LowByte = data[0]; HighByte = data[1]; TReading = (HighByte << 8) + LowByte; SignBit = TReading & 0x8000; // test most sig bit if (SignBit) { // negative TReading = (TReading ^ 0xffff) + 1; // 2's comp } Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25 return(Tc_100); } void write_temp(int Tc_100) { Whole = Tc_100 / 100; // separate off the whole and fractional portions Fract = Tc_100 % 100; if (SignBit) { Serial.print("-"); } Serial.print(Whole); Serial.print("."); if (Fract < 10) { Serial.print("0"); } Serial.print(Fract); Serial.print(","); Tf_100 = ((Tc_100 * 9) / 5) + 3200; Whole = Tf_100 / 100; // separate off the whole and fractional portions Fract = Tf_100 % 100; if (SignBit) { Serial.print("-"); } Serial.print(Whole); Serial.print("."); if (Fract < 10) { Serial.print("0"); } Serial.print(Fract); } void write_pres(unsigned long pres) { Whole = pres / 100; // separate off the whole and fractional portions Fract = pres % 100; if (SignBit) { Serial.print("-"); } Serial.print(Whole); Serial.print("."); if (Fract < 10) { Serial.print("0"); } Serial.print(Fract); }