2025 Avtor: John Day | [email protected]. Nazadnje spremenjeno: 2025-01-13 06:58
V tem navodilu vam bom pokazal, kako z daljinskim upravljalnikom IR upravljati LED diode RGB in iz tega narediti zabavno igro!
Korak: Potrebni deli
Deli, ki jih potrebujete za ta projekt, so:
- arduino one- RGB LED-IR-senzor
(število led je odvisno od igre, količine življenj ali)- 2 zeleni LED-1-rumeni LED-1-oranžni LED-3 rdeči LED-diodi
2. korak: Koda
#vključi
int redPin = 10;
int greenPin = 6; int bluePin = 5;
int levensPin1 = 8;
int levensPin2 = 9; int levensPin3 = 12;
int ravenPin1 = 2;
int ravenPin2 = 3; int ravenPin3 = 4; int ravenPin4 = 7;
int RECV_PIN = 11;
IRrecv unrecv (RECV_PIN); decode_results rezultati;
// vnos barv
int input = {-1, -1, -1}; int barva = {-1, -1, -1};
int indexNumber = 0;
int indexColor = 0;
int waarde = 0;
int rood = 0;
int groen = 0; int blauw = 0;
// ustvarjanje barv
int roodRandom = 0; int groenRandom = 0; int blauwRandom = 0;
// spremenljivke igre
int razlika = 200;
int levelNumber = 1;
int raven = {-1, -1, -1, -1};
int t = 0;
int raven1 = 255;
int raven2 = 0; int raven3 = 0; int raven4 = 0;
int življenj = 3;
int levens1 = 255; int levens2 = 255; int levens3 = 255;
int roodGok = 0;
int groenGok = 0; int blauwGok = 0;
void setup () {
Serial.begin (9600); unrecv.enableIRIn (); // zaženemo sprejemnik pinMode (redPin, OUTPUT); pinMode (greenPin, OUTPUT); pinMode (bluePin, OUTPUT);
pinMode (levensPin1, OUTPUT);
pinMode (levensPin2, OUTPUT); pinMode (levensPin3, OUTPUT);
}
void loop () {
if (unrecv.decode (& results)) // sprejema signale z oddaljenega {Serial.println (results.value, DEC); // prikaže signale, prejete od vsakega pritisnjenega gumba unrecv.resume (); // prejemamo naslednje signalno stikalo (results.value) {// spremenimo signale v številke na gumbih in dodelimo dejanja gumbom
zadeva 109562864:
addWaarde (1); prekiniti;
zadeva 109562824:
addWaarde (2); prekiniti;
zadeva 109562856:
addWaarde (3); prekiniti;
zadeva 109562872:
addWaarde (4); prekiniti;
zadeva 109562820:
addWaarde (5); prekiniti;
zadeva 109562852:
addWaarde (6); prekiniti;
zadeva 109562868:
addWaarde (7); prekiniti;
zadeva 109562828:
addWaarde (8); prekiniti;
zadeva 109562860:
addWaarde (9); prekiniti;
zadeva 109562876:
addWaarde (0); prekiniti;
primer 109562818: // oud ding enter/R CLK
handleInput (); prekiniti;
primer 109562816: // moč oud ding
handleColor (); prekiniti; primer 109562878: // preklic oud ding/L CLK resetColor (); prekiniti;
primer 109562866: // lege knop midden
randomColor (); prekiniti;
zadeva 109562830: // pijltje naar rechts
displayEverything (); prekiniti; primer 109562838: // esc primer 109562822: // ponastavitev vira AVEEverything (); prekiniti; }
// dodelimo gumbe na daljinskem upravljalniku za nadzor barve: na ta način lahko igralci vidijo, kako izgledajo barve in kako jih kombinirati, // pred začetkom igre. stikalo (results.value) {primer 109562840: rood = 255; prekiniti;
zadeva 109562844:
groen = 255; prekiniti;
zadeva 109562850:
blauw = 255; prekiniti;
zadeva 109562836:
rood = 0; groen = 0; blauw = 0; prekiniti; }
}
analogWrite (redPin, abs (255 - rood)); // krmiljenje LED diode RGB: vzvratno, ker je moja povezana v obratni smeri.
analogWrite (greenPin, abs (255 - groen)); analogWrite (bluePin, abs (255 - blauw));
// nadzor LED v živo
če (živi == 2) {levens1 = 0; } če (živi == 1) {levens2 = 0; } če (živi == 0) {levens3 = 0; } analogWrite (levensPin1, levens1); analogWrite (levensPin2, levens2); analogWrite (levensPin3, levens3);
// nadzor nivojev LED
if (raven številke == 1) {raven1 = 255; } if (levelNumber == 2) {level1 = 255; raven2 = 255; } if (levelNumber == 3) {level1 = 255; raven2 = 255; raven3 = 255; } if (levelNumber == 4) {level1 = 255; raven2 = 255; raven3 = 255; raven4 = 255; } analogWrite (levelPin1, level1); analogWrite (levelPin2, level2); analogWrite (levelPin3, level3); analogWrite (levelPin4, level4);
}
void addWaarde (int vrednost) {// doda pritisnjeno vrednost na seznam
if (indeksna številka == 3) {
vrnitev; }
if (vnos [indexNumber] == -1) {
input [indexNumber] = vrednost; indexNumber ++; }}
void handleInput () {// naredi 'waarde' (vrednost) s seznama
waarde = abs (vnos [0] * 100 + vhod [1] * 10 + vhod [2]); vhod [0] = -1; // ponastavi signalni vnos [1] = -1; vhod [2] = -1;
indexNumber = 0;
Serial.print ("waarde opgeslagen:"); Serial.println (waarde); addColor (waarde); }
void addColor (int waarde) {// dodamo 3-mestno "waarde" na seznam
if (indexColor == 3) {
vrnitev; }
if (barva [indexColor] == -1) {
barva [indexColor] = barva; če (barva [indexColor]> 255) {color [indexColor] = 255; } indexColor ++; }
}
void randomColor () {// naredi svetlobo naključno obarvano in jo shrani v spremenljivke
roodRandom = naključno (0, 255);
groenRandom = naključno (0, 255);
blauwRandom = naključno (0, 255);
rood = roodRandom;
groen = groenRandom; blauw = blauwRandom; Serial.print ("roodRandom ="); Serial.println (roodRandom); Serial.print ("groenRandom ="); Serial.println (groenRandom); Serial.print ("blauwRandom ="); Serial.println (blauwRandom);
}
void handleColor () {// spremenite 'waarde' s seznama v spremenljivke 'Gok' (ugibajte)
roodGok = barva [0]; groenGok = barva [1]; blauwGok = barva [2]; // preverite, ali se spremenljivke Gok preveč razlikujejo od prave naključne barve: če je tako, tri sekunde prikažite ugibanje, utripa zeleno in znova spremenite barvo, dodajte raven abd, da bo dovoljena razlika manjša // če ne, prikažite ugibajte tri sekunde, rdeče utripajte in odštejte življenje // če niste več v življenju, se igra znova začne, če (abs ((rood + groen + blauw) - (roodGok + groenGok + blauwGok)) <= diff) { analogWrite (redPin, abs (255 - roodGok)); analogWrite (greenPin, abs (255 - groenGok)); analogWrite (bluePin, abs (255 - blauwGok)); zamuda (3000); analogWrite (redPin, 255); analogWrite (greenPin, 0); analogWrite (bluePin, 255); zamuda (300); analogWrite (redPin, 255); analogWrite (greenPin, 255); analogWrite (bluePin, 255); zamuda (300); analogWrite (redPin, 255); analogWrite (greenPin, 0); analogWrite (bluePin, 255); zamuda (300); analogWrite (redPin, 255); analogWrite (greenPin, 255); analogWrite (bluePin, 255); zamuda (300);
Serial.print ("roodGok =");
Serial.println (roodGok); Serial.print ("groenGok ="); Serial.println (groenGok); Serial.print ("blauwGok ="); Serial.println (blauwGok); resetColor (); randomColor (); levelNumber ++; razlika -= 50; } else {analogWrite (redPin, abs (255 - roodGok)); analogWrite (greenPin, abs (255 - groenGok)); analogWrite (bluePin, abs (255 - blauwGok)); zamuda (3000); analogWrite (redPin, 0); analogWrite (greenPin, 255); analogWrite (bluePin, 255); zamuda (300); analogWrite (redPin, 255); analogWrite (greenPin, 255); analogWrite (bluePin, 255); zamuda (300); analogWrite (redPin, 0); analogWrite (greenPin, 255); analogWrite (bluePin, 255); zamuda (300); analogWrite (redPin, 255); analogWrite (greenPin, 255); analogWrite (bluePin, 255); zamuda (300); Serial.print ("roodGok ="); Serial.println (roodGok); Serial.print ("groenGok ="); Serial.println (groenGok); Serial.print ("blauwGok ="); Serial.println (blauwGok);
resetColor ();
življenja--; } if (živi == 0) {analogWrite (redPin, 0); analogWrite (greenPin, 255); analogWrite (bluePin, 255); zamuda (100); analogWrite (redPin, 255); analogWrite (greenPin, 255); analogWrite (bluePin, 255); zamuda (100); analogWrite (redPin, 0); analogWrite (greenPin, 255); analogWrite (bluePin, 255); zamuda (100); analogWrite (redPin, 255); analogWrite (greenPin, 255); analogWrite (bluePin, 255); zamuda (100); analogWrite (redPin, 0); analogWrite (greenPin, 255); analogWrite (bluePin, 255); zamuda (100); analogWrite (redPin, 255); analogWrite (greenPin, 255); analogWrite (bluePin, 255); zamuda (100); resetEverything (); }
}
void resetColor () {// uporabite za ponastavitev vrednosti, za napačen pritisk ali vsako novo ugibanje (samo za gotovost)
barva [0] = -1; barva [1] = -1; barva [2] = -1;
indexNumber = 0;
indexColor = 0; }
void resetEverything () {// znova zaženite igro
barva [0] = -1; barva [1] = -1; barva [2] = -1;
indexNumber = 0;
indexColor = 0;
življenja = 3;
rood = 0; groen = 0; blauw = 0;
roodRandom = 0;
groenRandom = 0; blauwRandom = 0;
levens1 = 255;
levens2 = 255; levens3 = 255;
raven1 = 255;
raven2 = 0; raven3 = 0; raven4 = 0;
levelNumber = 1;
t = 0;
}
void displayEverything () {// to uporabite za prikaz pomembnih informacij na serijskem monitorju
Serial.print ("roodGok ="); Serial.println (roodGok); Serial.print ("groenGok ="); Serial.println (groenGok); Serial.print ("blauwGok ="); Serial.println (blauwGok); Serial.print ("roodRandom ="); Serial.println (roodRandom); Serial.print ("groenRandom ="); Serial.println (groenRandom); Serial.print ("blauwRandom ="); Serial.println (blauwRandom);
Serial.print ("rood =");
Serial.println (rood); Serial.print ("groen ="); Serial.println (groen); Serial.print ("blauw ="); Serial.println (blauw);
Serial.print ("waarde opgeslagen:");
Serial.println (waarde); }
3. korak: Zgradite
Za ta projekt očitno želite lepo izdelavo. ta del je precej odprt za razlago, vendar je pri tem treba upoštevati nekaj stvari: - imejte sprednjo ploščo tanko, da lahko še vedno držite svetleče diode in IR sprejemnik še vedno deluje
- v gradnji imejte dovolj prostora za arduino in spajkalno ploščo
- poskrbite, da je zadaj luknja za moč arduina
Dodal sem grobo shemo ožičenja.
Vso srečo in lepo se imejte!