L(dr) and P(ir) readings
[Arduino] / DHT11_DS18B20_temperature / DHT11_DS18B20_temperature.ino
index b0b8c2c..c5fb8da 100644 (file)
@@ -68,16 +68,59 @@ OneWire oneWire(ONE_WIRE_BUS);
 // Pass our oneWire reference to Dallas Temperature. 
 DallasTemperature sensors(&oneWire);
 
+
+#include <Adafruit_GFX.h>
+#include <Adafruit_PCD8544.h>
+
+// pin 7 - Serial clock out (SCLK)
+// pin 6 - Serial data out (DIN)
+// pin 5 - Data/Command select (D/C)
+// pin 4 - LCD chip select (CS)
+// pin 3 - LCD reset (RST)
+Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
+
+#define Backlight_Pin 9
+
 void setup()
 {
   Serial.begin(9600);
-  Serial.println("DHT11 TEST PROGRAM ");
-  Serial.print("LIBRARY VERSION: ");
+  Serial.println("DHT11 DS18B20 temperature");
+  Serial.print("DHT11 LIBRARY VERSION: ");
   Serial.println(DHT11LIB_VERSION);
   Serial.println();
+
+  // DS18B20
   sensors.begin();
+  
+  // Nokia 5110 display
+  display.begin();
+
+  // you can change the contrast around to adapt the display
+  // for the best viewing!
+  display.setContrast(50);
+
+  pinMode(Backlight_Pin, OUTPUT);
+
+  pinMode(0, INPUT);
+  Serial.print("Backlight ");
+  Serial.println(analogRead(0));
+
+  display.clearDisplay();
+  display.setTextSize(1);
+  display.setTextColor(BLACK);
+  display.setCursor(0,0);
+  display.println("Ready");
+  display.display();
 }
 
+float dht11_temperature = 0;
+float dht11_humidity = 0;
+float ds18b20_temperature = 0;
+
+#define TEMP_SIZE LCDWIDTH
+float temp[TEMP_SIZE] = {0.0};
+int temp_pos = 0; // position in circular buffer above
+
 void loop()
 {
   Serial.println("\n");
@@ -101,11 +144,13 @@ void loop()
                break;
   }
 
+  dht11_humidity = (float)DHT11.humidity;
   Serial.print("Humidity (%): ");
-  Serial.println((float)DHT11.humidity, 2);
+  Serial.println(dht11_humidity, 2);
 
+  dht11_temperature = (float)DHT11.temperature;
   Serial.print("Temperature (oC): ");
-  Serial.println((float)DHT11.temperature, 2);
+  Serial.println(dht11_temperature, 2);
 
   Serial.print("Temperature (oF): ");
   Serial.println(Fahrenheit(DHT11.temperature), 2);
@@ -126,9 +171,67 @@ void loop()
 
   sensors.requestTemperatures(); // Send the command to get temperatures
   Serial.print("Temperature for the device 1 (index 0) is: ");
-  Serial.println(sensors.getTempCByIndex(0));  
+  ds18b20_temperature = sensors.getTempCByIndex(0);
+  Serial.println(ds18b20_temperature);  
+
+  temp[temp_pos] = ds18b20_temperature;
+
+  display.clearDisplay();
+  display.setCursor(0,0);
+  display.print(dht11_temperature, 0);
+  display.print("C ");
+  display.print(dht11_humidity, 0);
+  display.print("% ");
+  display.print(ds18b20_temperature, 2);
+  display.print("C");
+
+  float min = temp[0], max = temp[0];
+  
+  for(int i = 0; i < TEMP_SIZE; i++) {
+//    Serial.print(temp[i]);
+//    Serial.print(" ");
+    if (temp[i] < min && temp[i] > 0) min = temp[i];
+    if (temp[i] > max) max = temp[i];
+  }
+  Serial.println();
   
+  Serial.print("temperature range ");
+  Serial.print(min);
+  Serial.print("-");
+  Serial.println(max);
+
+  // draw right to left so most recent value is on the right
+  for(int x = TEMP_SIZE - 1; x >= 0; x--) {
+    int pos = ( x + temp_pos + 1 ) % TEMP_SIZE;
+    if ( temp[pos] > 0 ) {
+      int y = ( ( temp[pos] - min ) / ( max - min ) ) * ( LCDHEIGHT - 10 );
+      display.drawLine(x, LCDHEIGHT - y, x, LCDHEIGHT, BLACK);
+//      display.drawPixel(x,y + 10, BLACK);
+//      Serial.print(temp[pos],2);
+//      Serial.print(" ");
+    }
+  }
+  Serial.println();
+
+  // refresh LCD
+  display.display();
+
+  // pulse display backlight
+  int backlight = 0;
+
+  float old_temp = temp[(temp_pos + TEMP_SIZE - 1) % TEMP_SIZE];
+  if ( ds18b20_temperature < old_temp ) {
+    backlight = 32;
+  } else if ( ds18b20_temperature > old_temp ) {
+    backlight = 255;
+  }
+  analogWrite(Backlight_Pin, backlight);
+
   delay(2000);
+
+  // move slot in circular bugger
+  if ( ++temp_pos > TEMP_SIZE ) temp_pos = 0;
+
 }
 //
 // END OF FILE