Cov txheej txheem:

Txhim kho Arduino LED Mood Cube (Yooj Yim) (Video suav nrog): 4 Cov Kauj Ruam
Txhim kho Arduino LED Mood Cube (Yooj Yim) (Video suav nrog): 4 Cov Kauj Ruam

Video: Txhim kho Arduino LED Mood Cube (Yooj Yim) (Video suav nrog): 4 Cov Kauj Ruam

Video: Txhim kho Arduino LED Mood Cube (Yooj Yim) (Video suav nrog): 4 Cov Kauj Ruam
Video: Control RGB LED with LDR Sensor, Arduino analogRead, 2024, Hlis ntuj nqeg
Anonim
Txhim kho Arduino LED Mood Cube (Yooj Yim) (Video suav nrog)
Txhim kho Arduino LED Mood Cube (Yooj Yim) (Video suav nrog)

Tom qab pom ib qho kev xav me me ntawm lub voos xwmfab tsim los ntawm 'earl, Kuv txiav txim siab los ua qhov hloov kho ntawm LED Mood Cube. Kuv cov ntawv yuav nyuaj dua li qhov qub, vim nws yuav loj dua me ntsis qub, muaj ob xim ntxiv piv rau thawj lub voos xwmfab (ntxiv daj thiab dawb), muaj qhov tsis sib xws ntawm kev hloov xim, thiab lwm yam. yuav tsum yog txoj haujlwm zoo los xyaum ua txuas ntxiv ntawm kev siv LED teeb rau cov uas nkag siab lub tswv yim ntawm kev txuas cov teeb LED nrog cov xov hlau.

Kauj ruam 1: Cov ntaub ntawv

Khoom siv
Khoom siv
Khoom siv
Khoom siv

Nov yog qee cov ntaub ntawv koj yuav xav tau los ua Lub Cube Mood no:

  • Cov qhob cij
  • Arduino - (Kuv muaj Leonardo ntawm no)
  • Arduino fais fab mov / USB cable
  • Cov qhob cij
  • Jumper Wires (Ntau ntawm lawv, Kuv siv 29 xov hlau)
  • Liab LED x 2
  • Xiav LED x 2
  • Ntsuab LED x 2
  • Daj LED x 2
  • Dawb LED x 1
  • 9 tiv
  • Lub thawv loj txaus kom haum rau lub khob cij (Kuv siv lub thawv khau)
  • Riam Riam
  • Ntawv

Kauj ruam 2: Code

Qee qhov kev piav qhia rau cov cai tau muab ntawm no:

Cov qhab nia ntawm cov lej nkag mus rau qhov qub chaw ntawm kuv qhov haujlwm raws li tus kws kho mob ntawm txoj haujlwm tsim cov lej no. Kuv tsuas yog txhim kho qee qhov ntawm lawv los ntawm kev ua rau lawv nyuaj dua. Hauv qee cov lej, koj yuav pom // 改 thaum kawg. Qhov no txhais tau tias cov cai no tau kho los ntawm kuv, yog li nws txawv ntawm kuv li qub.

Kuv kuj muaj tus lej ntawm cov cai ntawm Arduino Creator.

/* Code rau hla -3 fading 3 LEDs, liab, ntsuab thiab xiav (RGB) Txhawm rau tsim kom ploj, koj yuav tsum ua ob yam: 1. Piav cov xim uas koj xav kom pom 2. Teev qhov kev txiav txim koj xav kom lawv ploj mus.

DESCRIBING IB TUG:

Cov xim tsuas yog ib qho ntawm peb feem pua, 0-100, tswj cov liab, ntsuab thiab xiav LEDs

Liab yog xim liab LED ntawm tag nrho, xiav thiab ntsuab tawm

int liab = {100, 0, 0} Dim dawb yog tag nrho peb LEDs ntawm 30% int dimWhite = {30, 30, 30} thiab lwm yam.

Qee qhov xim ib txwm tau muab hauv qab no, lossis ua koj tus kheej

LISTING TSO CAI:

Hauv qhov tseem ceeb ntawm txoj haujlwm, koj yuav tsum teev cov xaj uas koj xav kom cov xim tshwm tuaj, piv txwv. crossFade (liab); crossFade (ntsuab); crossFade (xiav);

Cov xim yuav tshwm nyob rau hauv qhov kev txiav txim ntawd, ploj mus

ib qho xim thiab mus rau tom ntej

Ib qho ntxiv, muaj 5 qhov kev xaiv uas koj tuaj yeem kho:

1. Thawj xim tau teeb tsa rau xim dub (yog li thawj xim ploj hauv), tab sis koj tuaj yeem teeb tsa cov xim pib ua lwm yam xim 2. Lub voj sab hauv ua haujlwm rau 1020 kev npaj; qhov 'tos' sib txawv teeb tsa lub sijhawm kwv yees ntawm kev hla ib leeg. Hauv kev xav, 'tos' ntawm 10 ms yuav tsum ua kom hla ntawm ~ 10 vib nas this. Hauv kev coj ua, lwm txoj haujlwm cov cai tau ua qhov qeeb no mus rau ~ 11 vib nas this ntawm kuv lub rooj tsavxwm. YMMV. 3. Yog tias 'rov ua dua' tau teeb tsa rau 0, qhov kev zov me nyuam yuav voj mus tas li. yog tias nws tau teeb tsa tus lej, nws yuav rov ua cov lej ntawd, tom qab ntawd nres ntawm cov xim kawg hauv ntu. (Teeb 'rov qab' rau 1, thiab ua kom cov xim kawg yog dub yog tias koj xav kom nws ploj mus thaum kawg.) 4. Muaj qhov xaiv 'tuav' sib txawv, uas ua rau qhov kev zov me nyuam rau "tuav" milliseconds thaum muaj xim ua tiav, tab sis ua ntej pib xim tom ntej. 5. Teem tus DEBUG chij rau 1 yog tias koj xav kom debugging tso tawm kom xa mus rau tus saib xyuas.

Sab hauv ntawm txoj haujlwm tsis nyuaj, tab sis lawv

yog fussy me ntsis - kev ua haujlwm sab hauv tau piav qhia hauv qab lub ntsiab lus.

Plaub Hlis 2007, Clay Shirky

*

/ Tso zis

int ylwPin = 5; // Daj LED, txuas nrog tus lej digital 5 // 改 int redPin = 6; // Liab LED, txuas nrog tus lej digital 6 // 改 int grnPin = 7; // Ntsuab LED, txuas nrog tus lej digital 7 // 改 int bluPin = 8; // Blue LED, txuas nrog tus lej digital 8 // 改 int whiPin = 9; // Dawb LED, txuas nrog tus pin 9 // 改 int ylwPin2 = 10; // Daj LED, txuas nrog tus lej digital 10 // 改 int redPin2 = 11; // Liab LED, txuas nrog tus lej digital 11 // 改 int grnPin2 = 12; // Ntsuab LED, txuas nrog tus lej digital 12 // 改 int bluPin2 = 13; // Xiav LED, txuas nrog tus lej digital 13 // 改

// Xim arrays

int dub [9] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // 改 int dawb [9] = {100, 100, 100, 100, 100, 100, 100, 100, 100, 100}; // red int liab [9] = {0, 0, 100, 0, 0, 0, 100, 0, 0}; // 改 int ntsuab [9] = {0, 100, 0, 0, 0, 0, 0, 100, 0}; // blue int xiav [9] = {0, 0, 0, 100, 0, 0, 0, 0, 0, 100}; // 改 int daj [9] = {100, 0, 0, 0, 0, 100, 0, 0, 0}; // 改 int ntshav [9] = {0, 50, 0, 50, 0, 0, 50, 0, 50}; // 改 int txiv kab ntxwv [9] = {50, 50, 0, 0, 0, 50, 50, 0, 0, 0}; // 改 int pink [9] = {0, 50, 0, 0, 50, 0, 0, 50, 0,};改 // lwm

// Teeb thawj xim

int redVal = dub [0]; int grnVal = dub [1]; int bluVal = dub [2]; int ylwVal = dub [3]; // 改 int whiVal = dub [4]; // 改

int tos = 15; // 10ms sab hauv crossFade ncua; nce rau qeeb qeeb // 改

int tuav = 1; // Yeem xaiv tuav thaum xim tiav, ua ntej crossFade tom ntej // 改 int DEBUG = 1; // DEBUG txee; yog teem rau 1, yuav sau qhov tseem ceeb rov qab ntawm cov lej int loopCount = 60; // Npaum li cas yuav tsum ceeb toom DEBUG? int rov = 0; // Muaj pes tsawg zaus peb yuav tsum tau ua ntej nres? (0 rau tsis nres) // 改 int j = 0; // Loop txee rau rov ua dua

// Pib hloov xim xim

int prevR = redVal; int prevG = grnVal; int prevB = bluVal; int prevY = ylwVal; int prevW = whiVal; // 改

// Teeb tsa cov LED tawm

teeb tsa tsis muaj dab tsi () {pinMode (redPin, OUTPUT); // teeb tsa tus pin raws li tso zis pinMode (grnPin, OUTPUT); pinMode (bluPin, OUTPUT); tus pinMode (ylwPin, OUTPUT); // 改 pinMode (whiPin, OUTPUT); // 改 pinMode (grnPin2, OUTPUT); // 改 pinMode (bluPin2, OUTPUT); // 改 pinMode (ylwPin2, OUTPUT); // 改 pinMode (redPin2, OUTPUT); // 改

yog (DEBUG) {// Yog tias peb xav pom qhov tseem ceeb rau kev debugging …

Serial.begin (9600); //… teeb tsa cov ntawv tso tawm}}

// Qhov haujlwm tseem ceeb: teev qhov kev txiav txim ntawm crossfades

void loop () {crossFade (liab)); crossFade (ntsuab); crossFade (xiav); crossFade (daj); crossFade (dawb); crossFade (liab dawb); crossFade (ntshav); crossFade (txiv kab ntxwv);

yog tias (rov ua dua) {// Puas yog peb rov ua dua qhov tsawg kawg ntawm lub sijhawm?

j += 1; yog (j> = rov hais dua) {// Puas yog peb tseem tsis tau muaj? tawm (j); // Yog tias muaj, nres. }}}

/* Cov kab hauv qab no yog MATH - Koj YUAV TSUM tsis xav hloov qhov no rau cov hauv paus

Txoj haujlwm ua haujlwm zoo li no:

Xav txog qhov crossfade uas txav LED liab los ntawm 0-10, ntsuab los ntawm 0-5, thiab xiav los ntawm 10 txog 7, hauv kaum kauj ruam. Peb xav xav suav 10 cov kauj ruam thiab nce lossis txo cov xim hauv qhov sib npaug ua ntu zuj zus. Xav txog a + qhia nce tus nqi los ntawm 1, thiab a - sib npaug txo nws. Peb 10 kauj ruam ploj yuav zoo li:

1 2 3 4 5 6 7 8 9 10

R + + + + + + + + + + + + G + + + + + + B - - -

Liab nce los ntawm 0 txog 10 hauv kaum kauj ruam, ntsuab los ntawm

0-5 hauv 5 kauj ruam, thiab xiav poob los ntawm 10 txog 7 hauv peb qib.

Hauv qhov kev zov me nyuam tiag, cov xim feem pua raug hloov pauv mus rau

0-255 qhov tseem ceeb, thiab muaj 1020 qib (255*4).

Txhawm rau txiav txim siab loj npaum li cas ib qib yuav tsum yog nruab nrab ntawm ib qho-lossis

down-tick ntawm ib qho ntawm qhov muaj txiaj ntsig LED, peb hu calculStep (), uas suav qhov tsis sib xws ntawm qhov pib thiab qhov kawg qhov tseem ceeb, thiab tom qab ntawd faib qhov sib txawv los ntawm 1020 txhawm rau txiav txim siab qhov loj ntawm theem ntawm kev hloov kho hauv tus nqi. */

int calculStep (int prevValue, int endValue) {

int kauj ruam = endValue - prevValue; // Qhov sib txawv tag nrho yog dab tsi? yog (kauj ruam) {// Yog tias nws tsis xoom, kauj ruam = 1020 / kauj ruam; // faib los ntawm 1020} rov qab nqis; }

/* Cov haujlwm tom ntej yog xamVal. Thaum lub voj nqi, kuv, nce mus txog qhov loj me uas tsim nyog rau ib qho ntawm cov xim, nws nce lossis txo tus nqi ntawm cov xim ntawd los ntawm 1. (R, G, thiab B yog suav txhua qhov cais.) */

int calculVal (int qib, int val, int kuv) {

yog ((kauj ruam) && i % kauj ruam == 0) {// Yog tias kauj ruam tsis yog xoom thiab nws lub sijhawm hloov pauv tus nqi, yog (kauj ruam> 0) {// nce tus nqi yog kauj ruam yog qhov zoo… val += 1; } lwm yog (kauj ruam 255) {val = 255; } lwm yog (val <0) {val = 0; } rov qab val; }

/* crossFade () hloov qhov feem pua xim rau a

0-255 ntau, tom qab ntawd loops 1020 zaug, tshuaj xyuas seb tus nqi yuav tsum tau hloov kho txhua lub sijhawm, tom qab ntawv sau cov xim muaj txiaj ntsig rau qhov pins raug. */

tsis muaj dab tsi crossFade (int xim ) {// 改

// Hloov mus rau 0-255 int R = (xim [0] * 255) / 100; int G = (xim [1] * 255) / 100; int B = (xim [2] * 255) / 100; int Y = (xim [3] * 255) / 100; // 改 int W = (xim [4] * 255) /100; // 改

int stepR = calculStep (prevR, R);

int stepG = calculStep (prevG, G); int stepB = calculStep (prevB, B); int stepY = calculStep (prevY, Y); // 改 int stepW = calculStep (prevW, W); // 改

rau (int i = 0; i <= 1020; i ++) {redVal = calculVal (stepR, redVal, i); grnVal = calculVal (stepG, grnVal, kuv); bluVal = calculVal (stepB, bluVal, kuv); ylwVal = calculVal (stepY, ylwVal, i); // 改 whiVal = calculVal (stepW, whiVal, kuv); // 改

analogWrite (redPin, redVal); // Sau qhov txiaj ntsig tam sim no rau LED pins

analogWrite (grnPin, grnVal); analogWrite (bluPin, bluVal); analogWrite (ylwPin, ylwVal); // 改 analogWrite (whiPin, whiVal); // 改 analogWrite (grnPin2, grnVal); // 改 analogWrite (bluPin2, bluVal); // 改 analogWrite (ylwPin2, ylwVal); // 改 analogWrite (redPin2, redVal); // 改

ncua (tos); // Ncua rau 'tos' milliseconds ua ntej rov ua lub voj

yog tias (DEBUG) {// Yog tias peb xav tau cov ntawv tso tawm, luam tawm ntawm

yog (i == 0 lossis i % loopCount == 0) {// pib, thiab txhua lub voj voos suav lub sijhawm Serial.print ("Loop/RGBYW: #"); Serial.print (kuv); Serial.print ("|"); Serial.print (redVal); Serial.print (" /"); Serial.print (grnVal); Serial.print (" /"); Serial.println (bluVal); Serial.print (" /"); Serial.println (ylwVal); // 改 Serial.print (" /"); // 改 Serial.println (whiVal); // 改 Serial.print (" /"); // 改} DEBUG += 1; }} // Hloov kho qhov txiaj ntsig tam sim no rau lub voj tom ntej prevR = redVal; prevG = grnVal; prevB = bluVal; prevY = ylwVal; // 改 prevW = whiVal; // 改 ncua (tuav); // Ncua rau xaiv 'tos' milliseconds ua ntej rov pib lub voj}

Kauj ruam 3: Teeb

Teeb
Teeb
Teeb
Teeb
Teeb
Teeb
  1. Tau daim ntawv qhia zaub mov.
  2. Demo ntawm txuas cov xov hluav taws xob kom lub teeb LED tuaj yeem ci:

    1. Muab cov LED tso rau ntawm daim ntawv tais. Muab qhov ntev kawg tso rau sab laug thiab qhov luv luv ntawm sab xis.
    2. Muab ib qho kawg ntawm cov hlua jumper tso rau ntawm ib qho chaw uas nyob rau tib kab nrog ntev kawg ntawm LED. Muab lwm qhov kawg tso rau ntu Digital PWM. Txoj cai tau teev tseg LEDs daj kom txuas nrog 10 thiab 5, liab rau 6 thiab 11, xiav xiav rau 8 thiab 13, ntsuab rau 7 thiab 12, thiab thaum kawg dawb LED rau 9.
    3. Tso ib kawg ntawm tus tiv thaiv ntawm tib kab nrog luv kawg ntawm LED. Muab lwm qhov kawg tso rau qhov chaw ze.
    4. Muab qhov kawg ntawm lwm lub xov hlau txuas nrog tib kab nrog qhov kawg ntawm tus tiv thaiv uas tsis nyob rau tib kab nrog luv kawg ntawm LED. Muab lwm qhov kawg ntawm xaim tso rau ntawm kab tsis zoo.
    5. Tso qhov kawg ntawm lwm lub jumper xaim ntawm kab tsis zoo thiab tso nws lwm qhov kawg ntawm GND.
  3. Rov Ua Kauj Ruam 2 rau 8 zaug raws li koj xav tau 9 LEDs kom ci
  4. Muab lub khob cij tso rau hauv lub thawv. Ob peb ceeb toom ntawm no:

    1. Kuv siv lub hwj chim. Tsim lub qhov me me rau koj cov xov hlau siv rab riam kom hla dhau lub thawv thiab txuas mus rau lub khob cij.
    2. Nco ntsoov lub thawv raws li ib sab uas qhib. Lub thawv khau twb muaj ib sab uas qhib. Yog tias lub thawv kaw tag, txiav ib sab ntawm lub thawv kom qhib qhov chaw.
    3. Npog sab tsis muaj dab tsi nrog ntawv. Qhov no yog rau LED teeb kom ci los ntawm daim ntawv.

Pom zoo: