Demo Video:
Github: https://github.com/weijuinlee/EA_CA2
Code Snippets:
//Libraries
#include <DHT.h>
#include <Servo.h>
//Constants
#define DHTPIN 2 // pin we're connected to
#define DHTTYPE DHT22 // DHT22
//Variables
float temp; //Stores temperature value
const int transistorPin = 9; // connected to the base of the transistor
int pos = 0; // variable to store the servo position
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
Servo myservo; // create servo object to control a servo
void setup() {
// set the transistor pin as output:
pinMode(transistorPin, OUTPUT);
//Initialize serial port
Serial.begin(9600);
//Serial.println("DHT22 sensor testing");
//Initialize the DHT sensor
dht.begin();
myservo.attach(10); // attaches the servo on pin 10 to the servo object
}
void loop() {
//Read data and store it to variable temp
temp = dht.readTemperature();
Serial.print("Celsius = ");
Serial.print(temp);
Serial.println(" degree celsius");
if (temp > 30.0) {
digitalWrite(transistorPin, HIGH);
delay(500);
digitalWrite(transistorPin, LOW);
delay(500);
for (pos = 0; pos <= 90; pos += 10) { // goes from 0 degrees to 90 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 90; pos >= 0; pos -= 10) { // goes from 90 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
else {
digitalWrite(transistorPin, LOW);
}
}