Mundarija:

IR-masofadan turib rangni aniqlash o'yini: 3 qadam
IR-masofadan turib rangni aniqlash o'yini: 3 qadam

Video: IR-masofadan turib rangni aniqlash o'yini: 3 qadam

Video: IR-masofadan turib rangni aniqlash o'yini: 3 qadam
Video: 45 SONIYADA ISMINGIZNI UNUTASIZ 2024, Noyabr
Anonim
IR-masofadan turib rangni aniqlash o'yini
IR-masofadan turib rangni aniqlash o'yini

Bu yo'riqnomada men sizga IR-masofadan boshqarish pulti yordamida RGB-ni qanday boshqarishni va undan qiziqarli o'yinni ko'rsataman!

1 -qadam: kerakli qismlar

Kerakli qismlar
Kerakli qismlar

Ushbu loyiha uchun sizga kerak bo'ladigan qismlar:

- arduino one- RGB Led- IQ sensori

(LEDlar soni o'yinga, hayot soniga bog'liq)- 2 ta yashil chiroq- 1 ta sariq- 1 ta to'q sariq- 3 ta qizil LED

2 -qadam: kod

#qo'shing

int redPin = 10;

int greenPin = 6; int bluePin = 5;

int levensPin1 = 8;

int levensPin2 = 9; int levensPin3 = 12;

int darajasiPin1 = 2;

int darajasiPin2 = 3; int darajasiPin3 = 4; int darajasiPin4 = 7;

int RECV_PIN = 11;

IRrecv irrecv (RECV_PIN); decode_results natijalari;

// kiritish ranglari

int kiritish = {-1, -1, -1}; int rangi = {-1, -1, -1};

int indexNumber = 0;

int indexColor = 0;

int waarde = 0;

int rood = 0;

int groen = 0; int blauw = 0;

// ranglar yaratish

int roodRandom = 0; int groenRandom = 0; int blauwRandom = 0;

// o'yin o'zgaruvchilari

int farqi = 200;

int darajasiNumber = 1;

int darajasi = {-1, -1, -1, -1};

int t = 0;

int darajasi1 = 255;

int darajasi2 = 0; int darajasi3 = 0; int darajasi4 = 0;

int life = 3;

int levens1 = 255; int levens2 = 255; int levens3 = 255;

int roodGok = 0;

int groenGok = 0; int blauwGok = 0;

bo'sh o'rnatish () {

Serial.begin (9600); irrecv.enableIRIn (); // qabul qiluvchining pinMode -ni ishga tushiring (redPin, OUTPUT); pinMode (greenPin, OUTPUT); pinMode (bluePin, OUTPUT);

pinMode (levensPin1, OUTPUT);

pinMod (levensPin2, OUTPUT); pinMode (levensPin3, OUTPUT);

}

void loop () {

agar (irrecv.decode (& natijalar)) // signallarni masofadan boshqaruvchi {Serial.println (results.value, DEC); // har bir bosilgan tugmachadan olingan signallarni ko'rsatish irrecv.resume (); // navbatdagi signal kalitini qabul qilish (natijalar qiymati) {// signallarni tugmalardagi raqamlarga aylantirish va tugmalarga harakatlarni tayinlash

ish 109562864:

addWaarde (1); tanaffus;

ish 109562824:

addWaarde (2); tanaffus;

ish 109562856:

addWaarde (3); tanaffus;

ish 109562872:

addWaarde (4); tanaffus;

ish 109562820:

addWaarde (5); tanaffus;

ish 109562852:

addWaarde (6); tanaffus;

ish 109562868:

addWaarde (7); tanaffus;

ish 109562828:

addWaarde (8); tanaffus;

ish 109562860:

addWaarde (9); tanaffus;

ish 109562876:

addWaarde (0); tanaffus;

ish 109562818: // oud ding enter/R CLK

handleInput (); tanaffus;

ish 109562816: // duding kuchi

handleColor (); tanaffus; ish 109562878: // oud ding bekor qilish/L CLK resetColor (); tanaffus;

ish 109562866: // lege knop midden

tasodifiy rang (); tanaffus;

ish 109562830: // pijltje naar rechts

displayEverything (); tanaffus; ish 109562838: // esc ishi 109562822: // AV manbasini resetEverything (); tanaffus; }

// rangni boshqarish uchun masofadan boshqarish pultidagi tugmachalarni belgilang: shu tariqa o'yinchilar ranglar qanday ko'rinishini va ularni qanday birlashtirishni ko'rishlari mumkin.

// o'yinni boshlashdan oldin. switch (results.value) {ish 109562840: rood = 255; tanaffus;

ish 109562844:

groen = 255; tanaffus;

ish 109562850:

blauw = 255; tanaffus;

ish 109562836:

rood = 0; groen = 0; blauw = 0; tanaffus; }

}

analogWrite (redPin, abs (255 - rood)); // boshqaruvchi RGB led: teskari, chunki meniki teskari simli.

analogWrite (greenPin, abs (255 - groen)); analogWrite (bluePin, abs (255 - blauw));

// jonli LEDlarni boshqarish

agar (yashaydi == 2) {levens1 = 0; } agar (yashaydi == 1) {levens2 = 0; } agar (yashaydi == 0) {levens3 = 0; } analogWrite (levensPin1, levens1); analogWrite (levensPin2, levens2); analogWrite (levensPin3, levens3);

// svetodiod darajasini nazorat qilish

agar (levelNumber == 1) {level1 = 255; } if (levelNumber == 2) {level1 = 255; daraja2 = 255; } if (levelNumber == 3) {level1 = 255; daraja2 = 255; daraja3 = 255; } if (levelNumber == 4) {level1 = 255; daraja2 = 255; daraja3 = 255; daraja4 = 255; } analogWrite (levelPin1, level1); analogWrite (levelPin2, level2); analogWrite (levelPin3, level3); analogWrite (levelPin4, level4);

}

void addWaarde (int value) {// bosilgan qiymatni ro'yxatga qo'shing

agar (indexNumber == 3) {

qaytish; }

if (kiritish [indexNumber] == -1) {

kirish [indexNumber] = qiymat; indexNumber ++; }}

void handleInput () {// ro'yxatdan "waarde" (qiymat) chiqaring

waarde = abs (kirish [0] * 100 + kirish [1] * 10 + kirish [2]); kirish [0] = -1; // signalni kiritishni tiklash [1] = -1; kirish [2] = -1;

indexNumber = 0;

Serial.print ("waarde opgeslagen:"); Serial.println (waarde); addColor (rangsiz); }

void addColor (int waarde) {// ro'yxatga 3 xonali 'waarde' qo'shing

agar (indexColor == 3) {

qaytish; }

agar (rang [indexColor] == -1) {

rang [indexColor] = bejirim; if (color [indexColor]> 255) {color [indexColor] = 255; } indexColor ++; }

}

void randomColor () {// yorug'likni tasodifiy rangga aylantirib, uni o'zgaruvchilarda saqlang

roodRandom = tasodifiy (0, 255);

groenRandom = tasodifiy (0, 255);

blauwRandom = tasodifiy (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 () {// ro'yxatdan "waarde" ni "Gok" (taxminiy) o'zgaruvchilariga aylantiring

roodGok = rang [0]; groenGok = rang [1]; blauwGok = rang [2]; // Gok o'zgaruvchilari haqiqiy tasodifiy rang bilan juda farq qiladimi -yo'qligini tekshirib ko'ring: agar shunday bo'lsa, taxminni uch soniya davomida ko'rsating, yashil yonib -o'chib turing va rangni o'zgartiring, ruxsat etilgan farqni kichikroq qilish uchun abd darajasini qo'shing // bo'lmasa, uch soniya taxmin qiling, qizil milt -milt o'chiring va hayotni olib tashlang // agar siz hayotdan mahrum bo'lsangiz, o'yin yana boshlanadi, agar (abs ((rood + groen + blauw) - (roodGok + groenGok + blauwGok)) <= farq) { analogWrite (redPin, abs (255 - roodGok)); analogWrite (greenPin, abs (255 - groenGok)); analogWrite (bluePin, abs (255 - blauwGok)); kechikish (3000); analogWrite (redPin, 255); analogWrite (greenPin, 0); analogWrite (bluePin, 255); kechikish (300); analogWrite (redPin, 255); analogWrite (greenPin, 255); analogWrite (bluePin, 255); kechikish (300); analogWrite (redPin, 255); analogWrite (greenPin, 0); analogWrite (bluePin, 255); kechikish (300); analogWrite (redPin, 255); analogWrite (greenPin, 255); analogWrite (bluePin, 255); kechikish (300);

Serial.print ("roodGok =");

Serial.println (roodGok); Serial.print ("groenGok ="); Serial.println (groenGok); Serial.print ("blauwGok ="); Serial.println (blauwGok); resetColor (); tasodifiy rang (); levelNumber ++; farq -= 50; } boshqa {analogWrite (redPin, abs (255 - roodGok)); analogWrite (greenPin, abs (255 - groenGok)); analogWrite (bluePin, abs (255 - blauwGok)); kechikish (3000); analogWrite (redPin, 0); analogWrite (greenPin, 255); analogWrite (bluePin, 255); kechikish (300); analogWrite (redPin, 255); analogWrite (greenPin, 255); analogWrite (bluePin, 255); kechikish (300); analogWrite (redPin, 0); analogWrite (greenPin, 255); analogWrite (bluePin, 255); kechikish (300); analogWrite (redPin, 255); analogWrite (greenPin, 255); analogWrite (bluePin, 255); kechikish (300); Serial.print ("roodGok ="); Serial.println (roodGok); Serial.print ("groenGok ="); Serial.println (groenGok); Serial.print ("blauwGok ="); Serial.println (blauwGok);

resetColor ();

hayot-; } agar (yashaydi == 0) {analogWrite (redPin, 0); analogWrite (greenPin, 255); analogWrite (bluePin, 255); kechikish (100); analogWrite (redPin, 255); analogWrite (greenPin, 255); analogWrite (bluePin, 255); kechikish (100); analogWrite (redPin, 0); analogWrite (greenPin, 255); analogWrite (bluePin, 255); kechikish (100); analogWrite (redPin, 255); analogWrite (greenPin, 255); analogWrite (bluePin, 255); kechikish (100); analogWrite (redPin, 0); analogWrite (greenPin, 255); analogWrite (bluePin, 255); kechikish (100); analogWrite (redPin, 255); analogWrite (greenPin, 255); analogWrite (bluePin, 255); kechikish (100); resetEverything (); }

}

void resetColor () {// qiymatlarni tiklash uchun, noto'g'ri bosish yoki har bir yangi taxmin uchun foydalaning (faqat aniqlik uchun)

rang [0] = -1; rang [1] = -1; rang [2] = -1;

indexNumber = 0;

indexColor = 0; }

void resetEverything () {// o'yinni qayta boshlang

rang [0] = -1; rang [1] = -1; rang [2] = -1;

indexNumber = 0;

indexColor = 0;

hayot = 3;

rood = 0; groen = 0; blauw = 0;

roodRandom = 0;

groenRandom = 0; blauwRandom = 0;

levens1 = 255;

levens2 = 255; levens3 = 255;

daraja1 = 255;

daraja2 = 0; daraja3 = 0; daraja4 = 0;

levelNumber = 1;

t = 0;

}

void displayEverything () {// buni ketma -ket monitorda muhim ma'lumotlarni ko'rsatish uchun foydalaning

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 -qadam: qurish

Qurmoq
Qurmoq
Qurmoq
Qurmoq
Qurmoq
Qurmoq
Qurmoq
Qurmoq

Siz, albatta, bu loyiha uchun yaxshi qurilishni xohlaysiz. bu qism talqin qilish uchun juda ochiq, lekin shuni yodda tutish kerakki: - old panelni ingichka qilib ushlab turing, shunda siz hali ham chiroqni ushlab turishingiz mumkin va IQ -qabul qilgich ishlaydi

- arduino va lehim taxtasi uchun bino ichida etarli joy saqlang

- orqada arduino kuchi uchun teshik borligiga ishonch hosil qiling

Men simlar uchun qo'pol sxemani qo'shdim.

Omad tilaymiz va dam oling!

Tavsiya: