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

+_~ 发表于 2018-8-12 16:31

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

#include <Wire.h>
int temp;
int humi;
int tol;
int j;
unsigned int loopCnt;
int chr = {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 * 128 + chr * 64 + chr * 32 + chr * 16 + chr * 8 + chr * 4 + chr * 2 + chr;
temp = chr * 128 + chr * 64 + chr * 32 + chr * 16 + chr * 8 + chr * 4 + chr * 2 + chr;
tol = chr * 128 + chr * 64 + chr * 32 + chr * 16 + chr * 8 + chr * 4 + chr * 2 + chr;
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);
}
}
页: [1]
查看完整版本: RGB模块 +DH11模块制水温度警报器