Arduinoを使って温湿度計&水温計 二個目

 

もう一つの水槽用に二個目の温湿度計+水温計を作る。

構成は一個目とほぼ同じだが、LCDが違うのでスケッチも若干違っている。

 

買ったのは

SODIAL(R)キーパッド シールド ボード ブルー バックライト Arduinoロボット LCD 1602 1280 2560 UNO US用品

Amazon CAPTCHA

LCDにボタンが6個ついたもの。Arduino Unoにそのまま取り付けられる

シールドと呼ばれる形式。

前回のI2C変換を使って配線を減らしたかったけど、ピンアサインが

良くワカランのでそのまま使うことにした。

 

で、せっかくなのでボタン使って何かしようかと思ったけど、パッと

思いつかないので後回し。

スケッチもキーの読み取りとかいろいろコメントアウト

 


#include <LiquidCrystal.h>
#include <LCDKeypad.h>
#include "DHT.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#define DHTPIN 3
#define DHTTYPE DHT11
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);           // select the pins used on the LCD panel

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);


DHT dht(DHTPIN, DHTTYPE);

// 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

#define DELTA 10

int cnt=0;

 
int read_LCD_buttons(){               // read the 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
    // We make this the 1st option for speed reasons since it will be the most likely result
 
    if (adc_key_in > 1000) return btnNONE;
 
    // 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; 
 
   // For V1.0 comment the other threshold and use the one below:
   /*
     if (adc_key_in < 50)   return btnRIGHT; 
     if (adc_key_in < 195)  return btnUP;
     if (adc_key_in < 380)  return btnDOWN;
     if (adc_key_in < 555)  return btnLEFT;
     if (adc_key_in < 790)  return btnSELECT;  
   */
 
    return btnNONE;                // when all others fail, return this.
}
 unsigned char bckl=150;
 
void setup(){
    pinMode(10, OUTPUT);
    analogWrite(10, bckl);
   lcd.begin(16, 2);               // start the library
  // lcd.setCursor(0,0);             // set the LCD cursor   position
  // lcd.print("Push the buttons");  // print a simple message on the LCD

//   Serial.begin(9600);
//  Serial.println("DHT11 test!");
  dht.begin();
    // Start up the library
  sensors.begin();
}

 

void loop(){
    //adjust the backlight


 // delay(3000);

  int h = dht.readHumidity();
  int t = dht.readTemperature();
  float wt = sensors.getTempCByIndex(0);

  /*
    // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");

  Serial.print("Temperature for the device 1 (index 0) is: ");
  Serial.println(sensors.getTempCByIndex(0));

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.println(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C ");
  */
 
/*
if(wt<30){
  lcd.blink();
  }else{
    lcd.noBlink();
  }
*/
 sensors.requestTemperatures(); // Send the command to get temperatures
 
  //lcd.print("ROOM TEMP:");
  lcd.clear();
  lcd.print("Hu:");
  lcd.print(h);
  lcd.print("%");
  lcd.setCursor(8,0);
  lcd.print("T:");
  lcd.print(t);
  lcd.print("*C ");
 
  //print water temp
  lcd.setCursor(0,1);
  if(wt<25){
    if(cnt==0){
    lcd.print("WARNIG!");
    lcd.print(sensors.getTempCByIndex(0));
    lcd.print(" *C ");
    cnt=1;
    }else{
       cnt=0;
             }
  }else{
      lcd.print("WATER: ");
      lcd.print(sensors.getTempCByIndex(0));
      lcd.print("*C ");
  }
  delay(500);
       
}

 

とりあえず動いた。

f:id:woosan900rr:20160820235054j:plain

 

こちらも忘れないうちに配線図を追加。

f:id:woosan900rr:20160904003927p:plain