不得不说,Arduino真的和适合快速开发,有很多库能够直接使用。上一次已经实现了蓝牙BLE的功能,现在需要对外设进行控制,主要包括:
通过舵机控制门的开关,舵机的控制使用servo库。 servo.attach(pin)
绑定舵机的控制引脚 servo.write(angle)
控制舵机的角度
要实现门开启3s后自动关闭的功能,需要引入定时的库。这里使用的'Ticker'库, Ticker timer1(closeDoor, 3000, 1);
创建一个定时器 timer1.update();
在主循环中调用这个函数,用于刷新定时器的值 timer1.start();
启动定时器
通过门磁开关判断门当前是否为开启状态,用的是磁性的接近开关。
这个是一个开关量信号,当两边接近的时候,通过磁性吸合内部的开关,否则内部的开关断开,因此将IO口设置成上拉输入,当读取到低电平的时候,意味着门闭合了。
ADPS-9960是一个手势传感器,也可以当成接近开关来使用,通过'Adafruit_APDS9960' 库来完成操作。 apds.readProximity();
读取接近开关检测到的距离。
六、代码
#include <ArduinoBLE.h>
#include "Ticker.h"
#include <Servo.h>
#include "Adafruit_APDS9960.h"
const int ledPin = LED_BUILTIN; // set ledPin to on-board LED
const int buttonPin = 4; // set buttonPin to digital pin 4
const int switchPin = 7;
const int INT_PIN = 2;
BLEService doorService("19B10010-E8F2-537E-4F6C-D104768A1214"); // create service
// create switch characteristic and allow remote device to read and write
BLEByteCharacteristic ledCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
// create button characteristic and allow remote device to get notifications
BLEByteCharacteristic buttonCharacteristic("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify);
BLEByteCharacteristic apds9960Characteristic("19B10013-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify);
void closeDoor();
Ticker timer1(closeDoor, 3000, 1);
Servo myservo;
int pos = 0;
Adafruit_APDS9960 apds;
void setup() {
// Serial.begin(9600);
// while (!Serial);
pinMode(INT_PIN, INPUT_PULLUP);
pinMode(ledPin, OUTPUT); // use the LED as an output
pinMode(buttonPin, INPUT); // use button pin as an input
pinMode(switchPin, INPUT_PULLUP);
if(!apds.begin()){
// Serial.println("failed to initialize device! Please check your wiring.");
}
// else Serial.println("Device initialized!");
//enable proximity mode
apds.enableProximity(true);
// //set the interrupt threshold to fire when proximity reading goes above 175
// apds.setProximityInterruptThreshold(0, 175);
// //enable the proximity interrupt
// apds.enableProximityInterrupt();
myservo.attach(9);
myservo.write(0);
// begin initialization
if (!BLE.begin()) {
// Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
// set the local name peripheral advertises
BLE.setLocalName("Door_ctrl");
// set the UUID for the service this peripheral advertises:
// BLE.setAdvertisedService(doorService);
// add the characteristics to the service
doorService.addCharacteristic(ledCharacteristic);
doorService.addCharacteristic(buttonCharacteristic);
doorService.addCharacteristic(apds9960Characteristic);
// add the service
BLE.addService(doorService);
ledCharacteristic.writeValue(0);
buttonCharacteristic.writeValue(0);
apds9960Characteristic.writeValue(0);
// start advertising
BLE.advertise();
// Serial.println("Bluetooth® device active, waiting for connections...");
}
void loop() {
// poll for Bluetooth® Low Energy events
BLE.poll();
timer1.update();
// read the current button pin state
char buttonValue = digitalRead(buttonPin);
// has the value changed since the last read
bool buttonChanged = (buttonCharacteristic.value() != buttonValue);
if (buttonChanged) {
// button state changed, update characteristics
ledCharacteristic.writeValue(buttonValue);
buttonCharacteristic.writeValue(buttonValue);
}
if (ledCharacteristic.written() || buttonChanged) {
// update LED, either central has written to characteristic or button state has changed
if (ledCharacteristic.value()) {
Serial.println("LED on");
// digitalWrite(ledPin, HIGH);
myservo.write(70);
timer1.start();
// Serial.println("Start timer!");
} else {
Serial.println("LED off");
// digitalWrite(ledPin, LOW);
}
}
int switchState = digitalRead(switchPin);
if (switchState == LOW) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
int value = apds.readProximity();
if(value > 100){
if(apds9960Characteristic.value() != 1){
apds9960Characteristic.writeValue(1);
}
}else{
if(apds9960Characteristic.value() != 0){
apds9960Characteristic.writeValue(0);
}
}
}
void closeDoor() {
// Serial.println("Timer end!");
myservo.write(0);
}
引用: Jacktang 发表于 2022-10-14 07:54 这个小门控制的还很听话,效果不错
谢谢