<acronym id="s8ci2"><small id="s8ci2"></small></acronym>
<rt id="s8ci2"></rt><rt id="s8ci2"><optgroup id="s8ci2"></optgroup></rt>
<acronym id="s8ci2"></acronym>
<acronym id="s8ci2"><center id="s8ci2"></center></acronym>
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

    瀏覽量

    184848
  • 手勢傳感器
    +關注

    關注

    1

    文章

    13

    瀏覽量

    12767
收藏 人收藏

    評論

    相關推薦

    如何連接Arduino聲音傳感器以控制帶有聲音的LED

    在本教程中,您將學習如何連接Arduino聲音傳感器以控制帶有聲音的LED。在本指南結束時,您將擁有一個可以正常工作的聲控LED!
    的頭像 發表于 02-11 10:21 ?1037次閱讀
    如何連接<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 ?2469次閱讀

    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 手勢傳感器的光學機械部分提供設計指南?;诩t外(IR)技術的手勢檢測系統存在一個關鍵的設計問題,即 LED
    的頭像 發表于 06-09 18:15 ?411次閱讀
    汽車<b class='flag-5'>手勢</b><b class='flag-5'>傳感器</b>如何克服光學串擾?

    汽車手勢傳感器的玻璃蓋片和孔徑設計

    本應用筆記為 MAX25205?和 MAX25405?手勢傳感器的光學機械部分提供設計指南?;诩t外(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
    亚洲欧美日韩精品久久_久久精品AⅤ无码中文_日本中文字幕有码在线播放_亚洲视频高清不卡在线观看
    <acronym id="s8ci2"><small id="s8ci2"></small></acronym>
    <rt id="s8ci2"></rt><rt id="s8ci2"><optgroup id="s8ci2"></optgroup></rt>
    <acronym id="s8ci2"></acronym>
    <acronym id="s8ci2"><center id="s8ci2"></center></acronym>