0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

怎样用Arduino将手势传感器和LED环组合

454398 来源:工程师吴畏 2019-08-14 10:33 次阅读

步骤1:组件

1。 Arduino UNO

2。 usb cable

3. APDS9960手势传感器

4。 24 led neopixel led ring

5. 雄性 - 男性,男性 - 男性面包板电缆

6。 面包板

7. LED环的5 V电源(我正在使用4节电池)

8 。 要将新像素环连接到面包板,您需要将三个公引脚焊接到它:GND,PWR和控制引脚。为此你需要一个烙铁和助焊剂

这里的主要部件是APDS-9960手势传感器和24个新像素环。您可以根据需要切换不同的arduinos,usb线缆电源和面包板。

步骤2:组装和上传

汇编

在开始之前,请确保您拥有所有组件。我们将有一些很好的步骤:)我还将Fritzing原理图作为图片和fritzing格式附加。

1。将3个公引脚焊接到新像素环(GND,PWR,控制引脚)

2。将新像素环连接到面包板上

3。将APDS9960传感器连接到面包板

4。接地:电池组,arduino UNO,APDS9960和neopixel到面包板地面

5。连接电源:arduino UNO 3V至APDS9960电源引脚,neopixel至电池组电源

6。将neopixel控制引脚连接到arduino D6引脚

7。将APDS9960的SDA和SCL分别连接到A4和A5

8。将APDS9960中断引脚连接到arduino D2

代码上传

首先,您需要下载并安装必要的arduino库:

1。 Neopixel ring library

2。手势传感器库

如果您不知道如何安装arduino库,请查看本教程

在下一节中,我将把代码直接嵌入到本教程中,所以如果你愿意,你可以从那里复制并粘贴它。

最后使用usb线将arduino连接到电脑,将1.5伏电池放入电池组,然后将草图上传到arduino。

第3步:它是如何工作的?

在最后一部分中,我们将学习如何将这些组件组合在一起,如何使用它们的库以及我如何使用它们构建我的代码:

首先让我们快速浏览一下传感器和我们将使用的neopixel库API方法

1 。来自adafruit的 Neopixel API

从这个库我们将使用控制单个led颜色的方法并应用它们

- 包括库:

#include

- 声明库

#define NEOPIXED_CONTROL_PIN 6

#define NUM_LEDS 24

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, NEOPIXED_CONTROL_PIN, NEO_RBG + NEO_KHZ800);

- 初始化

#typically inside the setup block

void setup() {

strip.begin();

# maybe some other stuff here # 。..。

}

- 点亮单个像素然后应用所有修改条带(以某种方式呈现)

# set up pixel 0 to be red

strip.setPixelColor(0, strip.Color(255, 0, 0));

# set up pixel 1 to be green

strip.setPixelColor(1, strip.Color(0, 255, 0));

# set up pixel 2 to be blue

strip.setPixelColor(2, strip.Color(0, 0 255));

strip.show();

2。 APDS 9960手势传感器

从这个库我们将使用“读取手势”功能。此功能将能够区分左右,上下,近远命令。这里有一个技巧,我们不会连续询问传感器的最后一个手势。电路板能够通过已发现手势的中断“ping”。

- 包括库,类似于neopixel

- 将库声明为中断引脚,和中断标志

#define APDS9960_INT 2

SparkFun_APDS9960 apds = SparkFun_APDS9960();

int isr_flag = 0;

- 初始化库,通常在设置函数内

void setup()

{

# declare the interrupt pin as INPUT and attach a function to it

pinMode(APDS9960_INT, INPUT);

attachInterrupt(0, interruptRoutine, FALLING);

if ( apds.init() && apds.enableGestureSensor(true)) {

Serial.println(“APDS-9960 initialization complete”);

} else {

Serial.println(“Something went wrong during APDS-9960 init!”);

}

# initialize other things maybe

}

- 定义中断函数,这里我们只设置一个flag

void interruptRoutine() {

isr_flag = 1;

}

- 在循环函数内部定期检查标志以查看是否已检测到手势

void loop()

{

# check the flag

if( isr_flag == 1 ) {

# if the flag is set, remove the interrupt, make the necessary processing inside handleGesture() function

# and then reset the flag and reattach the interrupt

detachInterrupt(0);

handleGesture();

isr_flag = 0;

attachInterrupt(0, interruptRoutine, FALLING);

}

# some other code here maybe

}

- 定义handleGesture()函数我们在哪里可以要求最后一个手势

void handleGesture() {

# if no gesture is avalible return, this is only a safe check

if ( !apds.isGestureAvailable() ) {

return;

}

# reads the last gesture, compares with the known ones and print a message

switch ( apds.readGesture() ) {

case DIR_UP:

Serial.println(“UP”);

break;

case DIR_DOWN:

Serial.println(“DOWN”);

break;

case DIR_LEFT:

Serial.println(“LEFT”);

break;

case DIR_RIGHT:

Serial.println(“RIGHT”);

break;

case DIR_FAR:

Serial.println(“FAR”);

break;

}

}

现在让我们看看整个代码的运行情况:

所以我已经解释了手势传感器的基本API和新像素环现在让我们把事情放在一起:

算法运行如下:

- 初始化库(参见上面的代码)

- 创建一个led数组强度被称为“ledStates”。该阵列将包含24个LED强度,以150到2的递减方式排列

- 在主循环内部检查中断引脚是否已被修改,如果是,则需要更改LED的动画或颜色

- “handleGesture()”函数检查最后一个手势并为UP -DOWN手势调用函数“toggleColor”或为LEFT - RIGHT手势设置全局变量“ledDirection”

- “toggleColor()”函数只是改变一个名为“colorSelection”的全局变量,其中一个值为0,1,2

- 在主循环函数中也有另一个名为“animateLeds();”的函数。叫做。此函数检查是否超过100毫秒,如果是,则使用“rotateLeds()”函数旋转LED,然后重新绘制它们

- “rotateLeds()”将向前或向后“旋转”LED使用另一个名为“intermediateLedStates”的数组。

旋转“效果”将如下所示:

# after initialization

{150, 100, 70, 50, 40, 30, 10, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

# after rotateLeds() is called

{0, 150, 100, 70, 50, 40, 30, 10, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

# after rotateLeds() is called again

{0, 0, 150, 100, 70, 50, 40, 30, 10, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

# and so on

首先创建新阵列并复制新位置上的旧led强度(增加位置)或减少它)。之后用“intermediateLedStates”覆盖“ledStates”数组,这样过程将在100毫秒后继续。

#include “SparkFun_APDS9960.h”

#include “Adafruit_NeoPixel.h”

#include “Wire.h”

#define NEOPIXED_CONTROL_PIN 6

#define NUM_LEDS 24

#define APDS9960_INT 2

#define LED_SPEED_STEP_INTERVAL 100

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, NEOPIXED_CONTROL_PIN, NEO_RBG + NEO_KHZ800);

SparkFun_APDS9960 apds = SparkFun_APDS9960();

unsigned long lastLedChangeTime = 0;

short ledDirection = 0;

short colorSelection = 0;

byte ledStates[] = {150, 100, 70, 50, 40, 30, 10, 2, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

int isr_flag = 0;

void setup()

{

Serial.begin(9600);

Serial.println(“Program started”);

strip.begin();

pinMode(APDS9960_INT, INPUT);

attachInterrupt(0, interruptRoutine, FALLING);

if ( apds.init() && apds.enableGestureSensor(true)) {

Serial.println(“APDS-9960 initialization complete”);

} else {

Serial.println(“Something went wrong during APDS-9960 init!”);

}

lastLedChangeTime = millis();

Serial.println(“Init succesfully”);

}

void loop()

{

if( isr_flag == 1 ) {

detachInterrupt(0);

handleGesture();

isr_flag = 0;

attachInterrupt(0, interruptRoutine, FALLING);

}

animateLeds();

}

void interruptRoutine()

{

isr_flag = 1;

}

/**

* This will handle gestures from the APDS9960 sensor

* Up and Down gestures will call toggleColor function

* Left and Right gestures will change the led animation

*/

void handleGesture() {

if ( !apds.isGestureAvailable() ) {

return;

}

switch ( apds.readGesture() ) {

case DIR_UP:

Serial.println(“UP”);

toggleColor();

break;

case DIR_DOWN:

Serial.println(“DOWN”);

toggleColor();

break;

case DIR_LEFT:

ledDirection = 1;

Serial.println(“LEFT”);

break;

case DIR_RIGHT:

ledDirection = -1;

Serial.println(“RIGHT”);

break;

case DIR_FAR:

ledDirection = 0;

Serial.println(“FAR”);

break;

}

}

/**

* Change current leds color

* Each time this function is called will change the leds state

*/

void toggleColor()

{

if (colorSelection == 0) {

colorSelection = 1;

} else if (colorSelection == 1) {

colorSelection = 2;

} else {

colorSelection = 0;

}

}

/**

* The animation will run after LED_SPEED_STEP_INTERVAL millis

* First the rotateLeds function is called, then the leds colors are set using the strip api

*/

void animateLeds()

{

if (millis() - lastLedChangeTime 《 LED_SPEED_STEP_INTERVAL) {

return;

}

rotateLeds();

for (int i=0; i 《 NUM_LEDS; i++) {

strip.setPixelColor(i, getColor(ledStates[i]));

strip.show();

}

lastLedChangeTime = millis();

}

/**

* Using a secondary array “intermediateLedStates”, leds intensities are animated

* First the values from “ledStates” are copied on “intermediateLedStates” like so

* let‘s sat the “ledStates” array is {100, 80, 60, 0, 0, 0} and the ledDirection is 1

* then after this function is called “ledStates” array is {0, 100, 80, 60, 0, 0} simulating a rotation effect

*/

void rotateLeds()

{

byte intermediateLedStates[NUM_LEDS];

for (int i=0; i 《 NUM_LEDS; i++) {

intermediateLedStates[i] = 0;

}

for (int i=0; i 《 NUM_LEDS; i++) {

if (ledDirection == 1) {

if (i == NUM_LEDS -1) {

intermediateLedStates[0] = ledStates[i];

} else {

intermediateLedStates[i + 1] = ledStates[i];

}

} else {

if (i == 0) {

intermediateLedStates[NUM_LEDS - 1] = ledStates[i];

} else {

intermediateLedStates[i - 1] = ledStates[i];

}

}

}

for (int i=0; i 《 NUM_LEDS; i++) {

ledStates[i] = intermediateLedStates[i];

}

}

uint32_t getColor(int intensity)

{

switch (colorSelection) {

case 0:

return strip.Color(intensity, 0, 0);

case 1:

return strip.Color(0, intensity, 0);

default:

return strip.Color(0, 0, intensity);

}

}

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • led
    led
    +关注

    关注

    237

    文章

    22455

    浏览量

    645897
  • Arduino
    +关注

    关注

    184

    文章

    6428

    浏览量

    184852
  • 手势传感器
    +关注

    关注

    1

    文章

    13

    浏览量

    12767
收藏 人收藏

    评论

    相关推荐

    如何连接Arduino声音传感器以控制带有声音的LED

    在本教程中,您将学习如何连接Arduino声音传感器以控制带有声音的LED。在本指南结束时,您将拥有一个可以正常工作的声控LED
    的头像 发表于 02-11 10:21 1046次阅读
    如何连接<b class='flag-5'>Arduino</b>声音<b class='flag-5'>传感器</b>以控制带有声音的<b class='flag-5'>LED</b>

    怎样用32单片机测电压?

    怎样用32单片机测电压
    发表于 10-31 07:09

    一般的手势传感器有哪些型号?

    你们都是使用什么型号的手势传感器
    发表于 10-30 06:08

    Arduino的各种传感器树莓派也可以吗?

    Arduino的各种传感器,树莓派也可以么?
    发表于 09-28 08:13

    使用Arduino和PAJ7620手势传感器制作手势控制机器人

    使用Arduino和PAJ7620手势传感器制作手势控制机器人,简单程序即可实现。小小的传感器可以识别各种
    发表于 09-27 06:17

    基于arduino设计的手势控制小车

    基于arduino手势控制小车
    发表于 09-25 06:06

    手势识别传感器是如何工作的?

    电子发烧友网报道(文/黄山明)手势识别传感器,顾名思义是一种能够对用户手势动作进行识别的传感器手势识别
    的头像 发表于 09-22 01:23 2470次阅读

    HarmonyOS/OpenHarmony(Stage模型)应用开发组合手势(一)连续识别

    组合手势由多种单一手势组合而成,通过在GestureGroup中使用不同的GestureMode来声明该组合
    发表于 09-07 15:20

    Arduino供电、传感器控制的褪色LED灯带

    电子发烧友网站提供《Arduino供电、传感器控制的褪色LED灯带.zip》资料免费下载
    发表于 07-11 14:24 0次下载
    <b class='flag-5'>Arduino</b>供电、<b class='flag-5'>传感器</b>控制的褪色<b class='flag-5'>LED</b>灯带

    如何在Arduino中使用APDS9960手势传感器

    电子发烧友网站提供《如何在Arduino中使用APDS9960手势传感器.zip》资料免费下载
    发表于 06-28 16:01 0次下载
    如何在<b class='flag-5'>Arduino</b>中使用APDS9960<b class='flag-5'>手势</b><b class='flag-5'>传感器</b>

    使用BH1750和Arduino的黑暗传感器LED

    电子发烧友网站提供《使用BH1750和Arduino的黑暗传感器LED.zip》资料免费下载
    发表于 06-27 15:14 1次下载
    使用BH1750和<b class='flag-5'>Arduino</b>的黑暗<b class='flag-5'>传感器</b>和<b class='flag-5'>LED</b>

    汽车手势传感器如何克服光学串扰?

    本文为 MAX25205 和 MAX25405 手势传感器的光学机械部分提供设计指南。基于红外(IR)技术的手势检测系统存在一个关键的设计问题,即 LED
    的头像 发表于 06-09 18:15 412次阅读
    汽车<b class='flag-5'>手势</b><b class='flag-5'>传感器</b>如何克服光学串扰?

    汽车手势传感器的玻璃盖片和孔径设计

    本应用笔记为 MAX25205 和 MAX25405 手势传感器的光学机械部分提供设计指南。基于红外(IR)技术的手势检测系统存在一个关键的设计问题,即 LED
    的头像 发表于 06-08 11:50 661次阅读
    汽车<b class='flag-5'>手势</b><b class='flag-5'>传感器</b>的玻璃盖片和孔径设计

    NodeMCU如何组合并为CO2传感器和OLED显示供电?

    我对电子一窍不通。我几周前才了解微控制Arduino 等。 我刚收到 Senseair S8 CO2 传感器,正在等待。 并未展示如何组合并为 CO2
    发表于 06-02 07:58

    如何使用可穿戴传感器和ESP2866驾驶鹦鹉无人机?

    可以使用可穿戴传感器轻松实现手势识别。凭借 node.js 和 Arduino 硬件的灵活性,我们现在正在扩展到不同的例,并想出将自然用户界面 (NUI) 和软体动力学 (SBD)
    发表于 05-23 07:58