DHT11温湿度传感器单独测试时正常,一合并代码就出错!-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 354|回复: 0

[未解决] DHT11温湿度传感器单独测试时正常,一合并代码就出错!

[复制链接]
发表于 2022-4-23 17:05 | 显示全部楼层 |阅读模式
求大佬们帮忙看一下
#include <DHT.h>
#include <DHT_U.h>
#include <DS3231.h>
#include <Servo.h>
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Wire.h>

#define DHTTYPE DHT11   // DHT 11
#define DHTPIN A5
DHT dht(DHTPIN, DHTTYPE);


    // 定义矩阵键盘
const byte ROWS = 4; // 四行
const byte COLS = 4; //四列
char keys[ROWS][COLS] = {
  {'1','2','3',':'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = { 2, 3, 4, 5 };// 将键盘 ROW0、ROW1、ROW2 和 ROW3 连接到2,3,4,5Arduino 引脚。
byte colPins[COLS] = { 6, 7, 8, 9 };// 将键盘 COL0、COL1 、COL2和COL3 连接到6,7,8,9 Arduino 引脚
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );      //  创建矩阵键盘

DS3231  rtc(SDA, SCL);
Servo servo_test;      //initialize a servo object for the connected servo  
LiquidCrystal lcd(A0, A1, A2, 11, 12, 13); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)

//int angle = 0;   
// int potentio = A0;      // 初始化电位器的 A0 模拟引脚

int t1, t2, t3, t4, t5, t6;
boolean feed = true;    // 闹钟条件
char key;
int r[6];

void setup()
{
  dht.begin();
  servo_test.attach(10);   // 将伺服电机的信号引脚连接到arduino的pin10
  Serial.println();
  rtc.begin();
  lcd.begin(16,2);
  servo_test.write(55);
  Serial.begin(115200);
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
}



void setFeedingTime()
{
  feed = true;
   int i=0;

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Set feeding Time");
   delay(400);  
  lcd.clear();
  lcd.print("HH:MM");
  lcd.setCursor(0,1);
      char j = 0;

  while(1)
  {
    key = kpd.getKey();
    if(key!=NO_KEY){
      lcd.setCursor(j,1);
      lcd.print(key);
      r[i] = key;
      i++;
      j++;
      if (j == 2)
      {
        lcd.print(":");
        j++;
      }
      delay(500);
    }
    if (key == 'D')
    {
      key=0; break;
    }
  }
}

void loop()
{
  lcd.setCursor(0,0);
  int buttonPress;
  buttonPress = digitalRead(A3);
  if (buttonPress==1)
   setFeedingTime();
  Serial.println(buttonPress);
  
   lcd.print("Time:  ");
   String t = "";
   t = rtc.getTimeStr();
   t1 = t.charAt(0);
   t2 = t.charAt(1);
   t3 = t.charAt(3);
   t4 = t.charAt(4);
   t5 = t.charAt(6);
   t6 = t.charAt(7);
   
   lcd.print(rtc.getTimeStr());
   delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float te = dht.readTemperature();
   if (isnan(h) || isnan(te))
   {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }
  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(te);
  Serial.print(F("°C "));
  

   lcd.setCursor(0,1);
   lcd.print("T:");
   lcd.print(te);
   lcd.print("C");
   lcd.print("H:");
   lcd.print(h);
   lcd.print("%");

   if (t1==r[0] && t2==r[1] && t3==r[2] && t4==r[3]&& feed==true)
     {
      servo_test.write(100);                   //将舵机旋转到指定角度的命令
       delay(400);   
      servo_test.write(55);
      feed=false;
     }
}      


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-1 05:19 , Processed in 0.069824 second(s), 16 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表