Arduino - 溫溼度感測器 + 伺服馬達、光敏電阻 + LED(Temperature and Humidity sensor + Servomotor、photocell + LED)


目的


1.以溫溼度感測器感測溫度變化,進而使伺服馬達有不同的轉速

2.光敏電阻監控光線變化,光線越暗,LED越亮


成果:


Arduino Code:

#include < Wire.h > 
#include < SPI.h > 
#include < dht11.h > // DHT11 感測器程式庫
#include < LiquidCrystal.h >

const byte LED = 11;
dht11 DHT11; // 宣告溫濕度檢測器程式物件
const byte dataPin = 2;
int LDR = 0; //光敏電阻輸入,接在anallog pin 0
float readLDR = 0; //光敏電阻感應到的光源產生的電阻值
byte mopin = 5;
int potValue = 0;
byte val = 0;
//led change
int bright = 0;

void setup() {
    pinMode(LED, OUTPUT);
    pinMode(mopin, OUTPUT);
    Serial.begin(9600);
}
void loop() {
    readLDR = analogRead(LDR); //存取光敏電阻的電阻值
    int chk = DHT11.read(dataPin);

    if (chk == 0) {
        Serial.println(DHT11.temperature);
        if (DHT11.temperature < 27) {
            val = 0;
            analogWrite(mopin, val);
        }
        else if (DHT11.temperature == 27) {
            analogWrite(mopin, 100);
        }
        else if (DHT11.temperature > 27) {
            analogWrite(mopin, 200);
        }

        analogWrite(11, bright);
        if (readLDR > 850) {
            bright = 255;
        }
        else if (readLDR > 600 && readLDR < 850) {
            bright = 80;
        }
        else if (readLDR < 600) {
            bright = 0;
        }
        //Serial.println(readLDR);

    }
}

留言