#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);
}
}
|