Site icon George的生活點滴

Arduino 自動門程式

//自動門 
#include <Servo.h> 
Servo myservo; 
 
 
int sensorValue = 0;   
const int IR_InPin = A3;                            // A3 接上 IR 
const int Ledred_OutPin = 10;  
const int Ledgreen_OutPin = 11; 
const int BezzerPin = 9; 
void setup() { 
  Serial.begin(57600); 
  myservo.attach(4);                                //360度伺服馬達接第4腳
  pinMode(IR_InPin, INPUT);       
  pinMode(BezzerPin, OUTPUT);           
  pinMode(Ledred_OutPin, OUTPUT);       
  pinMode(Ledgreen_OutPin, OUTPUT);    
 } 
 
 
void loop() { 
  sensorValue = analogRead(IR_InPin);   
  myservo.write(90);  
  if (sensorValue >= 360){ 
    myservo.write(50);    // 順時針轉 (開門)
    digitalWrite(Ledred_OutPin, HIGH); //10 
    tone(BezzerPin, 1000, 100); 
    delay(2500);          //運轉 2.5秒
    myservo.write(90);   
    tone(BezzerPin, 100, 10); 
    digitalWrite(Ledgreen_OutPin, HIGH);  
    digitalWrite(Ledred_OutPin, LOW);    
    delay(10000);        //開門時間 10秒
    digitalWrite(Ledgreen_OutPin, LOW); 
    digitalWrite(Ledred_OutPin, HIGH );  
    myservo.write(180);    // 逆時針轉 (關門)
    tone(BezzerPin, 100, 10); 
    delay(2500);           //運轉 2.5 秒
    tone(BezzerPin, 1000, 100); 
    digitalWrite(Ledred_OutPin, LOW);  
    myservo.write(90);     //馬達停止
 }    
  myservo.write(90);   
}

// 360 度馬達  90 為停止    0-89順時針轉  , 91-180 逆時針轉

利用智高 1247A 的套件, 以 Arduino 程式直接燒錄在板子上, 完成一個自動門

當車子行經此門, IR 感應到後, 門會開啟 10 秒鐘的時間, 之後再行關閉

2017-03-14

Exit mobile version