Cov txheej txheem:

Tswj Neopixel Coj Lub Nplhaib Nrog Gesture Sensor: 3 Kauj Ruam (nrog Duab)
Tswj Neopixel Coj Lub Nplhaib Nrog Gesture Sensor: 3 Kauj Ruam (nrog Duab)

Video: Tswj Neopixel Coj Lub Nplhaib Nrog Gesture Sensor: 3 Kauj Ruam (nrog Duab)

Video: Tswj Neopixel Coj Lub Nplhaib Nrog Gesture Sensor: 3 Kauj Ruam (nrog Duab)
Video: Tswj Tus Kheej - Koomtes Lis (Official Audio) 2024, Kaum ib hlis
Anonim
Image
Image
Sib dhos & Upload
Sib dhos & Upload

Hauv qhov kev qhia no peb yuav ua si nrog lub ntsej muag piav tes piav taw (APDS-9960) thiab lub nplhaib neopixel kom kawm paub yuav ua li cas ua ke ob qho tib si siv Arduino UNO.

Cov khoom kawg yuav teb rau sab laug - sab xis piav tes piav taw los ntawm kev coj ua coj mus rau sab xis lossis sab laug, thiab nce mus -nqis tes taw los ntawm kev hloov xim LED.

Hauv cov kauj ruam tom ntej, koj yuav piav qhia luv luv ntawm cov npe thiab yuav ua li cas txuas cov khoom siv. Thiab tom qab ntawd peb yuav tshuaj xyuas cov cai ib kauj ruam los kawm seb nws ua haujlwm li cas.

Kauj Ruam 1: Cheeb Tsam

1. Arduino UNO

2. usb cable

3. APDS9960 piav tes piav taw (https://www.sparkfun.com/products/12787)

4. 24 coj neopixel coj lub nplhaib (https://www.adafruit.com/product/1586)

5. txiv neej-poj niam, txiv neej-txiv neej breadboard cables

6. qhob cij

7. 5 V fais fab mov rau lub nplhaib coj (kuv siv 4 lub roj teeb rov qab)

8. Txhawm rau txuas lub nplhaib neopixel rau lub khob cij koj yuav tsum tau muab peb tus txiv neej txuas rau nws: GND, PWR, thiab tswj tus pin. Rau qhov no koj yuav xav tau cov hlau txuas thiab flux

Cov khoom tseem ceeb ntawm no yog APDS-9960 piav tes piav tes piav tes thiab lub nplhaib 24 neopixel. Koj tuaj yeem hloov pauv arduinos sib txawv, cov khoom siv usb cov khoom siv hluav taws xob thiab cov laug cam raws li koj xav tau.

Kauj ruam 2: Sib dhos & Upload

Los ua ke

Ua ntej koj yuav pib ua kom ntseeg tau tias koj muaj tag nrho cov khoom ntawm koj lub rooj. Peb yuav muaj qee cov kauj ruam zoo ua raws:). Kuv kuj tau txuas Fritzing schematic raws li daim duab thiab tseem nyob hauv hom fritzing.

1. Solder 3 tus pin txiv neej rau lub nplhaib neopixel (GND, PWR, tus pin tswj)

2. muab lub nplhaib neopixel txuas rau ntawm lub khob cij

3. txuas lub APDS9960 sensor mus rau lub khob cij

4. txuas thaj chaw: roj teeb pob, arduino UNO, APDS9960 thiab neopixel mus rau hauv av ua khob cij

5. txuas lub zog: arduino UNO 3V rau APDS9960 tus pin fais fab, neopixel rau roj teeb pob

6. txuas tus neopixel tswj tus pin rau arduino D6 tus pin

7. txuas SDA thiab SCL ntawm APDS9960 mus rau A4 thiab A5 feem

8. txuas tus APDS9960 cuam tshuam tus pin rau arduino D2

Hloov cov chaws

Ua ntej tshaj plaws koj yuav tsum rub tawm thiab teeb tsa cov tsev qiv ntawv arduino tsim nyog:

1. Neopixel ring library:

2. Lub tsev qiv ntawv piav tes piav taw:

Yog tias koj tsis paub yuav teeb tsa lub tsev qiv ntawv arduino txheeb xyuas qhov kev qhia no li cas.

Tom qab koj tau rub tawm thiab teeb tsa cov tsev qiv ntawv saum toj no, koj tuaj yeem clone lossis rub tawm kuv cov chaw cia khoom arduino nyob ntawm no: https://github.com/danionescu0/arduino, thiab peb yuav siv daim duab no: https://github.com/danionescu0 /arduino/tsob ntoo/tus tswv/cov haujlwm/neopixel_ring_gestures

Hauv ntu tom ntej no kuv yuav sau txoj cai ncaj qha rau hauv qhov kev qhia no, yog li yog tias koj nyiam koj tuaj yeem luam thiab muab tshuaj txhuam los ntawm qhov ntawd.

Thaum kawg txuas lub arduino lub khoos phis tawj siv lub usb cable, tso 1.5 v roj teeb rau hauv lub roj teeb pob, thiab xa cov duab kos rau hauv arduino.

Kauj Ruam 3: Nws Ua Haujlwm Li Cas?

Hauv ntu kawg no peb yuav kawm paub tias cov khoom no sib koom ua ke li cas, yuav siv lawv lub tsev qiv ntawv li cas thiab kuv tau teeb tsa kuv li cas:

Ua ntej peb tau txais nrawm nrawm los ntawm lub ntsuas pa thiab neopixel lub tsev qiv ntawv API txoj hauv kev uas peb yuav siv

1. Neopixel API los ntawm adafruit

Los ntawm lub tsev qiv ntawv no peb yuav siv txoj hauv kev uas tswj tus kheej coj tus xim thiab siv lawv

- suav nrog lub tsev qiv ntawv:

#suav nrog

- tshaj tawm lub tsev qiv ntawv

#define NEOPIXED_CONTROL_PIN 6

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

- pib

#typically sab hauv teeb teeb tsa

teeb tsa tsis muaj dabtsis () {strip.begin (); # tej zaum muaj lwm yam khoom ntawm no #…. }

- teeb ib tus neeg pixels tom qab ntawd siv txhua qhov kev hloov pauv mus rau kab txaij (ua nws txoj hauv kev)

# teeb tsa pixel 0 kom liab

strip.setPixelColor (0, strip. Color (255, 0, 0)); # teeb tsa pixel 1 kom ntsuab ntsuab.setPixelColor (1, strip. Color (0, 255, 0)); # teeb tsa pixel 2 kom xiav strip.setPixelColor (2, strip. Color (0, 0 255)); strip.show ();

2. APDS 9960 piav tes piav taw

Los ntawm lub tsev qiv ntawv no peb yuav siv qhov "nyeem taw tes" ua haujlwm. Txoj haujlwm no yuav tuaj yeem paub qhov txawv ntawm sab laug-sab xis, nce-nqis, kaw-nyob deb cov lus txib. Muaj qhov ua kom yuam kev ntawm no, peb yuav tsis nug tus ntsuas tsis tu ncua rau qhov kev piav tes piav taw zaum kawg. Pawg thawj coj muaj peev xwm "ping" los ntawm kev cuam tshuam uas tau pom lub ntsej muag.

- suav nrog lub tsev qiv ntawv, zoo ib yam li neopixel

- tshaj tawm lub tsev qiv ntawv cuam tshuam tus pin, thiab cuam tshuam tus chij

#define APDS9960_INT 2

SparkFun_APDS9960 apds = SparkFun_APDS9960 (); int isr_flag = 0;

- pib lub tsev qiv ntawv, feem ntau hauv kev teeb tsa

void teeb tsa ()

{ # tshaj tawm qhov cuam tshuam tus pin li INPUT thiab txuas lub luag haujlwm rau nws pinMode (APDS9960_INT, INPUT); attachInterrupt (0, cuam tshuam rau kev rov ua dua tshiab, poob); yog (apds.init () && apds.enableGestureSensor (tseeb)) {Serial.println ("APDS-9960 initialization complete"); } lwm {Serial.println ("Muaj qee yam tsis raug thaum lub sijhawm APDS-9960 pib!"); } # pib lwm yam tej zaum tej zaum}

- txhais cov haujlwm cuam tshuam, ntawm no peb yuav teeb tsa tus chij nkaus xwb

void interruptRoutine () {

isr_flag = 1; }

- sab hauv lub voj ua haujlwm kos tus chij ib ntus kom pom tias tau piav tes piav taw

void lub voj ()

{ # khij tus chij yog (isr_flag == 1) { # yog tias tus chij tau teeb tsa, tshem qhov cuam tshuam, ua qhov tsim nyog ua hauv handleGesture () muaj nuj nqi # thiab tom qab ntawd rov pib dua tus chij thiab rov txuas qhov cuam tshuam cuam tshuam detachInterrupt (0); handleGesture (); isr_flag = 0; attachInterrupt (0, cuam tshuam kev rov ua dua tshiab, poob); } # qee qhov chaws nyob ntawm no tej zaum}

- txhais handleGesture () muaj nuj nqi qhov twg peb tuaj yeem thov kom piav tes piav taw kawg

tsis muaj dab tsi handleGesture () {

# yog tias tsis muaj kev piav tes piav taw tuaj yeem rov qab los, qhov no tsuas yog kev nyab xeeb tshawb xyuas yog (! apds.isGestureAvailable ()) {rov qab; } # nyeem qhov taw tes kawg, sib piv nrog cov uas paub thiab luam tawm cov lus hloov (apds.readGesture ()) {case DIR_UP: Serial.println ("UP"); tawg; rooj plaub DIR_DOWN: Serial.println ("DOWN"); tawg; rooj plaub DIR_LEFT: Serial.println ("LEFT"); tawg; rooj plaub DIR_RIGHT: Serial.println ("RIGHT"); tawg; rooj plaub DIR_FAR: Serial.println ("FAR"); tawg; }}

Tam sim no cia saib tag nrho cov cai hauv kev nqis tes ua:

Yog li kuv tau piav qhia lub hauv paus API ntawm lub ntsej muag ntsuas lub ntsej muag thiab lub nplhaib neopixel, tam sim no cia peb tso khoom ua ke:

Lub algorithm khiav zoo li no:

- pib lub tsev qiv ntawv (saib cov cai saum toj no)

- tsim cov kab ke ntawm kev ua kom muaj zog hu ua "ledStates". Cov kab no yuav suav nrog 24 coj qhov hnyav uas tau teeb tsa los ntawm 150 txog 2

- sab hauv lub voj tseem ceeb xyuas yog tias tus pin cuam tshuam tau hloov pauv yog li ntawd nws yog lub sijhawm los hloov coj cov duab lossis xim

- qhov "handleGesture ()" muaj nuj nqi tshawb xyuas lub ntsej muag kawg thiab hu lub luag haujlwm "toggleColor" rau UP -DOWN piav tes piav taw lossis teeb tsa lub ntiaj teb sib txawv "ledDirection" rau LEFT - CUAB piav tes piav taw

- qhov "toggleColor ()" ua haujlwm yooj yim hloov pauv thoob ntiaj teb lub npe "colorSelection" nrog ib qho ntawm qhov muaj txiaj ntsig 0, 1, 2

- kuj tseem nyob hauv lub ntsiab lus ua haujlwm lwm txoj haujlwm hu ua "animateLeds ();" hu ua. Txoj haujlwm no txheeb xyuas yog tias 100 milliseconds dhau, thiab yog li nws tig lub leds siv "rotateLeds ()" ua haujlwm thiab tom qab ntawd rov ua dua

- lub "rotateLeds ()" yuav "tig" lub leds rau pem hauv ntej lossis rov qab los ntawm kev siv lwm qhov hu ua "intermediateLedStates".

Kev sib hloov "cuam tshuam" yuav zoo li no:

# tom qab pib ua

{150, 100, 70, 50, 40, 30, 10, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} # tom qab tigLeds () hu ua {0, 150, 100, 70, 50, 40, 30, 10, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0; # tom qab tigLeds () raug hu dua {0, 0, 150, 100, 70, 50, 40, 30, 10, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; # thiab lwm yam

Txog qhov no thawj zaug tsim cov kab tshiab thiab luam tawm cov qub coj nruj ntawm txoj haujlwm tshiab (nce txoj haujlwm lossis txo nws). Tom qab ntawd nws rov sau dua "ledStates" array nrog "intermediateLedStates" yog li cov txheej txheem yuav txuas ntxiv tom qab lwm 100 milliseconds.

#suav nrog "SparkFun_APDS9960.h"

#suav nrog "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_), NEOPIXED_CONTROL_), SparkFun_APDS9960 apds = SparkFun_APDS9960 (); unsigned ntev lastLedChangeTime = 0; luv ledDirection = 0; luv 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; teeb tsa tsis muaj dab tsi () {Serial.begin (9600); Serial.println ("Pib qhov program"); strip.begin (); pinMode (APDS9960_INT, INPUT); attachInterrupt (0, cuam tshuam rau kev rov ua dua tshiab, poob); yog (apds.init () && apds.enableGestureSensor (tseeb)) {Serial.println ("APDS-9960 initialization complete"); } lwm {Serial.println ("Muaj qee yam tsis raug thaum lub sijhawm APDS-9960 pib!"); } lastLedChangeTime = millis (); Serial.println ("Init succesfully"); } void loop () {yog (isr_flag == 1) {detachInterrupt (0); handleGesture (); isr_flag = 0; attachInterrupt (0, cuam tshuam kev rov ua dua tshiab, poob); } animateLeds (); } tsis muaj dabtsis cuam tshuamRoutine () {isr_flag = 1; } / ** * Qhov no yuav lis tes taw los ntawm APDS9960 sensor * Kev nce thiab nqis tes yuav hu rau toggleColor muaj nuj nqi * Sab laug thiab Txoj cai tes taw yuav hloov cov coj ua yeeb yam * / tsis muaj dab tsi handleGesture () {yog (! Apds.isGestureAvailable ()) {rov qab; } hloov (apds.readGesture ()) {case DIR_UP: Serial.println ("UP"); toggleColor (); tawg; rooj plaub DIR_DOWN: Serial.println ("DOWN"); toggleColor (); tawg; rooj plaub DIR_LEFT: coj mus rau = 1; Serial.println ("LEFT"); tawg; rooj plaub DIR_RIGHT: ledDirection = -1; Serial.println ("RIGHT"); tawg; rooj plaub DIR_FAR: coj mus rau = 0; Serial.println ("FAR"); tawg; }} / ** * Hloov xim leds tam sim no * Txhua lub sijhawm ua haujlwm no hu ua yuav hloov lub xeev leds * / tsis muaj dab tsi toggleColor () {yog (colorSelection == 0) {colorSelection = 1; } lwm yog (colorSelection == 1) {colorSelection = 2; } lwm {colorSelection = 0; }} / ** * Cov tsiaj ntawv yuav ua haujlwm tom qab LED_SPEED_STEP_INTERVAL millis * Thawj qhov kev ua haujlwm RotLeds hu ua, tom qab ntawd cov leds xim tau teeb tsa siv kab txaij api * / tsis muaj dab tsi animateLeds () {yog (millis () - lastLedChangeTime <LED_SPEED_STEP_INTERVAL) {rov qab; } rotateLeds (); rau (int i = 0; kuv <NUM_LEDS; kuv ++) {strip.setPixelColor (i, getColor (ledStates )); strip.show (); } lastLedChangeTime = millis (); } /** * Siv qhov thib ob array "intermediateLedStates", leds intensities yog animated * Ua ntej qhov tseem ceeb ntawm "ledStates" tau theej ntawm "intermediateLedStates" zoo li ntawd * cia zaum "ledStates" array yog {100, 80, 60, 0, 0, 0} thiab coj kev coj ua yog 1 * tom qab ntawd lub luag haujlwm no hu ua "ledStates" array yog {0, 100, 80, 60, 0, 0} simulating kev hloov pauv */ tsis muaj dab tsi rotateLeds () {byte intermediateLedStates [NUM_LEDS]; rau (int kuv = 0; kuv <NUM_LEDS; kuv ++) {intermediateLedStates = 0; } rau (int i = 0; i <NUM_LEDS; i ++) {if (ledDirection == 1) {if (i == NUM_LEDS -1) {intermediateLedStates [0] = ledStates ; } lwm {intermediateLedStates [i + 1] = ledStates [kuv]; }} lwm {yog (kuv == 0) {intermediateLedStates [NUM_LEDS - 1] = ledStates [kuv]; } lwm {intermediateLedStates [i - 1] = ledStates [kuv]; }}} rau (int i = 0; i <NUM_LEDS; i ++) {ledStates = intermediateLedStates ; }} uint32_t getColor (int siv) {hloov (colorSelection) {rooj plaub 0: rov qab strip. Color (siv tau, 0, 0); rooj plaub 1: rov qab strip. Color (0, siv tau, 0); default: rov qab strip. Color (0, 0, siv tau); }}

Kuv vam tias koj nyiam qhov no, koj tuaj yeem siv ntu lus nug los nug kuv cov lus nug.

Pom zoo: