步驟1:組件
1。 Arduino UNO
2。 usb cable
3. APDS9960手勢(shì)傳感器
4。 24 led neopixel led ring
5. 雄性 - 男性,男性 - 男性面包板電纜
6。 面包板
7. LED環(huán)的5 V電源(我正在使用4節(jié)電池)
8 。 要將新像素環(huán)連接到面包板,您需要將三個(gè)公引腳焊接到它:GND,PWR和控制引腳。為此你需要一個(gè)烙鐵和助焊劑
這里的主要部件是APDS-9960手勢(shì)傳感器和24個(gè)新像素環(huán)。您可以根據(jù)需要切換不同的arduinos,usb線纜電源和面包板。
步驟2:組裝和上傳
在開始之前,請(qǐng)確保您擁有所有組件。我們將有一些很好的步驟:)我還將Fritzing原理圖作為圖片和fritzing格式附加。
1。將3個(gè)公引腳焊接到新像素環(huán)(GND,PWR,控制引腳)
2。將新像素環(huán)連接到面包板上
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。手勢(shì)傳感器庫
如果您不知道如何安裝arduino庫,請(qǐng)查看本教程。
在下一節(jié)中,我將把代碼直接嵌入到本教程中,所以如果你愿意,你可以從那里復(fù)制并粘貼它。
最后使用usb線將arduino連接到電腦,將1.5伏電池放入電池組,然后將草圖上傳到arduino。
第3步:它是如何工作的?
在最后一部分中,我們將學(xué)習(xí)如何將這些組件組合在一起,如何使用它們的庫以及我如何使用它們構(gòu)建我的代碼:
首先讓我們快速瀏覽一下傳感器和我們將使用的neopixel庫API方法
1 。來自adafruit的 Neopixel API
從這個(gè)庫我們將使用控制單個(gè)led顏色的方法并應(yīng)用它們
- 包括庫:
#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 # 。..。
}
- 點(diǎn)亮單個(gè)像素然后應(yīng)用所有修改條帶(以某種方式呈現(xiàn))
# 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手勢(shì)傳感器
從這個(gè)庫我們將使用“讀取手勢(shì)”功能。此功能將能夠區(qū)分左右,上下,近遠(yuǎn)命令。這里有一個(gè)技巧,我們不會(huì)連續(xù)詢問傳感器的最后一個(gè)手勢(shì)。電路板能夠通過已發(fā)現(xiàn)手勢(shì)的中斷“ping”。
- 包括庫,類似于neopixel
- 將庫聲明為中斷引腳,和中斷標(biāo)志
#define APDS9960_INT 2
SparkFun_APDS9960 apds = SparkFun_APDS9960();
int isr_flag = 0;
- 初始化庫,通常在設(shè)置函數(shù)內(nèi)
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
}
- 定義中斷函數(shù),這里我們只設(shè)置一個(gè)flag
void interruptRoutine() {
isr_flag = 1;
}
- 在循環(huán)函數(shù)內(nèi)部定期檢查標(biāo)志以查看是否已檢測(cè)到手勢(shì)
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()函數(shù)我們?cè)谀睦锟梢砸笞詈笠粋€(gè)手勢(shì)
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;
}
}
現(xiàn)在讓我們看看整個(gè)代碼的運(yùn)行情況:
所以我已經(jīng)解釋了手勢(shì)傳感器的基本API和新像素環(huán)現(xiàn)在讓我們把事情放在一起:
算法運(yùn)行如下:
- 初始化庫(參見上面的代碼)
- 創(chuàng)建一個(gè)led數(shù)組強(qiáng)度被稱為“l(fā)edStates”。該陣列將包含24個(gè)LED強(qiáng)度,以150到2的遞減方式排列
- 在主循環(huán)內(nèi)部檢查中斷引腳是否已被修改,如果是,則需要更改LED的動(dòng)畫或顏色
- “handleGesture()”函數(shù)檢查最后一個(gè)手勢(shì)并為UP -DOWN手勢(shì)調(diào)用函數(shù)“toggleColor”或?yàn)長(zhǎng)EFT - RIGHT手勢(shì)設(shè)置全局變量“l(fā)edDirection”
- “toggleColor()”函數(shù)只是改變一個(gè)名為“colorSelection”的全局變量,其中一個(gè)值為0,1,2
- 在主循環(huán)函數(shù)中也有另一個(gè)名為“animateLeds();”的函數(shù)。叫做。此函數(shù)檢查是否超過100毫秒,如果是,則使用“rotateLeds()”函數(shù)旋轉(zhuǎn)LED,然后重新繪制它們
- “rotateLeds()”將向前或向后“旋轉(zhuǎn)”LED使用另一個(gè)名為“intermediateLedStates”的數(shù)組。
旋轉(zhuǎn)“效果”將如下所示:
# 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
首先創(chuàng)建新陣列并復(fù)制新位置上的舊led強(qiáng)度(增加位置)或減少它)。之后用“intermediateLedStates”覆蓋“l(fā)edStates”數(shù)組,這樣過程將在100毫秒后繼續(xù)。
#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 “l(fā)edStates” are copied on “intermediateLedStates” like so
* let‘s sat the “l(fā)edStates” array is {100, 80, 60, 0, 0, 0} and the ledDirection is 1
* then after this function is called “l(fā)edStates” 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
+關(guān)注
關(guān)注
242文章
23691瀏覽量
670760 -
Arduino
+關(guān)注
關(guān)注
188文章
6490瀏覽量
190068 -
手勢(shì)傳感器
+關(guān)注
關(guān)注
1文章
13瀏覽量
12919
發(fā)布評(píng)論請(qǐng)先 登錄
用示波器測(cè)量傳感器的放大電路及供電

環(huán)型壓力傳感器的研究和應(yīng)用

Cortex-A55國(guó)產(chǎn)處理器_教學(xué)實(shí)驗(yàn)箱_操作案例分享:5-21 手勢(shì)識(shí)別實(shí)驗(yàn)
觸摸傳感器的選擇與設(shè)計(jì)解決方案

使用OPT4003-Q1環(huán)境光傳感器進(jìn)行光源檢測(cè)

基于Arduino的紫外線傳感器設(shè)計(jì) 紫外線傳感器的工作原理和分類

安森美收購SWIR傳感器公司以增強(qiáng)智能傳感器產(chǎn)品組合

基于PIR傳感器的電路圖 帶Arduino的PIR傳感器設(shè)計(jì)

怎么用萬用表測(cè)量溫度傳感器的好壞
溫度傳感器怎樣根據(jù)測(cè)量環(huán)境選擇參數(shù)
鴻蒙ArkTS聲明式開發(fā):跨平臺(tái)支持列表 組合手勢(shì)

評(píng)論