Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- #define DEBUG 1
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
- int Temp;
- int Light, Humidity, hCheck;
- boolean Tilt;
- boolean doTemp = true;
- boolean doLight = true;
- boolean doHumi = true;
- boolean doTilt = true;
- boolean colonOn = false;
- int dtime = 1;
- #define tempPin 0
- #define lightPin 1
- #define tiltPin 6
- #define humidityPin 7
- #include <dht11.h>
- dht11 DHT11;
- // initialise LCD library and pins
- void setup() {
- lcd.begin(16, 2);
- Serial.begin(115200);
- pinMode(tempPin, INPUT);
- pinMode(lightPin, INPUT);
- pinMode(tiltPin, INPUT);
- pinMode(humidityPin, INPUT);
- pinMode(13, OUTPUT);
- Temp = 0;
- Light = 0;
- Tilt = 0;
- Humidity = 0;
- hCheck = -2;
- digitalWrite(13, HIGH);
- delay(200);
- digitalWrite(13, LOW);
- delay(200);
- digitalWrite(13, HIGH);
- delay(200);
- digitalWrite(13, LOW);
- delay(200);
- digitalWrite(13, HIGH);
- delay(200);
- digitalWrite(13, LOW);
- delay(200);
- }
- void loop()
- {
- if (doTemp)
- {
- //pds("reading temp pin");
- analogRead(tempPin);
- delay(10);
- Temp = convertTemp(analogRead(tempPin)); // ;
- delay(10);
- }
- if(doLight)
- {
- //pds("reading light pin");
- analogRead(lightPin);
- delay(10);
- Light = toLux(analogRead(lightPin));
- delay(10);
- }
- if(doHumi)
- {
- //pds("reading humidity");
- hCheck = DHT11.read(humidityPin);
- //Serial.println(String(hCheck));
- //Serial.println(String(DHT11.humidity));
- if(hCheck != 0)
- Humidity = 255; //Must be an error
- else
- Humidity = DHT11.humidity;
- }
- if(doTilt)
- {
- //pds("reading tilt pin");
- Tilt = (digitalRead(tiltPin) == LOW);
- }
- /*
- char charBuf[5];
- dtostrf(Temp, 1, 2, charBuf);
- */
- //Serial.println("TEMP:" + String(Temp));
- //Serial.println("LIGH:" + String(Light));
- //Serial.println("HUMI:" + String(Humidity));
- //Sermial.println("TILT:" + String(Tilt));
- /*
- */
- Temp = GetAvgTemp(Temp);
- showData(Temp, Light, Humidity, Tilt);
- Serial.println(String(Temp) + ":" + String(Light) + ":" + String(Humidity));
- //pds("delay started for " + String(dtime) + " seconds");
- delay(dtime * 1000);
- //pds("delay complete");
- }
- float convertTemp(float reading)
- {
- return (5.0 * reading * 100.0) / 1024.0;
- }
- // Change the ADC reading to Lux. Assumes a 10K pull down resistor and 5V
- int toLux(int adc)
- {
- if (adc > 868)
- return 300 + 5.6 * (adc - 868);
- else if (adc > 853)
- return 250 + 3.46 * (adc - 853);
- else if (adc > 788)
- return 150 + 1.52 * (adc - 788);
- else if (adc > 731)
- return 120 + 0.53 * (adc - 731);
- else if (adc > 683)
- return 80 + 0.82 * (adc - 683);
- else if (adc > 256)
- return 10 + 0.16 * (adc - 256);
- else
- return 0.04 * adc;
- }
- //void showData (float temp, int light, int humidity, boolean tilt)
- //{
- // String s1, s2, s3, s4;
- //
- // char charBuf[5];
- //
- // dtostrf(temp, 5, 2, charBuf);
- // if(temp == 0.0)
- // {
- // Serial.println("<d><temp>0.0</temp><light>" + String(light) + "</light><hum>" + String(humidity) + "</hum><tilt>" + String(tilt) + "</tilt></d>");
- // s1 = "00.00" + String(char(0xdf)) + "C";
- // }
- // else
- // {
- // Serial.println("<d><temp>" + String(charBuf) + "</temp><light>" + String(light) + "</light><hum>" + String(humidity) + "</hum><tilt>" + String(tilt) + "</tilt></d>");
- // s1 = String(charBuf) + String(char(0xdf)) + "C";
- // }
- // pds(s1);
- // //printTemp(s1);
- // pds("s1 printed");
- // s2 = String (light) + "Lux";
- // pds(s2);
- // //printLux(s2);
- // pds("s2 printed");
- //// lcd.setCursor(5,0);
- //// lcd.print(lcdSanitise(lcdMakeFirstLineEnd(String(char(0xdf)) + "C", s2)));
- // if(humidity == 255)
- // s3 = "ERROR";
- // else
- // s3 = String(humidity) + "%";
- //
- // pds(s3);
- // printHum(s3);
- // pds("s3 printed");
- // if (tilt)
- // s4 = "TILT";
- // else
- // s4 = "FLAT";
- // pds(s4);
- // //printTilt(s4);
- // pds("s4 printed");
- // //lcd.setCursor(0,1);
- // //lcd.print(lcdSanitise(lcdMakeLine(s1, s2)));
- //}
- void printTemp(String temp)
- {
- lcd.setCursor(0,0);
- lcd.print(temp);
- lcd.setCursor(5,0);
- lcd.print(String(char(0xdf)) + "C");
- }
- void printLux(String light)
- {
- lcd.setCursor(16 - light.length(),0);
- lcd.print(light);
- }
- void printHum(String humidity)
- {
- lcd.setCursor(0,1);
- lcd.print(humidity.length());
- // lcd.setCursor(0,0);
- // lcd.print(humidity);
- }
- void printTilt(String tilt)
- {
- lcd.setCursor(12,1);
- lcd.print(tilt);
- }
- String lcdMakeFirstLineEnd(String s1, String s2)
- {
- String spaces = " ";
- return s1 + spaces.substring(0, 11 - s1.length() - s2.length()) + s2;
- }
- String lcdMakeLine(String s1, String s2)
- {
- String spaces = " ";
- return s1 + spaces.substring(0, 16 - s1.length() - s2.length()) + s2;
- }
- String lcdSanitise(String textIn)
- {
- if (textIn.length() > 16)
- {
- return textIn.substring(0,16);
- }
- return textIn;
- }
- void pds(String s)
- {
- #ifdef DEBUG
- Serial.println("DEBUG: " + s);
- // More debug code...
- #endif
- }
- int prevTemps[10];
- void arrayRemoveAndShift()
- {
- for(int i = 8; i>=0; i--)
- {
- prevTemps[i+1] = prevTemps[i];
- }
- prevTemps[0] = 0;
- }
- void arrayInsert(int i)
- {
- prevTemps[0] = i;
- }
- int GetAvgTemp(int currentReading)
- {
- arrayRemoveAndShift();
- arrayInsert(currentReading);
- int avg = 0;
- int nonZeroVals = 0;
- for(int i = 9; i>=0; i--)
- {
- if(prevTemps[i] != 0)
- {
- nonZeroVals++;
- }
- }
- if (nonZeroVals > 0)
- {
- for (int i = 0; i < nonZeroVals; i++)
- {
- // Serial.print(String( prevTemps[i]) + ", ");
- avg += prevTemps[i];
- }
- // Serial.println("- " + String(avg / nonZeroVals));
- }
- else
- {
- return 0;
- }
- return avg / nonZeroVals;
- }
- void showData (int temp, int light, int humidity, boolean tilt)
- {
- String s1, s2, s3;
- String spaces = " ";
- s1 = String(temp) + char(0xdf) + "C";
- s2 = String (light) + "Lux";
- s3 = s1 + spaces.substring(0, 16 - s1.length() - s2.length()) + s2;
- lcd.setCursor(0,0);
- lcd.print(s3);
- if(humidity == 255)
- s1 = "ERROR";
- else
- s1 = String(humidity) + "%";
- if (tilt)
- s2 = "TILT";
- else
- s2 = "FLAT";
- s3 = s1 + spaces.substring(0, 16 - s1.length() - s2.length()) + s2;
- lcd.setCursor(0,1);
- lcd.print(s3);
- }
Add Comment
Please, Sign In to add comment