#include <OneWire.h>
#include <DallasTemperature.h>
//#include <U8glib.h>
//---
//U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE);
//----
const int SENSOR_PIN = 8; // Arduino pin connected to DS18B20 sensor's DQ pin
int sensor = 2; // the pin that the sensor is atteched to
int state = LOW; // by default, no motion detected
int val = 0;
OneWire oneWire(SENSOR_PIN); // setup a oneWire instance
DallasTemperature tempSensor(&oneWire); // pass oneWire to DallasTemperature library
float tempCelsius; // temperature in Celsius
float tempFahrenheit; // temperature in Fahrenheit
void setup()
{
pinMode(sensor, INPUT);
Serial.begin(9600); // initialize serial
tempSensor.begin(); // initialize the sensor
}
void loop()
{
tempSensor.requestTemperatures(); // send the command to get temperatures
tempCelsius = tempSensor.getTempCByIndex(0); // read temperature in Celsius(获取索引号0的传感器摄氏温度数据)
tempFahrenheit = tempCelsius * 9 / 5 + 32; // convert Celsius to Fahrenheit
Serial.print("Temperature: ");
Serial.print(tempCelsius); // print the temperature in Celsius
Serial.print("°C");
Serial.print(" ~ "); // separator between Celsius and Fahrenheit
Serial.print(tempFahrenheit); // print the temperature in Fahrenheit
Serial.println("°F");
val = digitalRead(sensor);
if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH;
}else{
Serial.println("Motion stopped!");
state = LOW;
}
/*
u8g.setFont(u8g_font_unifont);
u8g.setPrintPos(0, 15);
u8g.print("0.91 OELD");
u8g.setPrintPos(0,30);
u8g.print("hello world");
delay(500);*/
delay(1000);
}