#include <LiquidCrystal.h> ///คำสั่ง #include แล้วตามด้วยไฟล์ที่อยู่ใน Folder ชื่อ Libraries
#include <Wire.h>
#include <OneWire.h>
#include <RTClib.h>
// select the pins used on the Temperature panel
int DS18S20_Pin = A7; //DS18S20 Signal pin on digital A7
//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 2
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
RTC_DS1307 RTC;
// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
// For V1.1 us this threshold
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 650) return btnLEFT;
if (adc_key_in < 850) return btnSELECT;
return btnNONE; // when all others fail, return this...
}
void setup()
{
lcd.begin(16, 2); // start the library
lcd.setCursor(0,0);
Serial.begin(9600);
Wire.begin();
RTC.begin();
}
void loop()
{
//lcd.setCursor(9,1); // move cursor to second line "1" and 9 spaces over
//lcd.print(millis()/1000); // display seconds elapsed since power-up
float temperature = getTemp();
int guess, buttonPressed;
Serial.println(temperature);
lcd.setCursor(0,1); // move to the begining of the second line
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key) // depending on which button was pushed, we perform an action
{
case btnRIGHT:
{
lcd.clear();
lcd.print(" RIGHT ");
delay(500);
break;
}
case btnLEFT:
{
lcd.clear();
lcd.print(" LEFT ");
delay(500);
break;
}
case btnUP:
{
lcd.clear();
lcd.print(" UP ");
delay(500);
break;
}
case btnDOWN:
{
lcd.clear();
lcd.print(" DOWN ");
delay(500);
break;
}
case btnSELECT:
{
///////////////////// select sensor /////////////////
do{
lcd_key = read_LCD_buttons();
lcd.clear();
lcd.print(" Temperature &");
lcd.setCursor(0,1);
lcd.print("Dissolved Oxygen");
switch (lcd_key) // depending on which button was pushed, we perform an action
{
case btnUP:
{
do{
lcd_key = read_LCD_buttons();
lcd.clear();
lcd.print("Temperature : =");//Celsius
delay(300);
float temperature = getTemp();
lcd.setCursor(0,1); //ขึ้นlcdบรรทัด2
lcd.print(" ");
lcd.print(temperature,DEC); //โชว์ค่าอุณหภูมิน้ำ
lcd.print(" Celsius");
delay(500); //just here to slow down the output so it is easier to read
lcd.clear(); //เคียร์หน้าจอ
}while(lcd_key!=btnRIGHT);
lcd.clear();
lcd.print(" save ");
lcd.setCursor(0,1);
lcd.print(" Temperature");
delay(1000);
break;
}
case btnDOWN:
{
do{
lcd_key = read_LCD_buttons();
lcd.clear();
lcd.print("Dissolved Oxygen");
delay(300);
}while(lcd_key!=btnRIGHT);
lcd.clear();
lcd.print(" save ");
lcd.setCursor(0,1);
lcd.print("Dissolved Oxygen");
delay(1000);
break;
}
}
delay(300);
}while(lcd_key!=btnLEFT);
lcd.clear();
lcd.print(" Cancel ");
delay(1000);
break;
///////////////////////////////////////////////////////
}
case btnNONE:
{
time();
break;
}
}
}
void time(void)
{
lcd.clear(); //เคียร์หน้าจอ
DateTime now = RTC.now();
if (now.day() < 10) { Serial.print("0");
lcd.print(0);
}
Serial.print(now.day(), DEC);
Serial.print("/");
lcd.print(now.day(), DEC); //////////lcd day
lcd.print("/");
if (now.month() < 10) { Serial.print("0");
lcd.print(0);
}
Serial.print(now.month(), DEC);
Serial.print("/");
lcd.print(now.month(), DEC); //////////lcd month
lcd.print("/");
Serial.println(now.year(), DEC);
lcd.print(now.year(), DEC); //////////lcd year
lcd.setCursor(0,1); ////set line2
if (now.hour() < 10) { Serial.print("0");
lcd.print(0);
}
Serial.print(now.hour(), DEC);
Serial.print(":");
lcd.print(now.hour(), DEC);
lcd.print(":");
if (now.minute() < 10) { Serial.print("0");
lcd.print(0);
}
Serial.print(now.minute(), DEC);
Serial.print(":");
lcd.print(now.minute(), DEC);
lcd.print(":");
if (now.second() < 10) { Serial.print("0");
lcd.print(0);
}
Serial.println(now.second(), DEC);
lcd.print(now.second(), DEC);
// pause
delay(500);
}
//////////////////////////sensor///////////////////
//void temperature1(void)
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}
#include <LiquidCrystal.h> ///คำสั่ง #include แล้วตามด้วยไฟล์ที่อยู่ใน Folder ชื่อ Libraries
#include <Wire.h>
#include <OneWire.h>
#include <RTClib.h>
// select the pins used on the Temperature panel
int DS18S20_Pin = A7; //DS18S20 Signal pin on digital A7
//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 2
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
RTC_DS1307 RTC;
// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
// For V1.1 us this threshold
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 650) return btnLEFT;
if (adc_key_in < 850) return btnSELECT;
return btnNONE; // when all others fail, return this...
}
void setup()
{
lcd.begin(16, 2); // start the library
lcd.setCursor(0,0);
Serial.begin(9600);
Wire.begin();
RTC.begin();
}
void loop()
{
//lcd.setCursor(9,1); // move cursor to second line "1" and 9 spaces over
//lcd.print(millis()/1000); // display seconds elapsed since power-up
float temperature = getTemp();
int guess, buttonPressed;
Serial.println(temperature);
lcd.setCursor(0,1); // move to the begining of the second line
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key) // depending on which button was pushed, we perform an action
{
case btnRIGHT:
{
lcd.clear();
lcd.print(" RIGHT ");
delay(500);
break;
}
case btnLEFT:
{
lcd.clear();
lcd.print(" LEFT ");
delay(500);
break;
}
case btnUP:
{
lcd.clear();
lcd.print(" UP ");
delay(500);
break;
}
case btnDOWN:
{
lcd.clear();
lcd.print(" DOWN ");
delay(500);
break;
}
case btnSELECT:
{
///////////////////// select sensor /////////////////
do{
lcd_key = read_LCD_buttons();
lcd.clear();
lcd.print(" Temperature &");
lcd.setCursor(0,1);
lcd.print("Dissolved Oxygen");
switch (lcd_key) // depending on which button was pushed, we perform an action
{
case btnUP:
{
do{
lcd_key = read_LCD_buttons();
lcd.clear();
lcd.print("Temperature : =");//Celsius
delay(300);
float temperature = getTemp();
lcd.setCursor(0,1); //ขึ้นlcdบรรทัด2
lcd.print(" ");
//lcd.print(temperature,DEC);
float temp;
temp=temperature;
lcd.print(("%.2f",temp)); แก้ดูแล้วครับ ผลออกมาเป็นทศยม2ตำแหน่ง
lcd.print(" Celsius");
delay(500); //just here to slow down the output so it is easier to read
lcd.clear(); //เคียร์หน้าจอ
}while(lcd_key!=btnRIGHT);
lcd.clear();
lcd.print(" save ");
lcd.setCursor(0,1);
lcd.print(" Temperature");
delay(1000);
break;
}
case btnDOWN:
{
do{
lcd_key = read_LCD_buttons();
lcd.clear();
lcd.print("Dissolved Oxygen");
delay(300);
}while(lcd_key!=btnRIGHT);
lcd.clear();
lcd.print(" save ");
lcd.setCursor(0,1);
lcd.print("Dissolved Oxygen");
delay(1000);
break;
}
}
delay(300);
}while(lcd_key!=btnLEFT);
lcd.clear();
lcd.print(" Cancel ");
delay(1000);
break;
///////////////////////////////////////////////////////
}
case btnNONE:
{
time();
break;
}
}
}
void time(void)
{
lcd.clear(); //เคียร์หน้าจอ
DateTime now = RTC.now();
if (now.day() < 10) { Serial.print("0");
lcd.print(0);
}
Serial.print(now.day(), DEC);
Serial.print("/");
lcd.print(now.day(), DEC); //////////lcd day
lcd.print("/");
if (now.month() < 10) { Serial.print("0");
lcd.print(0);
}
Serial.print(now.month(), DEC);
Serial.print("/");
lcd.print(now.month(), DEC); //////////lcd month
lcd.print("/");
Serial.println(now.year(), DEC);
lcd.print(now.year(), DEC); //////////lcd year
lcd.setCursor(0,1); ////set line2
if (now.hour() < 10) { Serial.print("0");
lcd.print(0);
}
Serial.print(now.hour(), DEC);
Serial.print(":");
lcd.print(now.hour(), DEC);
lcd.print(":");
if (now.minute() < 10) { Serial.print("0");
lcd.print(0);
}
Serial.print(now.minute(), DEC);
Serial.print(":");
lcd.print(now.minute(), DEC);
lcd.print(":");
if (now.second() < 10) { Serial.print("0");
lcd.print(0);
}
Serial.println(now.second(), DEC);
lcd.print(now.second(), DEC);
// pause
delay(500);
}
//////////////////////////sensor///////////////////
//void temperature1(void)
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}