ArduinoUNO实战-第十六章-滚珠开关实验可以实现电动车防盗报警设备

目标

利用滚珠开关的特性,编写一个电动车防盗报警设备

理论学习

滚动开关,当有震动发生的时候,滚珠开关的两个引脚会瞬间接通,arduino通过中断方式获取到该信号,进行蜂鸣器报警

实物图

滚珠开关

在这里插入图片描述

SW-18010P SW-420

在这里插入图片描述

  • S是数据信号
  • — 是负极接地GND
  • 中间是正极,接5V

地址:https://detail.tmall.com/item.htm?spm=a1z0d.6639537/tb.1997196601.14.3b3c7484l3qT0j&id=568742382108

attachInterrupt()

外部中断函数,只有arduino指定的外部中断口有效
语法:attachInterrupt(interrupt,function,mode);
参数:intertupt:中断号,一般arduino有中断0(数字2口)和中断1(数字3口)
function:中断服务函数,该函数必须没有参数并且返回为空
mode:中断触发模式
- 1.LOW信号低触发
- 2.CHANGE 信号翻转触发
- 3.RISING 信号上升沿触发
- 4.FALLING 信号下降沿触发

接线图

在这里插入图片描述
黑色是有源蜂鸣器
白色的是滚珠开关

代码

滚珠开关

当有震动滚珠开光连通后,触发蜂鸣器报警

// 滚珠开关
#define key 2
// 蜂鸣器
#define buzzer 3
int flag = 0;
int count = 0;
void setup() {
//只是执行1次
	pinMode(key,INPUT_PULLUP);
	pinMode(buzzer, OUTPUT);
	attachInterrupt(0, buzzerDi, FALLING);
	Serial.begin(9600);

}
void loop() {
//可以执行多次
	if (flag == 1) {
		flag = 0;
		digitalWrite(buzzer,HIGH);
		delay(1000);
	}
	else {
		digitalWrite(buzzer,LOW);
	}
	Serial.println(count);
}
void buzzerDi() {
	flag = 1;
	count++;
}

当有震动时,触发attachInterrupt函数,调用buzzerDi,修改flag=1,loop中给高电平让蜂鸣器响一秒钟。然后恢复标记位。

震动传感器

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// #include <OneWire.h> //可以不引入,因为DallasTemperature.h中已经引入了OneWire.h
#include <DallasTemperature.h>

#define ONE_WIRE_BUS A0               //1-wire数据总线连接在IO4
OneWire oneWire(ONE_WIRE_BUS);       //声明
DallasTemperature sensors(&oneWire); //声明

LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

// 定义滚珠开关
#define KEY 2
int flag = 0;
int count = 0;

void setup()
{
  // 设置上拉
  pinMode(KEY,INPUT_PULLUP);
	attachInterrupt(0, buzzerDi, FALLING);
  // pinMode(KEY,INPUT);

  Serial.begin(115200);
  /**/
  Serial.println("");

  sensors.begin(); //初始化总线
  sensors.setWaitForConversion(false); //设置为非阻塞模式

  lcd.init();                      // initialize the lcd 
 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.print("Hello, world!");
  
}


unsigned long previousMillis = 0; //毫秒时间记录
const long interval = 1000;       //时间间隔

void loop()
{
  /**/
  //以下段落相当于每秒读取前次温度,并发起新一次温度转换
  unsigned long currentMillis = millis();         //读取当前时间
  if (currentMillis - previousMillis >= interval) //如果和前次时间大于等于时间间隔
  {
    previousMillis = currentMillis; //更新时间记录

    float tempC = sensors.getTempCByIndex(0); //获取索引号0的传感器摄氏温度数据
    if (tempC != DEVICE_DISCONNECTED_C)       //如果获取到的温度正常
    {
      Serial.print("\n当前温度是: ");
      Serial.print(tempC);
      Serial.println(" ℃");
      Serial.println(" ");

      String val = "temp:"+ String(tempC) + String("℃");
      lcd.setCursor(0,0);
      lcd.print("Hello, UNO world!");
      lcd.setCursor(0,1);
      //lcd.print(val);
      // lcd.print((int)tempC/10);
      // lcd.print(".");
      // lcd.print((int)tempC%10);
      lcd.print("temp:");
      lcd.print(tempC);
      // 打印摄氏度
      lcd.print((char)223);
      lcd.print("C");
      
    }
    sensors.requestTemperatures(); //发起新的温度转换
  }
  
  if (flag == 1) {
		flag = 0;
	  Serial.println(count);
    lcd.setCursor(0,0);
    lcd.print("warn, UNO world!");
    delay(1000);
	}
	else {
    lcd.setCursor(0,0);
    lcd.print("hi, UNO world!");
	}

  delay(10);
  //Serial.print(".");
}
// 当手拿起振动器时就会触发
void buzzerDi() {
	flag = 1;
	count++;
  Serial.println("xxxx");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值