Wednesday, July 27, 2016

Motion Sensor Sketch


/*
// Final Working sketch
// Motion Sensor with sound
// pwm 3,5,6,9,10,11
// Final PIR with sound
*/


//#include <Servo.h>
int spkr = 4;                // the pin that the LED is atteched to
int led = 13;                // the pin that the LED is atteched to
int sensor = 5;              // the pin that the sensor is atteched to
int state = LOW;             // by default, no motion detected
int val = 0;                 // variable to store the sensor status (value)
int pos = 0;
int  var = 0;
//Servo mHead;

void setup() {
  pinMode(led, OUTPUT);      // initalize LED as an output
 pinMode(spkr, OUTPUT);      // initalize speaker as an output
  pinMode(sensor, INPUT);    // initialize sensor as an input
  Serial.begin(9600);        // initialize serial
 //  mHead.attach(10);
}

void loop(){
  val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
    Serial.println("Motion detected!");
 while(var < 5) {       
        digitalWrite(13, HIGH);
        delay(1000);
       digitalWrite(13, LOW);
       delay(1000);
       tone(4, 440, 200);        //BB8 ?
      var++;
       }
   var=0;
  
    if (state == LOW) {
      digitalWrite(led, HIGH);   // turn LED ON
      state = HIGH;       // update variable state to HIGH
    }
  }
  else {
      digitalWrite(led, LOW); // turn LED OFF
      delay(200);             // delay 200 milliseconds
     
      if (state == HIGH){
        Serial.println("Motion stopped!");
        state = LOW;       // update variable state to LOW
    }
  }
}



/*void wink()
{
mHead.write(30);
  tone(4, 440, 200);
digitalWrite(led, HIGH);
      delay(1000);
  digitalWrite(led, LOW);
  delay(500);


mHead.write(60);
   tone(4, 440, 200);
digitalWrite(led, HIGH);
      delay(1000);
  digitalWrite(led, LOW);
  delay(500);

 
mHead.write(90);
   tone(4, 440, 200);
digitalWrite(led, HIGH);
      delay(1000);
  digitalWrite(led, LOW);
  delay(500);

 
  mHead.write(120);
   tone(4, 440, 200);
digitalWrite(led, HIGH);
      delay(1000);
  digitalWrite(led, LOW);
  delay(500);
 
 
  mHead.write(150);
  tone(4, 440, 200);
digitalWrite(led, HIGH);
      delay(1000);
  digitalWrite(led, LOW);
  delay(500);
 

  mHead.write(90);
  tone(4, 440, 200);
digitalWrite(led, HIGH);
      delay(1000);
  digitalWrite(led, LOW);
  delay(500);

}
*/

/*void 142813()
{
 for (int i = 0; i <= random(3, 9); i++){
        
        digitalWrite(ledPin, HIGH);  
        tone(speakerPin, K + random(-1700, 2000));          
        delay(random(70, 170));  
        digitalWrite(ledPin, LOW);           
        noTone(speakerPin);         
        delay(random(0, 30));             
    } 
    noTone(speakerPin);         
    delay(random(2000, 4000));             //2000,4000
}

No comments:

Post a Comment