极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11410|回复: 1

自动车库门系统

[复制链接]
发表于 2016-1-12 16:22:49 | 显示全部楼层 |阅读模式
  随着城市化进程的加速及人们经济生活水平的提高,汽车工业的发展,我国城市轿车购买力逐渐增强。以致于大中城市私人汽车数量的激增,那么作为新一代车族,每一个人都梦想拥有自己的车库,而车库的笨重及不方便性,甚至现在某些自动门的价格过高,使很多人望而却步,为了实现每个爱车人的高自动化车库的梦想,我们基于ARDUINO平台设计了自动车库门系统,并添加了警报模式,实现了一种系统,两种模式,使你的车库不再怕被“骚扰”。
使用模块
       超声波模块*1
       红外线遥控器*1
       蜂鸣器*1(也可以自行加警报灯)
       舵机*1(发电机也可)
       LCD显示屏
       面包板*2
       Uno开发板*3
模块工作原理
     本系统核心模块时超声波测距,通过测定不同距离,以及红外遥控对不同模块的切换,实现车库门的自动开关以及无人情况下的报警。同时人性化加入显示屏实时测距,以及减少误报警的影响。


这里展示较难模块的连接方式(需要自行组装)。
下面是实物的展示:




然后把代码录进各自的开发板,代码如下:

舵机
#include <Servo.h>
#include <IRremote.h>
int RECV_PIN = 8;
const int TrigPin = 2;
const int EchoPin = 3;
float cm1;
float cm2;
long on1  = 0x00FF629D;
IRrecv irrecv(RECV_PIN);
Servo myservo;//定义舵机变量名
decode_results results;
// Dumps out the decode_results structure.
// Call this after IRrecv::decode()
// void * to work around compiler issue
//void dump(void *v) {
//  decode_results *results = (decode_results *)v
void dump(decode_results *results) {
  int count = results->rawlen;
  if (results->decode_type == UNKNOWN)
    {
     Serial.println("Could not decode message");
    }
  else
   {
    if (results->decode_type == NEC)
      {
       Serial.print("Decoded NEC: ");
      }
    else if (results->decode_type == SONY)
      {
       Serial.print("Decoded SONY: ");
      }
    else if (results->decode_type == RC5)
      {
       Serial.print("Decoded RC5: ");
      }
    else if (results->decode_type == RC6)
      {
       Serial.print("Decoded RC6: ");
      }
     Serial.print(results->value, HEX);
     Serial.print(" (");
     Serial.print(results->bits, DEC);
     Serial.println(" bits)");
   }
     Serial.print("Raw (");
     Serial.print(count, DEC);
     Serial.print("): ");

  for (int i = 0; i < count; i++)
     {
      if ((i % 2) == 1) {
      Serial.print(results->rawbuf
*USECPERTICK, DEC);
     }
    else  
     {
      Serial.print(-(int)results->rawbuf
*USECPERTICK, DEC);
     }
    Serial.print(" ");
     }
      Serial.println("");
     }

void setup()
{
Serial.begin(9600);
pinMode(RECV_PIN, INPUT);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
pinMode(13, OUTPUT);
irrecv.enableIRIn(); // Start the receiver
myservo.attach(9);
}
int on = 0;
unsigned long last = millis();
void loop()
{int val;
int vall;
digitalWrite(TrigPin, LOW);
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);

cm1 = pulseIn(EchoPin, HIGH) / 58.0; //将回波时间换算成cm
cm1 = (int(cm1 * 100.0)) / 100.0; //保留两位小数
Serial.print(cm1);
Serial.println("cm1");
delay(2000);

digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);

cm2 = pulseIn(EchoPin, HIGH) / 58.0; //将回波时间换算成cm
cm2 = (int(cm2 * 100.0)) / 100.0; //保留两位小数
Serial.print(cm2);
Serial.println("cm2");
delay(2000);
if (irrecv.decode(&results))
   {
    // If it's been at least 1/4 second since the last
    // IR received, toggle the relay
    if (millis() - last > 250)
      {
       on = !on;
//       digitalWrite(8, on ? HIGH : LOW);
       digitalWrite(13, on ? HIGH : LOW);
       dump(&results);
      }
val=cm1-cm2;
vall=cm2-cm1;
if(results.value == on1)
{if(val>50)
{myservo.write(180);//设置舵机旋转的角度
delay(20000);
}

if(vall>30)
{myservo.write(90);//设置舵机旋转的角度
delay(1000);
}
}
}
}



蜂鸣器&红外线
#include <IRremote.h>
int RECV_PIN = 8;
const int TrigPin = 2;
const int EchoPin = 3;

int buzzer = A0;
float cm1;
float cm2;
int taste;

long on1  = 0x00FFA25D;
IRrecv irrecv(RECV_PIN);
decode_results results;
// Dumps out the decode_results structure.
// Call this after IRrecv::decode()
// void * to work around compiler issue
//void dump(void *v) {
//  decode_results *results = (decode_results *)v
void dump(decode_results *results) {
  int count = results->rawlen;
  if (results->decode_type == UNKNOWN)
    {
     Serial.println("Could not decode message");
    }
  else
   {
    if (results->decode_type == NEC)
      {
       Serial.print("Decoded NEC: ");
      }
    else if (results->decode_type == SONY)
      {
       Serial.print("Decoded SONY: ");
      }
    else if (results->decode_type == RC5)
      {
       Serial.print("Decoded RC5: ");
      }
    else if (results->decode_type == RC6)
      {
       Serial.print("Decoded RC6: ");
      }
     Serial.print(results->value, HEX);
     Serial.print(" (");
     Serial.print(results->bits, DEC);
     Serial.println(" bits)");
   }
     Serial.print("Raw (");
     Serial.print(count, DEC);
     Serial.print("): ");

  for (int i = 0; i < count; i++)
     {
      if ((i % 2) == 1) {
      Serial.print(results->rawbuf
*USECPERTICK, DEC);
     }
    else  
     {
      Serial.print(-(int)results->rawbuf
*USECPERTICK, DEC);
     }
    Serial.print(" ");
     }
      Serial.println("");
     }

void setup()
{
Serial.begin(9600);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
pinMode(RECV_PIN, INPUT);   
pinMode(buzzer, OUTPUT);
pinMode(13, OUTPUT);
irrecv.enableIRIn(); // Start the receiver
}
int on = 0;
unsigned long last = millis();

void loop()
{int val;
unsigned char i;//定义变量

digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);

cm1 = pulseIn(EchoPin, HIGH) / 58.0; //将回波时间换算成cm
cm1 = (int(cm1 * 100.0)) / 100.0; //保留两位小数
Serial.print(cm1);
Serial.println("cm1");
delay(1000);

digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);

cm2 = pulseIn(EchoPin, HIGH) / 58.0; //将回波时间换算成cm
cm2 = (int(cm2 * 100.0)) / 100.0; //保留两位小数
Serial.print(cm2);
Serial.println("cm2");
delay(1000);

val=cm1-cm2;
if (irrecv.decode(&results))
{
    // If it's been at least 1/4 second since the last
    // IR received, toggle the relay
    if (millis() - last > 250)
      {
       on = !on;
       digitalWrite(13, on ? HIGH : LOW);
       dump(&results);
      }
        
if (results.value == on1){
if (val>10)
        {while(1)
{ for(i=0;i<80;i++)
{
digitalWrite(buzzer,HIGH);
delay(1);//延时1ms
digitalWrite(buzzer,LOW);
delay(1);//延时ms
}
for(i=0;i<100;i++)
{
digitalWrite(buzzer,HIGH);
delay(2);//延时2ms
digitalWrite(buzzer,LOW);
delay(2);//延时2ms
}
}
}
}
}
}


液晶屏
#include <LiquidCrystal.h>   //调用arduino自带的LiquidCrystal库
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
int cm;
const int TrigPin = 2;
const int EchoPin = 3;

void setup()
{Serial.begin(9600);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
  lcd.begin(16, 2);  //初始化LCD
lcd.print("2016 1 12 tuesday");  //使屏幕显示文字
delay(1000); //延时1000ms
}

void loop ()                    
{
int val;
digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);

cm = pulseIn(EchoPin, HIGH) / 58.0; //将回波时间换算成cm
cm = (int(cm * 100.0)) / 100.0; //保留两位小数
Serial.print(cm);
Serial.println("cm1");
delay(1000);
val=(cm%1);
lcd.clear(); //清屏
lcd.print(" 1 12 Tuesday"); //使屏幕显示文字
lcd.setCursor(0, 1) ; //设置光标位置为第二行第一个位置
lcd.print("dis ");
lcd.print(cm);   
lcd.print("cm");

delay(500);                 
}
现在一个完整的车库系统就做好了,当然我们是新手,也希望大家多多指教。











本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2016-1-12 17:09:36 | 显示全部楼层
+10086+10086
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 06:00 , Processed in 0.054612 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表