随着城市化进程的加速及人们经济生活水平的提高,汽车工业的发展,我国城市轿车购买力逐渐增强。以致于大中城市私人汽车数量的激增,那么作为新一代车族,每一个人都梦想拥有自己的车库,而车库的笨重及不方便性,甚至现在某些自动门的价格过高,使很多人望而却步,为了实现每个爱车人的高自动化车库的梦想,我们基于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);
}
现在一个完整的车库系统就做好了,当然我们是新手,也希望大家多多指教。
|