#include Servo microservo; int pos=0; int temp; int tempPin = 2; // analog input pin int sampleTime = 10000; int i=0; #define trigPin 7 #define echoPin 6 #define led 11 #define led2 10 const int sensorPin= 0; int smoke_level; long duration, distance; void setup() { // Open serial connection. Serial.begin(9600); pinMode(9,OUTPUT); pinMode(13,OUTPUT); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(led, OUTPUT); pinMode(2,OUTPUT); pinMode(led2, OUTPUT); pinMode(sensorPin, INPUT);//the smoke sensor will be an input to the arduino microservo.attach(5); Serial.write('1000'); } void loop() { smoke_level= analogRead(sensorPin); //arduino reads the value from the smoke sensor //prints just for debugging purposes, to see what values the sensor is picking up if(smoke_level > 200) { //if smoke level is greater than 200, the buzzer will go off digitalWrite(2, HIGH); digitalWrite(13,HIGH); delay(10000); digitalWrite(13,LOW); for(pos = 0; pos < 180; pos += 3) // goes from 0 degrees to 180 degrees { // in steps of 1 degree microservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } } else { digitalWrite(2, LOW); } if(Serial.available() > 0) // if data present, blink { char inByte = Serial.read(); switch(inByte) { case'a': { digitalWrite(9,HIGH); Serial.println(" LED IS ON "); Serial.write('\n'); Serial.write('0'); } break; case'b': digitalWrite(9,LOW); Serial.println(" LED IS OFF "); Serial.write('\n'); Serial.write('0'); break; case'c': digitalWrite(13,HIGH); Serial.println(" FAN IS ON "); Serial.write('\n'); Serial.write('0'); break; case'd': digitalWrite(13,LOW); Serial.println(" FAN IS OFF "); Serial.write('\n'); Serial.write('0'); break; case'e': for(pos = 0; pos < 180; pos += 3) // goes from 0 degrees to 180 degrees { // in steps of 1 degree microservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } Serial.println(" DOOR IS OPEN "); Serial.write('\n'); Serial.write('0'); break; case'f': for(pos = 180; pos>=1; pos-=3) // goes from 180 degrees to 0 degrees { microservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } Serial.println(" DOOR IS CLOSED "); Serial.write('\n'); Serial.write('0'); break; case'g': //gets and prints the raw data from the lm35 { temp = analogRead(tempPin); //converts raw data into degrees celsius and prints it out // 500mV/1024=.48828125 temp = temp * 0.48828125; Serial.write('\n'); Serial.print(" CELSIUS: "); Serial.print(temp); Serial.write(temp); Serial.println("*C"); //converts celsius into fahrenheit temp = temp *9 / 5; temp = temp + 32; Serial.print(" FAHRENHEIT: "); Serial.print(temp); Serial.write(temp); Serial.println("*F"); Serial.write('0'); break; } case 'h': { do { digitalWrite(trigPin, LOW); // Added this line delayMicroseconds(10); // Added this line digitalWrite(trigPin, HIGH); // delayMicroseconds(1000); - Removed this line delayMicroseconds(10); // Added this line digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; if (distance <= 6) { digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off digitalWrite(led2,LOW); Serial.print(" Intrusion is Detected and distance is"); digitalWrite(2,HIGH); delay(3000); Serial.write('0'); } else { digitalWrite(led,LOW); digitalWrite(led2,HIGH); } if (distance >= 200 || distance <= 0) { Serial.println("Out of range"); } else { Serial.print(distance); Serial.println(" cm"); } delay(500); } while(distance>6); Serial.write('0'); break; } default: Serial.println("wrong value recieved by arduino"); Serial.write('0'); break; } } }