Project 2
Project 2
h>
#include <Keypad.h>
int sensePin = 0;
int ledPin = 11;
//Control DC Motor with temperature sensor
int tempPin = 2;
int fanSpeed = 10;
int GasSensorPin = 3;
int buzzer = 12;
int irPin = 1;
int currentDist = 0;
int lastDist = 0;
Servo ServoMotor;
char* password = "888"; // change the password here, just pick any 3 numbers
int position = 0;
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = { 7, 6, 5, 4 };
byte colPins[COLS] = { 3, 2, 1 };
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buzzer, OUTPUT);
ServoMotor.attach(9);
LockedPosition(true);
Serial.begin(9600);
}
void loop() {
// light controller
currentDist = analogRead(irPin);
//Serial.println(currentDist);
int lightVal = analogRead(sensePin);
//Serial.println(lightVal);
if ((currentDist > lastDist + thresh || currentDist < lastDist - thresh) && li
ghtVal < 90)
{
val = constrain(val, 80, 350);
int ledLevel = map(val, 80, 350, 255, 0);
analogWrite(ledPin, ledLevel);
delay(1500);
}
else
{
analogWrite(ledPin, 0);
}
lastDist = currentDist;
// Temperature Controller
float temp = analogRead(tempPin);
temp = (temp *5.0*100.0)/1024.0; // calculate temperature in degree Celsius
temp = constrain(temp, 25, 70);
int tempLevel = map(temp, 25, 70, 0, 255);
analogWrite(fanSpeed, tempLevel);
//Gas Leakage Indicator
int gasValue = analogRead(GasSensorPin);
//Serial.println(gasValue);
if (gasValue > 500)
{
digitalWrite(buzzer,HIGH);
}
else
{
digitalWrite(buzzer,LOW);
}
//Electronic door lock
char key = keypad.getKey();
//Serial.println(key);
if (key == '*' || key == '#')
{
position = 0;
LockedPosition(true);
}
if (key == password[position])
{
position ++;
}
if (position == 3)
{
LockedPosition(false);
}
delay(100);
}
void LockedPosition(int locked)
{
if (locked)
{
ServoMotor.write(11);
}
else
{
ServoMotor.write(90);
}
}