Arduino พัดลมเปิดปิดอัตโนมัติควบคุมความเร็วตามอุณหภูมิ 4ระดับ

จะทำพัดลมอัตโนมัติควบคุมความเร็วตามอุณหภูมิ 4 ระดับ ถ้าอุณหภูมิสูงขึ้นพัดลมจะหมุนเร็วขึ้นตามไปด้วย มีจอแสดงผล ค่าอุณหภูมิห้อง ความชื้นในอากาศ และ ไฟสถานะแสดงระดับความเร็วของพัดลม  ใช้ Arduino UNO R3 เป็นบอร์ดประมวลผล ในการอ่านค่าและสั่งงานอุปกรณ์ต่างๆ และใช้ เซ็นเซอร์วัดอุณหภูมิและความชื้น DHT22 วัดค่าอุณหภูมิห้อง

#include "DHT.h"
2
#include <Wire.h>
3
#include <LiquidCrystal_I2C.h>
4
int Number = 0;
5
const int in3Pin = 3;
6
const int in4Pin = 4;
7
int p8_Lv1 = 8;
8
int p8_Lv2 = 9;
9
int p8_Lv3 = 10;
10
int p8_Lv4 = 11;
11
LiquidCrystal_I2C lcd(0x27, 16, 2);
12
#define DHTPIN 2 // what digital pin we're connected to
13
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
14
DHT dht(DHTPIN, DHTTYPE);
15
void setup() {
16
Serial.begin(9600);
17
Serial.println("DHTxx test!");
18
dht.begin();
19
lcd.begin();
20
lcd.backlight();
21
pinMode(in3Pin, OUTPUT);
22
pinMode(in4Pin, OUTPUT);
23
analogWrite (in3Pin , 0);
24
digitalWrite(in4Pin, LOW);
25
pinMode(p8_Lv1, OUTPUT);
26
pinMode(p8_Lv2, OUTPUT);
27
pinMode(p8_Lv3, OUTPUT);
28
pinMode(p8_Lv4, OUTPUT);
29
digitalWrite(p8_Lv1, LOW);
30
digitalWrite(p8_Lv2, LOW);
31
digitalWrite(p8_Lv3, LOW);
32
digitalWrite(p8_Lv4, LOW);
33
}
34
void loop() {
35
delay(2000);
36
float h = dht.readHumidity();
37
// Read temperature as Celsius (the default)
38
float t = dht.readTemperature();
39
// Read temperature as Fahrenheit (isFahrenheit = true)
40
float f = dht.readTemperature(true);
41
// Check if any reads failed and exit early (to try again).
42
if (isnan(h) || isnan(t) || isnan(f)) {
43
Serial.println("Failed to read from DHT sensor!");
44
return;
45
}
46
// Compute heat index in Fahrenheit (the default)
47
float hif = dht.computeHeatIndex(f, h);
48
// Compute heat index in Celsius (isFahreheit = false)
49
float hic = dht.computeHeatIndex(t, h, false);
50
Serial.print("Humidity: ");
51
Serial.print(h);
52
Serial.print(" %\t");
53
Serial.print("Temperature: ");
54
Serial.print(t);
55
Serial.print(" *C ");
56
Serial.print(f);
57
Serial.print(" *F\t");
58
Serial.print("Heat index: ");
59
Serial.print(hic);
60
Serial.print(" *C ");
61
Serial.print(hif);
62
Serial.println(" *F");
63
if ( t < 30 ) {
64
Number = 0;
65
analogWrite (in3Pin , 0);
66
digitalWrite(p8_Lv1, LOW);
67
digitalWrite(p8_Lv2, HIGH);
68
digitalWrite(p8_Lv3, HIGH);
69
digitalWrite(p8_Lv4, HIGH);
70
}
71
if ( t > 30 && t <= 35 ) {
72
Number = 1;
73
analogWrite (in3Pin , 100);
74
digitalWrite(p8_Lv1, LOW);
75
digitalWrite(p8_Lv2, LOW);
76
digitalWrite(p8_Lv3, HIGH);
77
digitalWrite(p8_Lv4, HIGH);
78
}
79
if ( t > 35 && t <= 40 ) {
80
Number = 2;
81
analogWrite (in3Pin , 150);
82
digitalWrite(p8_Lv1, LOW);
83
digitalWrite(p8_Lv2, LOW);
84
digitalWrite(p8_Lv3, LOW);
85
digitalWrite(p8_Lv4, HIGH);
86
}
87
if ( t > 40 ) {
88
Number = 3;
89
analogWrite (in3Pin , 255);
90
digitalWrite(p8_Lv1, LOW);
91
digitalWrite(p8_Lv2, LOW);
92
digitalWrite(p8_Lv3, LOW);
93
digitalWrite(p8_Lv4, LOW);
94
}
95
lcd.setCursor(0, 0);
96
lcd.print("Tem:");
97
lcd.print(t);
98
lcd.print("*C");
99
lcd.setCursor(0, 1);
100
lcd.print("Hum:");
101
lcd.print(h);
102
lcd.print("% ");
103
lcd.print("Num:");
104
lcd.print(Number);
105
}
แหล่งที่มา
http://www.myarduino.net/product/1808/%E0%B9%82%E0%B8%9B%E0%B8%A3%E0%B9%80%
E0%B8%88%E0%B8%84-arduino-%E0%B8%9E%E0%B8%B1%E0%B8%94%E0%B8%A5%E0%
B8%A1%E0%B9%80%E0%B8%9B%E0%B8%B4%E0%B8%94%E0%B8%9B%E0%B8%B4%E0%
B8%94%E0%B8%AD%E0%B8%B1%E0%B8%95%E0%B9%82%E0%B8%99%E0%B8%A1%E0%
B8%B1%E0%B8%95%E0%B8%B4%E0%B8%84%E0%B8%A7%E0%B8%9A%E0%B8%84%E0%
B8%B8%E0%B8%A1%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B9%80%E0%
B8%A3%E0%B9%87%E0%B8%A7%E0%B8%95%E0%B8%B2%E0%B8%A1%E0%B8%AD%E0%
B8%B8%E0%B8%93%E0%B8%AB%E0%B8%A0%E0%B8%B9%E0%B8%A1%E0%B8%B4-4%E0%
B8%A3%E0%B8%B0%E0%B8%94%E0%B8%B1%E0%B8%9A

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

Coin Validator เครื่องรับเหรียญ เครื่องหยอดเหรียญ Multi Coin รุ่น SG-6

Learning 7 Segment + Arduino