Introduction to Physical Computing-Term I
CONCEPT : The basic concept of the project was to use the servo motor, ultrasonic sensor & sound sensor. With the ultrasonic sensor the distance of an object will be detected and the servo motor will rotate from 0 to 90 degree and the door opens .Then the sound sensor will detect the sound of a clap and will turn on the lights in the room.
#include<Servo.h>
int trig=6;
int echo=5;
int dt=10;
Servo servo;
int distance,duration;
void setup() {
// put your setup code here, to run once:
pinMode(trig,OUTPUT);
pinMode(echo,INPUT);
Serial.begin(9600);
servo.attach(11);
}
void loop() {
// put your main code here, to run repeatedly:
calc_dis();
servo.write(0);
if (distance < 50){
servo.write(90);
delay(3000);
}
else{
servo.write(0);}
delay(3000);
}
void calc_dis()
{
// int duration,distance;
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig,HIGH);
delay(dt);
digitalWrite(trig,LOW);
duration=pulseIn(echo,HIGH);
distance = (duration/2) / 29.1;
Serial.println(distance);
Serial.println(“cm”);
// return distance;
}
define sensorPin 7
define LED 13
int soundValue;
int lastSoundValue;
long lastNoiseTime = 0;
long currentNoiseTime = 0;
long lastLightChange = 0;
int ledStatus = LOW;
void setup() {
pinMode(sensorPin, INPUT);
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
soundValue = digitalRead(sensorPin);
currentNoiseTime = millis();
if (soundValue == 1) { // if there is currently a noise
if (
(currentNoiseTime > lastNoiseTime + 200) && // to debounce a sound occurring in more than a loop cycle as a single noise
(lastSoundValue == 0) // if it was silent before
){ ledStatus = !ledStatus; digitalWrite(LED, ledStatus); lastLightChange = currentNoiseTime; } lastNoiseTime = currentNoiseTime;
}
lastSoundValue = soundValue;
Serial.print(soundValue);
}