วันพฤหัสบดีที่ 15 สิงหาคม พ.ศ. 2556

การทดลอง

วงจร







1. ทดสอบตัว Panasonic PIR (Passive Infrared) Motion Sensor

เมื่อ Motion Senser ทำการ detect ไม่ได้เอาต์พุตของตัว PIR ก็จะเป็น LOW และ LED ก็จะดับ

เมื่อ Motion Senser ทำการ detect ได้เอาต์พุตของตัว PIR ก็จะเป็น HIGH และ LED ก็จะติด
วิดีโอการทดสอบ Motion Senser



โค้ด Arduino ที่ใช้ใจการทดสอบตัว Motion Senser

int ledPin = 13;        // choose the pin for the LED
int inputPin = 2;       // choose the input pin (for PIR sensor)
int val = 0;            // variable for reading the pin status
int state = 0;

void setup() {
  pinMode(ledPin, OUTPUT);    // declare LED as output
  pinMode(inputPin, INPUT);   // declare sensor as input
  Serial.begin(9600);
}

void loop(){
  val = digitalRead(inputPin);   // read input value
  switch(state){
  case 0:
    digitalWrite( ledPin, LOW );
    if( val == HIGH ){           // Motion Detected!
      state = 1;
      Serial.println("Motion detected!");
    }
    break;
  case 1:
    digitalWrite( ledPin, HIGH );
    if( val == LOW ){            // Motion Ended!
      state = 0;
      Serial.println("Motion ended!");
    }
    break;
  }
}
--------------------------------------------------------------------------------------------------- 

2. ทดสอบ Servo Motor

 
 รูปการทดลอง Servo กับ Arduino

วิดีโอการทดสอบ Servo Motor


โค้ด Arduino ที่ใช้ใจการทดสอบตัว Servo Motor

#include <Servo.h>

Servo servoMotor;
int outVal = 0;
boolean check = false;

void setup(){
  servoMotor.attach(9);
  Serial.begin( 9600 );
}

void loop(){
  if( outVal >= 180 ){
    check = true;
  }
  if( outVal <= 0 ){
    check = false;
  }
  servoMotor.write( outVal );
  delay( 1000 );
  if( check == false ){
    outVal += 30 ;
  }
  if( check == true ){
    outVal -= 30;
  }
}

--------------------------------------------------------------------------------------------------- 
3. เมื่อนำส่วนของ PIR Motion Sensor และ Servo Motor มารวมกัน





วิดีโอแสดงการทำงาน

โค้ดการทดลอง

#include <Servo.h>

Servo servoMotor;
int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int val = 0;                    // variable for reading the pin status
int state = 0;                  // state machine S0, S1
int a = 0;
int b = 0;

void setup() {
  servoMotor.attach(9);
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
  Serial.begin(9600);
}

void loop(){
  val = digitalRead(inputPin);       //read input value from PIR Motion Sensor
 
  switch(state){

  case 0:
    digitalWrite( ledPin, LOW );     // set LED ON
    dont_Move( );                  // don't move
    if( val == LOW ){               // Motion Detected!
      state = 1;                     // go to state S1;
      Serial.println("D");
    }
    break;

  case 1:
    digitalWrite( ledPin, HIGH );    // set LED OFF
    if( a%2 == 0 ){
      b = 15;
    }
    else{
      b = 165;
    }
    servo_move(b);                   // start move servo
    a++ ;                        
    if( val == HIGH ){                // Motion Ended!
      a = 0;
      state = 0;                     // return to state S0;
      Serial.println("E");
    }
    break;

  }

}


//fucntion move servo moto
void servo_move(int val){
  int i = 0;
  boolean check = false;
  int outVal = val;
  while( i < 5 ){
    if( outVal >= 165 ){
      check = true;
    }
    if( outVal <= 15 ){
      check = false;
    }
    servoMotor.write( outVal );
    delay( 500 );
    if( check == false ){
      outVal += 30 ;
    }
    if( check == true ){
      outVal -= 30;
    }
    i++;
  }
}


//fucntion stop servo motor
void dont_Move( ){
  servoMotor.write( 15 );
}

------------------------------------------------------------------------------------------------------------ 

ไม่มีความคิดเห็น:

แสดงความคิดเห็น