RGB模块 +DH11模块制水温度警报器-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3554|回复: 0

RGB模块 +DH11模块制水温度警报器

[复制链接]
发表于 2018-8-12 16:31 | 显示全部楼层 |阅读模式
#include <Wire.h>
int temp;
int humi;
int tol;
int j;
unsigned int loopCnt;
int chr[40] = {0};
unsigned long time;
#define pin 2
const int led1 = 9;
const int led2 = 10;
const int led3 = 11;

void setup() {
  Serial.begin(9600);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
}
void setColor(int red, int green, int blue) {
  analogWrite(led1, 255 - red);
  analogWrite(led2, 255 - green);
  analogWrite(led3, 255 - blue);
}
void loop() {
bgn:
  delay(2000);
  pinMode(pin, OUTPUT);
  digitalWrite(pin, LOW);
  delay(20);
  digitalWrite(pin, HIGH);
  delayMicroseconds(40);
  digitalWrite(pin, LOW);
  pinMode(pin, INPUT);
  loopCnt = 10000;
  while (digitalRead(pin) != HIGH)
  {
    if (loopCnt-- == 0)
    {
      Serial.println("HIGH");
      goto bgn;
    }
  }
  loopCnt = 30000;
  while (digitalRead(pin) != LOW)
  {
    if (loopCnt-- == 0)
    {
      Serial.println("LOW");
      goto bgn;
    }
  }
  for (int i = 0; i < 40; i++)
  {
    while (digitalRead(pin) == LOW)
    {}
    time = micros();
    while (digitalRead(pin) == HIGH)
    {}

    if (micros() - time > 50)
    {
      chr = 1;
    } else {
      chr = 0;
    }
  }

  humi = chr[0] * 128 + chr[1] * 64 + chr[2] * 32 + chr[3] * 16 + chr[4] * 8 + chr[5] * 4 + chr[6] * 2 + chr[7];
  temp = chr[16] * 128 + chr[17] * 64 + chr[18] * 32 + chr[19] * 16 + chr[20] * 8 + chr[21] * 4 + chr[22] * 2 + chr[23];
  tol = chr[32] * 128 + chr[33] * 64 + chr[34] * 32 + chr[35] * 16 + chr[36] * 8 + chr[37] * 4 + chr[38] * 2 + chr[39];
  Serial.print("temp:");
  Serial.println(temp);
  Serial.print("humi:");
  Serial.println(humi);
  delay(500);
  if (temp >= 65) {
    setColor(255, 0, 0);
    delay(500);
  }
  if (temp <= 5) {
    setColor(0, 0, 255);
    delay(500);
  }
  if (5 < temp < 65) {
    setColor(84,139,69);
    delay(500);
  }
}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-11-28 09:47 , Processed in 0.067095 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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