基于Arduino APDS9960 传感器的手势控制非接触式电梯
前言
在新冠病毒大流行的这段时间,电梯已成为每个人都触摸相同按钮的高风险场所。在许多地方,人们发现了防止接触电梯按钮的技巧,例如使用抽纸、牙签或卫生纸来按下电梯按钮。
这款非接触式电梯使用 Arduino Nano、APDS9960 手势传感器和 OLED 显示模块。使用这个基于手势的控制面板,您可以通过手势轻松控制您的 Lift。APDS9960 传感器用于读取手势。UP和DOWN手势用于设置楼层号,左侧手势关闭电梯门并根据楼层号移动电梯,右侧手势用于开门。
所需组件
Arduino Nano
OLED显示模块
APDS9960 RGB & 手势传感器
面包板
跳线
APDS9960 RGB & 手势传感器
APDS9960 RGB 和手势检测模块是一款小型分线板,配有内置 APDS-9960 传感器、UV 和 IR 阻挡滤光片、四个对不同方向敏感的独立二极管以及 I2C 兼容接口。该传感器可用于环境光和颜色测量、接近检测和非接触式手势感应。它的手势检测范围为 10 到 20 厘米,可用于控制微控制器、机器人和许多其他项目。
特征:
工作电压:2.4V 至 3.6V
操作范围:4-8 英寸(10-20 厘米)。
I2C 接口(I2C 地址:0x39)。
环境光和 RGB 颜色感应、接近
光学模块中的传感和手势检测
I2C 总线快速模式兼容接口,数据速率高达 400 kHz。
电路原理图
下面给出了使用 APDS9960 的非接触式电梯的电路图。
我们正在将 Arduino Nano 与 APDS9960 传感器和 OLED 显示器连接起来。APDS9960 Sensor 和 OLED Display 的 VCC 和 GND 引脚连接到 Arduino 的 3.3V 和 GND。而 APDS9960 Sensor 和 OLED Display 的 SCL 和 SDA 引脚分别连接到 Arduino 的 A5 和 A4 引脚。
VCC ==》3.3v
接地==》接地
SCL==》A5
SDA==》A4
这是使用 Arduino 的手势控制电梯的完整设置的外观:
代码说明
使用 APDS9960 的非接触式电梯的完整代码在页面末尾给出。在这里,我们将解释代码的一些重要部分。在这个程序中,我们将使用 APDS9960 和Adafruit_SH1106 库。可以从 Arduino IDE 下载 APDS9960 库。要下载库,请转到Sketch > Library Manager > Search ,然后输入Arduino APDS9960。而Adafruit_SH1106 库可以从这里下载。
因此,像往常一样通过包含所有必需的库来启动代码。Adafruit_SH1106.h是原始 Adafruit 库的修改版本。
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#include <Arduino_APDS9960.h>
在接下来的几行中,定义变量以存储用户想要去的当前楼层和楼层号。
int floornum=0;
int currentfloor=0;
之后,输入向上箭头、向下箭头、开门和关门图片的位图。可以使用Image2cpp 之类的转换器生成图像的 HEX 代码。
const unsigned char up [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,……………………………………………………………………………………..
};
const unsigned char down [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,……………………………………………………………………………………..
};
const unsigned char dooropen [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xc0, 0x1f, 0xff, 0xff, 0xe0, ,……………………………………………………………………………………..
};
在setup()函数中,以 9600 的波特率初始化串行监视器以进行调试。然后在接下来的几行中,使用begin()方法初始化 OLED 显示器和 APDS9960 传感器,如下所示:
Serial.begin(9600);
if (!APDS.begin()) {
Serial.println("Error initializing APDS9960 sensor!");
}
Serial.println("Detecting gestures ...");
display.begin(SH1106_SWITCHCAPVCC, 0x3C);
初始化显示器和传感器后,使用clearDisplay()方法清除显示缓冲区,并使用display.setTextSize()和display.setTextColor() 方法设置字体大小和颜色。
display.setTextSize(2);
display.setTextColor(WHITE);
display.clearDisplay();
display.display();
在void loop()内部,不断检查是否做出任何手势。如果是,则读取手势值并检查它是哪个手势(上、下、右、左)并在串行监视器上打印相应的读数。UP 和 DOWN 手势用于设置用户想去的楼层。左手势是关闭电梯门并根据楼层号移动电梯,而右手势用于开门。
if (APDS.gestureAvailable()) {
int gesture = APDS.readGesture();
switch (gesture) {
case GESTURE_UP:
Serial.println("Detected UP gesture");
display.clearDisplay();
floornum ++;
home1();
break;
case GESTURE_DOWN:
Serial.println("Detected DOWN gesture");
display.clearDisplay();
floornum --;
home1();
break;
case GESTURE_LEFT:
Serial.println("Detected LEFT gesture");
display.clearDisplay();
start();
break;
case GESTURE_RIGHT:
Serial.println("Detected RIGHT gesture");
display.clearDisplay();
home1();
break;
default:
break;
}
home1 ()函数用于绘制电梯的主页显示。这包括向上箭头、向下箭头、开门、关门标志和当前楼层号。drawBitmap() 函数用于在 OLED 显示器上绘制图像。drawBitmap() 函数的语法如下:
drawBitmap(int16_t x, int16_t y, bitmap, int16_t w, int16_t h, colour);
在哪里:
int16_t x, int16_t y 是OLED显示屏的X和Y坐标
位图是位图的名称
int16_t w, int16_t h是图像的高度和重量。
void home1()
{
display.setCursor(101,23);
display.println(floornum);
display.drawBitmap(23, 0, uparrow, 40, 18, WHITE);
display.drawBitmap(26, 46, downarrow, 40, 18, WHITE);
display.drawBitmap(0, 15, dooropen, 29, 30, WHITE);
display.drawBitmap(60, 15, closedoor, 29, 30, WHITE);
display.display();
}
start()函数用于向上或向下移动电梯。为此,将当前楼层号与用户想去的楼层号进行比较。如果楼层号大于当前楼层号。如果楼层号小于当前楼层号,则电梯将向上移动。然后电梯将向下移动。当两个当前楼层都没有时,电梯将停止。和楼层号。是相同的。
void start()
{
while(floornum > currentfloor){
Serial.println("going UP ");
currentfloor++;
display.drawBitmap(0, 0, up, 100, 64, WHITE);
display.setCursor(101,23);
display.println(currentfloor);
display.display();
display.clearDisplay();
delay(2000);
}
while(floornum < currentfloor){
Serial.println("going Down ");
currentfloor--;
display.drawBitmap(0, 0, down, 100, 64, WHITE);
display.setCursor(101,23);
display.println(currentfloor);
display.display();
display.clearDisplay();
delay(2000);
}
if(floornum== currentfloor){
Serial.println("Reached");
display.clearDisplay();
home1();
Serial.print(currentfloor);
}
}
测试手势控制的非接触式升降机
硬件和代码准备就绪后,将 Arduino Nano 连接到笔记本电脑并上传下面给出的完整代码。如您所见,默认情况下 OLED 将显示电梯用户界面。
现在向上或向下挥动你的手,如下面的视频所示,设置你想去的地板。然后做出确认电梯到该楼层的向左手势。如果您想停止电梯,请用手做出正确的手势。
完整代码
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#include <Arduino_APDS9960.h>
#define OLED_RESET -1
Adafruit_SH1106 display(OLED_RESET);
int floornum=0;
int currentfloor=0;
//Paste your bitmap here
const unsigned char up [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0f, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xf0,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, 0xff, 0xc7, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x07, 0xff, 0x81, 0xff, 0xc0, 0x00, 0x00,