fork download
  1.  
  2. #include <Wire.h>
  3. #include <EEPROM.h>
  4. #include <LiquidCrystal_I2C.h>
  5. #include <IRremote.h>
  6. #include <PT2258.h>
  7. #define PT2258_address 0b1000100
  8. #define RotaryB 11
  9. #define RotaryA 12
  10. #define RotarySW 13
  11. #define SW1 A1
  12. #define SW2 A2
  13. #define SW3 A3
  14. #define RECV_PIN 8
  15. #define powerOut 10
  16. // IR HEX code
  17. #define ir_power 0x807F827D // IR Power ON/OFF
  18. #define ir_mute 0x807F42BD // IR Mute
  19. #define ir_vol_up 0x807F906F // IR Vol++
  20. #define ir_vol_down 0x807FA05F // IR Vol--
  21. #define ir_ch_up 0x807F40BF // IR CH++
  22. #define ir_ch_down 0x807FC03F // IR CH--
  23. LiquidCrystal_I2C lcd(0x27, 16, 2);
  24.  
  25. byte custom_num[8][8] = {
  26. { B00111, B01111, B11111, B11111, B11111, B11111, B11111, B11111 },
  27. { B11111, B11111, B11111, B00000, B00000, B00000, B00000, B00000 },
  28. { B11100, B11110, B11111, B11111, B11111, B11111, B11111, B11111 },
  29. { B11111, B11111, B11111, B11111, B11111, B11111, B01111, B00111 },
  30. { B00000, B00000, B00000, B00000, B00000, B11111, B11111, B11111 },
  31. { B11111, B11111, B11111, B11111, B11111, B11111, B11110, B11100 },
  32. { B11111, B11111, B11111, B00000, B00000, B00000, B11111, B11111 },
  33. { B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111 }
  34. };
  35. const int digit_width = 3;
  36. const char custom_num_top[10][digit_width] = { 0, 1, 2, 1, 2, 32, 6, 6, 2, 6, 6, 2, 3, 4, 7, 7, 6, 6, 0, 6, 6, 1, 1, 2, 0, 6, 2, 0, 6, 2 };
  37. const char custom_num_bot[10][digit_width] = { 3, 4, 5, 4, 7, 4, 7, 4, 4, 4, 4, 5, 32, 32, 7, 4, 4, 5, 3, 4, 5, 32, 32, 7, 3, 4, 5, 4, 4, 5 };
  38. unsigned long returnTime;
  39. unsigned long lastRotaryTime = 0;
  40. unsigned long lastButtonTime = 0;
  41. unsigned long muteLcdTime = 0;
  42. int rotaryDelayTime = 200;
  43. int buttonDelayTime = 100;
  44. int muteLcdDelay = 0;
  45. int muteLcdOnTime = 500;
  46. int muteLcdOffTime = 500;
  47.  
  48. int masVolLimitMax = 69;
  49. int masVolLimitMin = 19;
  50. int chVolLimitMax = 10;
  51. int chVolLimitMin = -10;
  52. int lastEncoded = 0;
  53.  
  54. int vol_menu, mas_vol, ch1_vol, ch2_vol, ch3_vol, ch4_vol, ch5_vol, ch6_vol, mute, ch1, ch2, ch3, ch4, ch5, ch6;
  55. int return_d, power, menu_active;
  56. set_mute;
  57. IRrecv irrecv(RECV_PIN);
  58. decode_results results;
  59.  
  60. void setup() {
  61. Wire.begin();
  62. lcd.begin(16, 2);
  63. irrecv.enableIRIn();
  64.  
  65. pinMode(RotaryA, INPUT);
  66. pinMode(RotaryB, INPUT);
  67. pinMode(RotarySW, INPUT_PULLUP);
  68. pinMode(SW1, INPUT);
  69. pinMode(SW2, INPUT);
  70. pinMode(SW3, INPUT);
  71. pinMode(powerOut, OUTPUT);
  72.  
  73. digitalWrite(RotaryA, HIGH);
  74. digitalWrite(RotaryB, HIGH);
  75.  
  76. power = 0;
  77. eepromRead();
  78. startUp();
  79. powerUp();
  80. }
  81. void loop() {
  82. lcdDisplay();
  83. eepromUpdate();
  84. IRControl();
  85. returnDelay();
  86. //Power -------------------------------------------------//
  87. if (analogRead(SW1) > 900) {
  88. if (millis() - lastButtonTime > buttonDelayTime) {
  89. power++;
  90. if (power > 1) power = 0;
  91. powerUp();
  92. }
  93. lastButtonTime = millis();
  94. }
  95. if (power == 1) {
  96. updateEncoder();
  97. //Mute -------------------------------------------------//
  98. if (analogRead(SW2) > 900) {
  99. if (millis() - lastButtonTime > buttonDelayTime) {
  100. soundMute();
  101. }
  102. lastButtonTime = millis();
  103. }
  104. if (digitalRead(RotarySW) == LOW) {
  105. if (millis() - lastButtonTime > buttonDelayTime) {
  106. volumeMenuUp();
  107. }
  108. lastButtonTime = millis();
  109. }
  110. }
  111. }
  112.  
  113. void updateEncoder() {
  114. int CLK = digitalRead(RotaryA);
  115. int DT = digitalRead(RotaryB);
  116.  
  117. int encoded = (CLK << 1) | DT;
  118. int addData = (lastEncoded << 2) | encoded;
  119.  
  120. if (addData == 0b1101 || addData == 0b0100 || addData == 0b0010 || addData == 0b1011) {
  121. if ((millis() - lastRotaryTime) > rotaryDelayTime) {
  122. volumeUp();
  123. lastRotaryTime = millis();
  124. }
  125. }
  126. if (addData == 0b1110 || addData == 0b0111 || addData == 0b0001 || addData == 0b1000) {
  127. if ((millis() - lastRotaryTime) > rotaryDelayTime) {
  128. volumeDown();
  129. lastRotaryTime = millis();
  130. }
  131. }
  132. lastEncoded = encoded;
  133. }
  134.  
  135. void volumeMenuUp() {
  136. vol_menu++;
  137. if (vol_menu > 6) vol_menu = 0;
  138. btnPress();
  139. }
  140.  
  141. void volumeMenuDown() {
  142. vol_menu--;
  143. if (vol_menu < 0) vol_menu = 6;
  144. btnPress();
  145. }
  146.  
  147. void volumeUp() {
  148. if (vol_menu == 0) {
  149. mas_vol++;
  150. masVol();
  151. }
  152. if (vol_menu == 1) {
  153. ch1_vol++;
  154. ch1Vol(1);
  155. }
  156. if (vol_menu == 2) {
  157. ch2_vol++;
  158. ch2Vol(1);
  159. }
  160. if (vol_menu == 3) {
  161. ch3_vol++;
  162. ch3Vol(1);
  163. }
  164. if (vol_menu == 4) {
  165. ch4_vol++;
  166. ch4Vol(1);
  167. }
  168. if (vol_menu == 5) {
  169. ch5_vol++;
  170. ch5Vol(1);
  171. }
  172. if (vol_menu == 6) {
  173. ch6_vol++;
  174. ch6Vol(1);
  175. }
  176. btnPress();
  177. }
  178.  
  179. void volumeDown() {
  180. if (vol_menu == 0) {
  181. mas_vol--;
  182. masVol();
  183. }
  184. if (vol_menu == 1) {
  185. ch1_vol--;
  186. ch1Vol(1);
  187. }
  188. if (vol_menu == 2) {
  189. ch2_vol--;
  190. ch2Vol(1);
  191. }
  192. if (vol_menu == 3) {
  193. ch3_vol--;
  194. ch3Vol(1);
  195. }
  196. if (vol_menu == 4) {
  197. ch4_vol--;
  198. ch4Vol(1);
  199. }
  200. if (vol_menu == 5) {
  201. ch5_vol--;
  202. ch5Vol(1);
  203. }
  204. if (vol_menu == 6) {
  205. ch6_vol--;
  206. ch6Vol(1);
  207. }
  208. btnPress();
  209. }
  210.  
  211. void soundMute() {
  212. mute++;
  213. if (mute > 1) mute = 0;
  214. set_mute(mute);
  215. }
  216.  
  217. void masVol() {
  218. if (mas_vol > masVolLimitMax) mas_vol = masVolLimitMax;
  219. if (mas_vol < masVolLimitMin) mas_vol = masVolLimitMin;
  220.  
  221. if (mute == 0) {
  222. if (mas_vol == masVolLimitMin) set_mute(1);
  223. else set_mute(0);
  224. }
  225.  
  226. for (int i = 1; i < 5; i++) {
  227. ch1Vol(i);
  228. ch2Vol(i);
  229. ch3Vol(i);
  230. ch4Vol(i);
  231. ch5Vol(i);
  232. ch6Vol(i);
  233. }
  234. }
  235.  
  236. void ch1Vol(int adr) {
  237. if (ch1_vol > chVolLimitMax) ch1_vol = chVolLimitMax;
  238. if (ch1_vol < chVolLimitMin) ch1_vol = chVolLimitMin;
  239. ch1 = mas_vol + ch1_vol;
  240. set_address(adr);
  241. set_ch1(ch1);
  242. }
  243.  
  244. void ch2Vol(int adr) {
  245. if (ch2_vol > chVolLimitMax) ch2_vol = chVolLimitMax;
  246. if (ch2_vol < chVolLimitMin) ch2_vol = chVolLimitMin;
  247. ch2 = mas_vol + ch2_vol;
  248. set_address(adr);
  249. set_ch2(ch2);
  250. }
  251.  
  252. void ch3Vol(int adr) {
  253. if (ch3_vol > chVolLimitMax) ch3_vol = chVolLimitMax;
  254. if (ch3_vol < chVolLimitMin) ch3_vol = chVolLimitMin;
  255. ch3 = mas_vol + ch3_vol;
  256. set_address(adr);
  257. set_ch3(ch3);
  258. }
  259.  
  260. void ch4Vol(int adr) {
  261. if (ch4_vol > chVolLimitMax) ch4_vol = chVolLimitMax;
  262. if (ch4_vol < chVolLimitMin) ch4_vol = chVolLimitMin;
  263. ch4 = mas_vol + ch4_vol;
  264. set_address(adr);
  265. set_ch4(ch4);
  266. }
  267.  
  268. void ch5Vol(int adr) {
  269. if (ch5_vol > chVolLimitMax) ch5_vol = chVolLimitMax;
  270. if (ch5_vol < chVolLimitMin) ch5_vol = chVolLimitMin;
  271. ch5 = mas_vol + ch5_vol;
  272. set_address(adr);
  273. set_ch5(ch5);
  274. }
  275.  
  276. void ch6Vol(int adr) {
  277. if (ch6_vol > chVolLimitMax) ch6_vol = chVolLimitMax;
  278. if (ch6_vol < chVolLimitMin) ch6_vol = chVolLimitMin;
  279. ch6 = mas_vol + ch6_vol;
  280. set_address(adr);
  281. set_ch6(ch6);
  282. }
  283.  
  284. void powerUp() {
  285. if (power == 1) {
  286. lcd.clear();
  287. delay(500);
  288. lcd.setCursor(0, 1);
  289. lcd.print(" LOADING... ");
  290. delay(1000);
  291. lcd.clear();
  292. if (mas_vol == masVolLimitMin) {
  293. set_mute(1);
  294. } else {
  295. mute = 0;
  296. set_mute(mute);
  297. }
  298. vol_menu = 0;
  299. menu_active = 0;
  300. digitalWrite(powerOut, HIGH);
  301. delay(300);
  302.  
  303. } else {
  304.  
  305. set_mute(1);
  306. delay(100);
  307. digitalWrite(powerOut, LOW);
  308. menu_active = 100;
  309. }
  310. }
  311.  
  312. void startUp() {
  313. delay(500);
  314. pt2258();
  315. lcd.setCursor(0, 0);
  316. lcd.print(" DaacWave ");
  317. delay(300);
  318. lcd.setCursor(0, 1);
  319. lcd.print(" AUDIOS ");
  320. delay(1500);
  321. lcd.clear();
  322. delay(300);
  323. lcd.setCursor(0, 0);
  324. lcd.print(" 6CH-DVC ");
  325. lcd.setCursor(0, 1);
  326. lcd.print(" LOADING... ");
  327. delay(1200);
  328. lcd.clear();
  329. delay(200);
  330. for (int i = 1; i < 5; i++) {
  331. ch1Vol(i);
  332. ch2Vol(i);
  333. ch3Vol(i);
  334. ch4Vol(i);
  335. ch5Vol(i);
  336. ch6Vol(i);
  337. }
  338. }
  339.  
  340. //IR control --------------------------------------------------------------------------------//
  341. void IRControl() {
  342. if (irrecv.decode(&results)) {
  343. switch (results.value) {
  344. //power -------------------------------------------------//
  345. case ir_power:
  346. power++;
  347. if (power > 1) power = 0;
  348. powerUp();
  349. break;
  350. }
  351. if (power == 1) {
  352. switch (results.value) {
  353. //mute -------------------------------------------------//
  354. case ir_mute:
  355. soundMute();
  356. break;
  357. if (mute == 0) {
  358. //VOL -------------------------------------------------//
  359. case ir_vol_up:
  360. volumeUp();
  361. break;
  362.  
  363. case ir_vol_down:
  364. volumeDown();
  365. break;
  366. }
  367.  
  368. //CH1 -------------------------------------------------//
  369. case ir_ch_up:
  370. volumeMenuUp();
  371. break;
  372.  
  373. case ir_ch_down:
  374. volumeMenuDown();
  375. break;
  376. }
  377. }
  378. irrecv.resume();
  379. }
  380. }
  381.  
  382. void lcdDisplay() {
  383. int a, b, c;
  384. switch (menu_active) {
  385. case 0:
  386. //vol ----------------------------------------------//
  387. switch (vol_menu) {
  388. case 0:
  389. lcd.setCursor(0, 1);
  390. // (" ");
  391. lcd.print("MAS-VOL");
  392. lcd.setCursor(0, 0);
  393. c = mas_vol - masVolLimitMin;
  394. break;
  395.  
  396. case 1:
  397. lcd.setCursor(0, 1);
  398. // (" ");
  399. lcd.print("CH1-VOL");
  400. c = ch1_vol;
  401. break;
  402.  
  403. case 2:
  404. lcd.setCursor(0, 1);
  405. // (" ");
  406. lcd.print("CH2-VOL");
  407. c = ch2_vol;
  408. break;
  409.  
  410. case 3:
  411. lcd.setCursor(0, 1);
  412. // (" ");
  413. lcd.print("CH3-VOL");
  414. c = ch3_vol;
  415. break;
  416.  
  417. case 4:
  418. lcd.setCursor(0, 1);
  419. // (" ");
  420. lcd.print("CH4-VOL");
  421. c = ch4_vol;
  422. break;
  423.  
  424. case 5:
  425. lcd.setCursor(0, 1);
  426. // (" ");
  427. lcd.print("CH5-VOL");
  428. c = ch5_vol;
  429. break;
  430.  
  431. case 6:
  432. lcd.setCursor(0, 1);
  433. // (" ");
  434. lcd.print("CH6-VOL");
  435. c = ch6_vol;
  436. break;
  437. }
  438. break;
  439.  
  440. case 100:
  441. lcd.setCursor(0, 0);
  442. lcd.print(" ");
  443. lcd.setCursor(0, 1);
  444. if (muteLcdDelay == 0) {
  445. if ((millis() - muteLcdTime) >= muteLcdOnTime) {
  446. lcd.print(" ");
  447. muteLcdDelay = 1;
  448. muteLcdTime = millis();
  449. }
  450. } else {
  451. if ((millis() - muteLcdTime) >= muteLcdOffTime) {
  452. lcd.print(" STANDBY ");
  453. muteLcdDelay = 0;
  454. muteLcdTime = millis();
  455. }
  456. }
  457. break;
  458. }
  459. if (menu_active == 0) {
  460. if (mute == 1) {
  461. lcd.setCursor(0, 0);
  462. if (muteLcdDelay == 0) {
  463. if ((millis() - muteLcdTime) >= muteLcdOnTime) {
  464. lcd.print(" ");
  465. muteLcdDelay = 1;
  466. muteLcdTime = millis();
  467. }
  468. } else {
  469. if ((millis() - muteLcdTime) >= muteLcdOffTime) {
  470. lcd.print("MUTE ");
  471. muteLcdDelay = 0;
  472. muteLcdTime = millis();
  473. }
  474. }
  475.  
  476. } else {
  477. lcd.setCursor(0, 0);
  478. lcd.print("6CH-DVC");
  479. }
  480.  
  481. for (int i = 0; i < 8; i++)
  482. lcd.createChar(i, custom_num[i]);
  483.  
  484. int y;
  485. if (c < 0) {
  486. lcd.setCursor(8, 1);
  487. lcd.print("-");
  488. y = chVolLimitMax - (c + chVolLimitMax);
  489. } else if (c == chVolLimitMin) {
  490. lcd.setCursor(8, 1);
  491. lcd.print("-");
  492. y = chVolLimitMax;
  493. } else {
  494. lcd.setCursor(8, 1);
  495. lcd.print(" ");
  496. y = c;
  497. }
  498. a = y / 10;
  499. b = y - a * 10;
  500.  
  501. lcd.setCursor(9, 0);
  502. for (int i = 0; i < digit_width; i++)
  503. lcd.print(custom_num_top[a][i]);
  504.  
  505. lcd.setCursor(9, 1);
  506. for (int i = 0; i < digit_width; i++)
  507. lcd.print(custom_num_bot[a][i]);
  508.  
  509. lcd.setCursor(13, 0);
  510. for (int i = 0; i < digit_width; i++)
  511. lcd.print(custom_num_top[b][i]);
  512.  
  513. lcd.setCursor(13, 1);
  514. for (int i = 0; i < digit_width; i++)
  515. lcd.print(custom_num_bot[b][i]);
  516. }
  517. }
  518.  
  519. //EEPROM -----------------------------------------------------//
  520. void eepromUpdate() {
  521. EEPROM.update(0, mas_vol);
  522. EEPROM.update(1, ch1_vol + 10);
  523. EEPROM.update(2, ch2_vol + 10);
  524. EEPROM.update(3, ch3_vol + 10);
  525. EEPROM.update(4, ch4_vol + 10);
  526. EEPROM.update(5, ch5_vol + 10);
  527. EEPROM.update(6, ch6_vol + 10);
  528. }
  529.  
  530. void eepromRead() {
  531. mas_vol = EEPROM.read(0);
  532. ch1_vol = EEPROM.read(1) - 10;
  533. ch2_vol = EEPROM.read(2) - 10;
  534. ch3_vol = EEPROM.read(3) - 10;
  535. ch4_vol = EEPROM.read(4) - 10;
  536. ch5_vol = EEPROM.read(5) - 10;
  537. ch6_vol = EEPROM.read(6) - 10;
  538. }
  539.  
  540. void btnPress() {
  541. returnTime = millis();
  542. return_d = 1;
  543. }
  544. void returnDelay() {
  545. if (millis() - returnTime > 5000 && return_d == 1 && vol_menu != 0) {
  546. vol_menu = 0;
  547. return_d = 0;
  548. lcd.clear();
  549. }
  550. }
  551. if (power == 1) {
  552. updateEncoder();
  553. //Mute -------------------------------------------------//
  554. if (analogRead(SW2) > 900) {
  555. if (millis() - lastButtonTime > buttonDelayTime) {
  556. soundMute();
  557. }
  558. lastButtonTime = millis();
  559. }
  560. if (digitalRead(RotarySW) == LOW) {
  561. if (millis() - lastButtonTime > buttonDelayTime) {
  562. volumeMenuUp();
  563. }
  564. lastButtonTime = millis();
  565. }
  566. }
  567. }
  568.  
  569. void updateEncoder() {
  570. int CLK = digitalRead(RotaryA);
  571. int DT = digitalRead(RotaryB);
  572.  
  573. int encoded = (CLK << 1) | DT;
  574. int addData = (lastEncoded << 2) | encoded;
  575.  
  576. if (addData == 0b1101 || addData == 0b0100 || addData == 0b0010 || addData == 0b1011) {
  577. if ((millis() - lastRotaryTime) > rotaryDelayTime) {
  578. volumeUp();
  579. lastRotaryTime = millis();
  580. }
  581. }
  582. if (addData == 0b1110 || addData == 0b0111 || addData == 0b0001 || addData == 0b1000) {
  583. if ((millis() - lastRotaryTime) > rotaryDelayTime) {
  584. volumeDown();
  585. lastRotaryTime = millis();
  586. }
  587. }
  588. lastEncoded = encoded;
  589. }
  590.  
  591. void volumeMenuUp() {
  592. vol_menu++;
  593. if (vol_menu > 6) vol_menu = 0;
  594. btnPress();
  595. }
  596.  
  597. void volumeMenuDown() {
  598. vol_menu--;
  599. if (vol_menu < 0) vol_menu = 6;
  600. btnPress();
  601. }
  602.  
  603. void volumeUp() {
  604. if (vol_menu == 0) {
  605. mas_vol++;
  606. masVol();
  607. }
  608. if (vol_menu == 1) {
  609. ch1_vol++;
  610. ch1Vol(1);
  611. }
  612. if (vol_menu == 2) {
  613. ch2_vol++;
  614. ch2Vol(1);
  615. }
  616. if (vol_menu == 3) {
  617. ch3_vol++;
  618. ch3Vol(1);
  619. }
  620. if (vol_menu == 4) {
  621. ch4_vol++;
  622. ch4Vol(1);
  623. }
  624. if (vol_menu == 5) {
  625. ch5_vol++;
  626. ch5Vol(1);
  627. }
  628. if (vol_menu == 6) {
  629. ch6_vol++;
  630. ch6Vol(1);
  631. }
  632. btnPress();
  633. }
  634.  
  635. void volumeDown() {
  636. if (vol_menu == 0) {
  637. mas_vol--;
  638. masVol();
  639. }
  640. if (vol_menu == 1) {
  641. ch1_vol--;
  642. ch1Vol(1);
  643. }
  644. if (vol_menu == 2) {
  645. ch2_vol--;
  646. ch2Vol(1);
  647. }
  648. if (vol_menu == 3) {
  649. ch3_vol--;
  650. ch3Vol(1);
  651. }
  652. if (vol_menu == 4) {
  653. ch4_vol--;
  654. ch4Vol(1);
  655. }
  656. if (vol_menu == 5) {
  657. ch5_vol--;
  658. ch5Vol(1);
  659. }
  660. if (vol_menu == 6) {
  661. ch6_vol--;
  662. ch6Vol(1);
  663. }
  664. btnPress();
  665. }
  666.  
  667. void soundMute() {
  668. mute++;
  669. if (mute > 1) mute = 0;
  670. set_mute(mute);
  671. }
  672.  
  673. void masVol() {
  674. if (mas_vol > masVolLimitMax) mas_vol = masVolLimitMax;
  675. if (mas_vol < masVolLimitMin) mas_vol = masVolLimitMin;
  676.  
  677. if (mute == 0) {
  678. if (mas_vol == masVolLimitMin) set_mute(1);
  679. else set_mute(0);
  680. }
  681.  
  682. for (int i = 1; i < 5; i++) {
  683. ch1Vol(i);
  684. ch2Vol(i);
  685. ch3Vol(i);
  686. ch4Vol(i);
  687. ch5Vol(i);
  688. ch6Vol(i);
  689. }
  690. }
  691.  
  692. void ch1Vol(int adr) {
  693. if (ch1_vol > chVolLimitMax) ch1_vol = chVolLimitMax;
  694. if (ch1_vol < chVolLimitMin) ch1_vol = chVolLimitMin;
  695. ch1 = mas_vol + ch1_vol;
  696. set_address(adr);
  697. set_ch1(ch1);
  698. }
  699.  
  700. void ch2Vol(int adr) {
  701. if (ch2_vol > chVolLimitMax) ch2_vol = chVolLimitMax;
  702. if (ch2_vol < chVolLimitMin) ch2_vol = chVolLimitMin;
  703. ch2 = mas_vol + ch2_vol;
  704. set_address(adr);
  705. set_ch2(ch2);
  706. }
  707.  
  708. void ch3Vol(int adr) {
  709. if (ch3_vol > chVolLimitMax) ch3_vol = chVolLimitMax;
  710. if (ch3_vol < chVolLimitMin) ch3_vol = chVolLimitMin;
  711. ch3 = mas_vol + ch3_vol;
  712. set_address(adr);
  713. set_ch3(ch3);
  714. }
  715.  
  716. void ch4Vol(int adr) {
  717. if (ch4_vol > chVolLimitMax) ch4_vol = chVolLimitMax;
  718. if (ch4_vol < chVolLimitMin) ch4_vol = chVolLimitMin;
  719. ch4 = mas_vol + ch4_vol;
  720. set_address(adr);
  721. set_ch4(ch4);
  722. }
  723.  
  724. void ch5Vol(int adr) {
  725. if (ch5_vol > chVolLimitMax) ch5_vol = chVolLimitMax;
  726. if (ch5_vol < chVolLimitMin) ch5_vol = chVolLimitMin;
  727. ch5 = mas_vol + ch5_vol;
  728. set_address(adr);
  729. set_ch5(ch5);
  730. }
  731.  
  732. void ch6Vol(int adr) {
  733. if (ch6_vol > chVolLimitMax) ch6_vol = chVolLimitMax;
  734. if (ch6_vol < chVolLimitMin) ch6_vol = chVolLimitMin;
  735. ch6 = mas_vol + ch6_vol;
  736. set_address(adr);
  737. set_ch6(ch6);
  738. }
  739.  
  740. void powerUp() {
  741. if (power == 1) {
  742. lcd.clear();
  743. delay(500);
  744. lcd.setCursor(0, 1);
  745. lcd.print(" LOADING... ");
  746. delay(1000);
  747. lcd.clear();
  748. if (mas_vol == masVolLimitMin) {
  749. set_mute(1);
  750. } else {
  751. mute = 0;
  752. set_mute(mute);
  753. }
  754. vol_menu = 0;
  755. menu_active = 0;
  756. digitalWrite(powerOut, HIGH);
  757. delay(300);
  758.  
  759. } else {
  760.  
  761. set_mute(1);
  762. delay(100);
  763. digitalWrite(powerOut, LOW);
  764. menu_active = 100;
  765. }
  766. }
  767.  
  768. void startUp() {
  769. delay(500);
  770. pt2258();
  771. lcd.setCursor(0, 0);
  772. lcd.print(" DaacWave ");
  773. delay(300);
  774. lcd.setCursor(0, 1);
  775. lcd.print(" AUDIOS ");
  776. delay(1500);
  777. lcd.clear();
  778. delay(300);
  779. lcd.setCursor(0, 0);
  780. lcd.print(" 6CH-DVC ");
  781. lcd.setCursor(0, 1);
  782. lcd.print(" LOADING... ");
  783. delay(1200);
  784. lcd.clear();
  785. delay(200);
  786. for (int i = 1; i < 5; i++) {
  787. ch1Vol(i);
  788. ch2Vol(i);
  789. ch3Vol(i);
  790. ch4Vol(i);
  791. ch5Vol(i);
  792. ch6Vol(i);
  793. }
  794. }
  795.  
  796. //IR control --------------------------------------------------------------------------------//
  797. void IRControl() {
  798. if (irrecv.decode(&results)) {
  799. switch (results.value) {
  800. //power -------------------------------------------------//
  801. case ir_power:
  802. power++;
  803. if (power > 1) power = 0;
  804. powerUp();
  805. break;
  806. }
  807. if (power == 1) {
  808. switch (results.value) {
  809. //mute -------------------------------------------------//
  810. case ir_mute:
  811. soundMute();
  812. break;
  813. if (mute == 0) {
  814. //VOL -------------------------------------------------//
  815. case ir_vol_up:
  816. volumeUp();
  817. break;
  818.  
  819. case ir_vol_down:
  820. volumeDown();
  821. break;
  822. }
  823.  
  824. //CH1 -------------------------------------------------//
  825. case ir_ch_up:
  826. volumeMenuUp();
  827. break;
  828.  
  829. case ir_ch_down:
  830. volumeMenuDown();
  831. break;
  832. }
  833. }
  834. irrecv.resume();
  835. }
  836. }
  837.  
  838. void lcdDisplay() {
  839. int a, b, c;
  840. switch (menu_active) {
  841. case 0:
  842. //vol ----------------------------------------------//
  843. switch (vol_menu) {
  844. case 0:
  845. lcd.setCursor(0, 1);
  846. // (" ");
  847. lcd.print("MAS-VOL");
  848. lcd.setCursor(0, 0);
  849. c = mas_vol - masVolLimitMin;
  850. break;
  851.  
  852. case 1:
  853. lcd.setCursor(0, 1);
  854. // (" ");
  855. lcd.print("CH1-VOL");
  856. c = ch1_vol;
  857. break;
  858.  
  859. case 2:
  860. lcd.setCursor(0, 1);
  861. // (" ");
  862. lcd.print("CH2-VOL");
  863. c = ch2_vol;
  864. break;
  865.  
  866. case 3:
  867. lcd.setCursor(0, 1);
  868. // (" ");
  869. lcd.print("CH3-VOL");
  870. c = ch3_vol;
  871. break;
  872.  
  873. case 4:
  874. lcd.setCursor(0, 1);
  875. // (" ");
  876. lcd.print("CH4-VOL");
  877. c = ch4_vol;
  878. break;
  879.  
  880. case 5:
  881. lcd.setCursor(0, 1);
  882. // (" ");
  883. lcd.print("CH5-VOL");
  884. c = ch5_vol;
  885. break;
  886.  
  887. case 6:
  888. lcd.setCursor(0, 1);
  889. // (" ");
  890. lcd.print("CH6-VOL");
  891. c = ch6_vol;
  892. break;
  893. }
  894. break;
  895.  
  896. case 100:
  897. lcd.setCursor(0, 0);
  898. lcd.print(" ");
  899. lcd.setCursor(0, 1);
  900. if (muteLcdDelay == 0) {
  901. if ((millis() - muteLcdTime) >= muteLcdOnTime) {
  902. lcd.print(" ");
  903. muteLcdDelay = 1;
  904. muteLcdTime = millis();
  905. }
  906. } else {
  907. if ((millis() - muteLcdTime) >= muteLcdOffTime) {
  908. lcd.print(" STANDBY ");
  909. muteLcdDelay = 0;
  910. muteLcdTime = millis();
  911. }
  912. }
  913. break;
  914. }
  915. if (menu_active == 0) {
  916. if (mute == 1) {
  917. lcd.setCursor(0, 0);
  918. if (muteLcdDelay == 0) {
  919. if ((millis() - muteLcdTime) >= muteLcdOnTime) {
  920. lcd.print(" ");
  921. muteLcdDelay = 1;
  922. muteLcdTime = millis();
  923. }
  924. } else {
  925. if ((millis() - muteLcdTime) >= muteLcdOffTime) {
  926. lcd.print("MUTE ");
  927. muteLcdDelay = 0;
  928. muteLcdTime = millis();
  929. }
  930. }
  931.  
  932. } else {
  933. lcd.setCursor(0, 0);
  934. lcd.print("6CH-DVC");
  935. }
  936.  
  937. for (int i = 0; i < 8; i++)
  938. lcd.createChar(i, custom_num[i]);
  939.  
  940. int y;
  941. if (c < 0) {
  942. lcd.setCursor(8, 1);
  943. lcd.print("-");
  944. y = chVolLimitMax - (c + chVolLimitMax);
  945. } else if (c == chVolLimitMin) {
  946. lcd.setCursor(8, 1);
  947. lcd.print("-");
  948. y = chVolLimitMax;
  949. } else {
  950. lcd.setCursor(8, 1);
  951. lcd.print(" ");
  952. y = c;
  953. }
  954. a = y / 10;
  955. b = y - a * 10;
  956.  
  957. lcd.setCursor(9, 0);
  958. for (int i = 0; i < digit_width; i++)
  959. lcd.print(custom_num_top[a][i]);
  960.  
  961. lcd.setCursor(9, 1);
  962. for (int i = 0; i < digit_width; i++)
  963. lcd.print(custom_num_bot[a][i]);
  964.  
  965. lcd.setCursor(13, 0);
  966. for (int i = 0; i < digit_width; i++)
  967. lcd.print(custom_num_top[b][i]);
  968.  
  969. lcd.setCursor(13, 1);
  970. for (int i = 0; i < digit_width; i++)
  971. lcd.print(custom_num_bot[b][i]);
  972. }
  973. }
  974.  
  975. //EEPROM -----------------------------------------------------//
  976. void eepromUpdate() {
  977. EEPROM.update(0, mas_vol);
  978. EEPROM.update(1, ch1_vol + 10);
  979. EEPROM.update(2, ch2_vol + 10);
  980. EEPROM.update(3, ch3_vol + 10);
  981. EEPROM.update(4, ch4_vol + 10);
  982. EEPROM.update(5, ch5_vol + 10);
  983. EEPROM.update(6, ch6_vol + 10);
  984. }
  985.  
  986. void eepromRead() {
  987. mas_vol = EEPROM.read(0);
  988. ch1_vol = EEPROM.read(1) - 10;
  989. ch2_vol = EEPROM.read(2) - 10;
  990. ch3_vol = EEPROM.read(3) - 10;
  991. ch4_vol = EEPROM.read(4) - 10;
  992. ch5_vol = EEPROM.read(5) - 10;
  993. ch6_vol = EEPROM.read(6) - 10;
  994. }
  995.  
  996. void btnPress() {
  997. returnTime = millis();
  998. return_d = 1;
  999. }
  1000. void returnDelay() {
  1001. if (millis() - returnTime > 5000 && return_d == 1 && vol_menu != 0) {
  1002. vol_menu = 0;
  1003. return_d = 0;
  1004. lcd.clear();
  1005. }
  1006. }
  1007.  
  1008.  
  1009.  
  1010.  
  1011.  
  1012.  
  1013.  
  1014.  
  1015.  
  1016.  
  1017.  
  1018.  
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024.  
  1025.  
  1026.  
  1027.  
  1028.  
Success #stdin #stdout 0.03s 25904KB
stdin
Standard input is empty
stdout
#include <Wire.h>
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
#include <IRremote.h> 
#include <PT2258.h>
#define PT2258_address 0b1000100
#define RotaryB 11
#define RotaryA 12
#define RotarySW 13
#define SW1 A1      
#define SW2 A2        
#define SW3 A3       
#define RECV_PIN 8   
#define powerOut 10
// IR HEX code
#define ir_power 0x807F827D     // IR Power ON/OFF
#define ir_mute 0x807F42BD      // IR Mute
#define ir_vol_up 0x807F906F    // IR Vol++
#define ir_vol_down 0x807FA05F  // IR Vol--
#define ir_ch_up 0x807F40BF     // IR CH++
#define ir_ch_down 0x807FC03F   // IR CH-- 
LiquidCrystal_I2C lcd(0x27, 16, 2);

byte custom_num[8][8] = {
  { B00111, B01111, B11111, B11111, B11111, B11111, B11111, B11111 },
  { B11111, B11111, B11111, B00000, B00000, B00000, B00000, B00000 },
  { B11100, B11110, B11111, B11111, B11111, B11111, B11111, B11111 },
  { B11111, B11111, B11111, B11111, B11111, B11111, B01111, B00111 },
  { B00000, B00000, B00000, B00000, B00000, B11111, B11111, B11111 },
  { B11111, B11111, B11111, B11111, B11111, B11111, B11110, B11100 },
  { B11111, B11111, B11111, B00000, B00000, B00000, B11111, B11111 },
  { B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111 }
};
const int digit_width = 3;
const char custom_num_top[10][digit_width] = { 0, 1, 2, 1, 2, 32, 6, 6, 2, 6, 6, 2, 3, 4, 7, 7, 6, 6, 0, 6, 6, 1, 1, 2, 0, 6, 2, 0, 6, 2 };
const char custom_num_bot[10][digit_width] = { 3, 4, 5, 4, 7, 4, 7, 4, 4, 4, 4, 5, 32, 32, 7, 4, 4, 5, 3, 4, 5, 32, 32, 7, 3, 4, 5, 4, 4, 5 };
unsigned long returnTime;
unsigned long lastRotaryTime = 0;
unsigned long lastButtonTime = 0;
unsigned long muteLcdTime = 0;
int rotaryDelayTime = 200;
int buttonDelayTime = 100;
int muteLcdDelay = 0;
int muteLcdOnTime = 500;
int muteLcdOffTime = 500;

int masVolLimitMax = 69;
int masVolLimitMin = 19;
int chVolLimitMax = 10;
int chVolLimitMin = -10;
int lastEncoded = 0;

int vol_menu, mas_vol, ch1_vol, ch2_vol, ch3_vol, ch4_vol, ch5_vol, ch6_vol, mute, ch1, ch2, ch3, ch4, ch5, ch6;
int return_d, power, menu_active;
set_mute;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup() {
  Wire.begin();
  lcd.begin(16, 2);
  irrecv.enableIRIn();

  pinMode(RotaryA, INPUT);
  pinMode(RotaryB, INPUT);
  pinMode(RotarySW, INPUT_PULLUP);
  pinMode(SW1, INPUT);
  pinMode(SW2, INPUT);
  pinMode(SW3, INPUT);
  pinMode(powerOut, OUTPUT);

  digitalWrite(RotaryA, HIGH);
  digitalWrite(RotaryB, HIGH);

  power = 0;
  eepromRead();
  startUp();
  powerUp();
}
void loop() {
  lcdDisplay();
  eepromUpdate();
  IRControl();
  returnDelay();
  //Power -------------------------------------------------//
  if (analogRead(SW1) > 900) {
    if (millis() - lastButtonTime > buttonDelayTime) {
      power++;
      if (power > 1) power = 0;
      powerUp();
    }
    lastButtonTime = millis();
  }
  if (power == 1) {
    updateEncoder();
    //Mute -------------------------------------------------//
    if (analogRead(SW2) > 900) {
      if (millis() - lastButtonTime > buttonDelayTime) {
        soundMute();
      }
      lastButtonTime = millis();
    }
    if (digitalRead(RotarySW) == LOW) {
      if (millis() - lastButtonTime > buttonDelayTime) {
        volumeMenuUp();
      }
      lastButtonTime = millis();
    }
  }
}

void updateEncoder() {
  int CLK = digitalRead(RotaryA);
  int DT = digitalRead(RotaryB);

  int encoded = (CLK << 1) | DT;
  int addData = (lastEncoded << 2) | encoded;

  if (addData == 0b1101 || addData == 0b0100 || addData == 0b0010 || addData == 0b1011) {
    if ((millis() - lastRotaryTime) > rotaryDelayTime) {
      volumeUp();
      lastRotaryTime = millis();
    }
  }
  if (addData == 0b1110 || addData == 0b0111 || addData == 0b0001 || addData == 0b1000) {
    if ((millis() - lastRotaryTime) > rotaryDelayTime) {
      volumeDown();
      lastRotaryTime = millis();
    }
  }
  lastEncoded = encoded;
}

void volumeMenuUp() {
  vol_menu++;
  if (vol_menu > 6) vol_menu = 0;
  btnPress();
}

void volumeMenuDown() {
  vol_menu--;
  if (vol_menu < 0) vol_menu = 6;
  btnPress();
}

void volumeUp() {
  if (vol_menu == 0) {
    mas_vol++;
    masVol();
  }
  if (vol_menu == 1) {
    ch1_vol++;
    ch1Vol(1);
  }
  if (vol_menu == 2) {
    ch2_vol++;
    ch2Vol(1);
  }
  if (vol_menu == 3) {
    ch3_vol++;
    ch3Vol(1);
  }
  if (vol_menu == 4) {
    ch4_vol++;
    ch4Vol(1);
  }
  if (vol_menu == 5) {
    ch5_vol++;
    ch5Vol(1);
  }
  if (vol_menu == 6) {
    ch6_vol++;
    ch6Vol(1);
  }
  btnPress();
}

void volumeDown() {
  if (vol_menu == 0) {
    mas_vol--;
    masVol();
  }
  if (vol_menu == 1) {
    ch1_vol--;
    ch1Vol(1);
  }
  if (vol_menu == 2) {
    ch2_vol--;
    ch2Vol(1);
  }
  if (vol_menu == 3) {
    ch3_vol--;
    ch3Vol(1);
  }
  if (vol_menu == 4) {
    ch4_vol--;
    ch4Vol(1);
  }
  if (vol_menu == 5) {
    ch5_vol--;
    ch5Vol(1);
  }
  if (vol_menu == 6) {
    ch6_vol--;
    ch6Vol(1);
  }
  btnPress();
}

void soundMute() {
  mute++;
  if (mute > 1) mute = 0;
  set_mute(mute);
}

void masVol() {
  if (mas_vol > masVolLimitMax) mas_vol = masVolLimitMax;
  if (mas_vol < masVolLimitMin) mas_vol = masVolLimitMin;

  if (mute == 0) {
    if (mas_vol == masVolLimitMin) set_mute(1);
    else set_mute(0);
  }

  for (int i = 1; i < 5; i++) {
    ch1Vol(i);
    ch2Vol(i);
    ch3Vol(i);
    ch4Vol(i);
    ch5Vol(i);
    ch6Vol(i);
  }
}

void ch1Vol(int adr) {
  if (ch1_vol > chVolLimitMax) ch1_vol = chVolLimitMax;
  if (ch1_vol < chVolLimitMin) ch1_vol = chVolLimitMin;
  ch1 = mas_vol + ch1_vol;
  set_address(adr);
  set_ch1(ch1);
}

void ch2Vol(int adr) {
  if (ch2_vol > chVolLimitMax) ch2_vol = chVolLimitMax;
  if (ch2_vol < chVolLimitMin) ch2_vol = chVolLimitMin;
  ch2 = mas_vol + ch2_vol;
  set_address(adr);
  set_ch2(ch2);
}

void ch3Vol(int adr) {
  if (ch3_vol > chVolLimitMax) ch3_vol = chVolLimitMax;
  if (ch3_vol < chVolLimitMin) ch3_vol = chVolLimitMin;
  ch3 = mas_vol + ch3_vol;
  set_address(adr);
  set_ch3(ch3);
}

void ch4Vol(int adr) {
  if (ch4_vol > chVolLimitMax) ch4_vol = chVolLimitMax;
  if (ch4_vol < chVolLimitMin) ch4_vol = chVolLimitMin;
  ch4 = mas_vol + ch4_vol;
  set_address(adr);
  set_ch4(ch4);
}

void ch5Vol(int adr) {
  if (ch5_vol > chVolLimitMax) ch5_vol = chVolLimitMax;
  if (ch5_vol < chVolLimitMin) ch5_vol = chVolLimitMin;
  ch5 = mas_vol + ch5_vol;
  set_address(adr);
  set_ch5(ch5);
}

void ch6Vol(int adr) {
  if (ch6_vol > chVolLimitMax) ch6_vol = chVolLimitMax;
  if (ch6_vol < chVolLimitMin) ch6_vol = chVolLimitMin;
  ch6 = mas_vol + ch6_vol;
  set_address(adr);
  set_ch6(ch6);
}

void powerUp() {
  if (power == 1) {
    lcd.clear();
    delay(500);
    lcd.setCursor(0, 1);
    lcd.print("   LOADING...   ");
    delay(1000);
    lcd.clear();
    if (mas_vol == masVolLimitMin) {
      set_mute(1);
    } else {
      mute = 0;
      set_mute(mute);
    }
    vol_menu = 0;
    menu_active = 0;
    digitalWrite(powerOut, HIGH);
    delay(300);

  } else {

    set_mute(1);
    delay(100);
    digitalWrite(powerOut, LOW);
    menu_active = 100;
  }
}

void startUp() {
  delay(500);
  pt2258();
  lcd.setCursor(0, 0);
  lcd.print("    DaacWave    ");
  delay(300);
  lcd.setCursor(0, 1);
  lcd.print("     AUDIOS     ");
  delay(1500);
  lcd.clear();
  delay(300);
  lcd.setCursor(0, 0);
  lcd.print("   6CH-DVC      ");
  lcd.setCursor(0, 1);
  lcd.print("   LOADING...   ");
  delay(1200);
  lcd.clear();
  delay(200);
  for (int i = 1; i < 5; i++) {
    ch1Vol(i);
    ch2Vol(i);
    ch3Vol(i);
    ch4Vol(i);
    ch5Vol(i);
    ch6Vol(i);
  }
}

//IR control --------------------------------------------------------------------------------//
void IRControl() {
  if (irrecv.decode(&results)) {
    switch (results.value) {
      //power -------------------------------------------------//
      case ir_power:
        power++;
        if (power > 1) power = 0;
        powerUp();
        break;
    }
    if (power == 1) {
      switch (results.value) {
        //mute -------------------------------------------------//
        case ir_mute:
          soundMute();
          break;
          if (mute == 0) {
            //VOL -------------------------------------------------//
            case ir_vol_up:
              volumeUp();
              break;

            case ir_vol_down:
              volumeDown();
              break;
          }

        //CH1 -------------------------------------------------//
        case ir_ch_up:
          volumeMenuUp();
          break;

        case ir_ch_down:
          volumeMenuDown();
          break;
      }
    }
    irrecv.resume();
  }
}

void lcdDisplay() {
  int a, b, c;
  switch (menu_active) {
    case 0:
      //vol ----------------------------------------------//
      switch (vol_menu) {
        case 0:
          lcd.setCursor(0, 1);
          //       ("       ");
          lcd.print("MAS-VOL");
          lcd.setCursor(0, 0);
          c = mas_vol - masVolLimitMin;
          break;

        case 1:
          lcd.setCursor(0, 1);
          //       ("       ");
          lcd.print("CH1-VOL");
          c = ch1_vol;
          break;

        case 2:
          lcd.setCursor(0, 1);
          //       ("       ");
          lcd.print("CH2-VOL");
          c = ch2_vol;
          break;

        case 3:
          lcd.setCursor(0, 1);
          //       ("       ");
          lcd.print("CH3-VOL");
          c = ch3_vol;
          break;

        case 4:
          lcd.setCursor(0, 1);
          //       ("       ");
          lcd.print("CH4-VOL");
          c = ch4_vol;
          break;

        case 5:
          lcd.setCursor(0, 1);
          //       ("       ");
          lcd.print("CH5-VOL");
          c = ch5_vol;
          break;

        case 6:
          lcd.setCursor(0, 1);
          //       ("       ");
          lcd.print("CH6-VOL");
          c = ch6_vol;
          break;
      }
      break;

    case 100:
      lcd.setCursor(0, 0);
      lcd.print("                ");
      lcd.setCursor(0, 1);
      if (muteLcdDelay == 0) {
        if ((millis() - muteLcdTime) >= muteLcdOnTime) {
          lcd.print("                ");
          muteLcdDelay = 1;
          muteLcdTime = millis();
        }
      } else {
        if ((millis() - muteLcdTime) >= muteLcdOffTime) {
          lcd.print("    STANDBY     ");
          muteLcdDelay = 0;
          muteLcdTime = millis();
        }
      }
      break;
  }
  if (menu_active == 0) {
    if (mute == 1) {
      lcd.setCursor(0, 0);
      if (muteLcdDelay == 0) {
        if ((millis() - muteLcdTime) >= muteLcdOnTime) {
          lcd.print("       ");
          muteLcdDelay = 1;
          muteLcdTime = millis();
        }
      } else {
        if ((millis() - muteLcdTime) >= muteLcdOffTime) {
          lcd.print("MUTE   ");
          muteLcdDelay = 0;
          muteLcdTime = millis();
        }
      }

    } else {
      lcd.setCursor(0, 0);
      lcd.print("6CH-DVC");
    }

    for (int i = 0; i < 8; i++)
      lcd.createChar(i, custom_num[i]);

    int y;
    if (c < 0) {
      lcd.setCursor(8, 1);
      lcd.print("-");
      y = chVolLimitMax - (c + chVolLimitMax);
    } else if (c == chVolLimitMin) {
      lcd.setCursor(8, 1);
      lcd.print("-");
      y = chVolLimitMax;
    } else {
      lcd.setCursor(8, 1);
      lcd.print(" ");
      y = c;
    }
    a = y / 10;
    b = y - a * 10;

    lcd.setCursor(9, 0);
    for (int i = 0; i < digit_width; i++)
      lcd.print(custom_num_top[a][i]);

    lcd.setCursor(9, 1);
    for (int i = 0; i < digit_width; i++)
      lcd.print(custom_num_bot[a][i]);

    lcd.setCursor(13, 0);
    for (int i = 0; i < digit_width; i++)
      lcd.print(custom_num_top[b][i]);

    lcd.setCursor(13, 1);
    for (int i = 0; i < digit_width; i++)
      lcd.print(custom_num_bot[b][i]);
  }
}

//EEPROM -----------------------------------------------------//
void eepromUpdate() {
  EEPROM.update(0, mas_vol);
  EEPROM.update(1, ch1_vol + 10);
  EEPROM.update(2, ch2_vol + 10);
  EEPROM.update(3, ch3_vol + 10);
  EEPROM.update(4, ch4_vol + 10);
  EEPROM.update(5, ch5_vol + 10);
  EEPROM.update(6, ch6_vol + 10);
}

void eepromRead() {
  mas_vol = EEPROM.read(0);
  ch1_vol = EEPROM.read(1) - 10;
  ch2_vol = EEPROM.read(2) - 10;
  ch3_vol = EEPROM.read(3) - 10;
  ch4_vol = EEPROM.read(4) - 10;
  ch5_vol = EEPROM.read(5) - 10;
  ch6_vol = EEPROM.read(6) - 10;
}

void btnPress() {
  returnTime = millis();
  return_d = 1;
}
void returnDelay() {
  if (millis() - returnTime > 5000 && return_d == 1 && vol_menu != 0) {
    vol_menu = 0;
    return_d = 0;
    lcd.clear();
  }
}
if (power == 1) {
    updateEncoder();
    //Mute -------------------------------------------------//
    if (analogRead(SW2) > 900) {
      if (millis() - lastButtonTime > buttonDelayTime) {
        soundMute();
      }
      lastButtonTime = millis();
    }
    if (digitalRead(RotarySW) == LOW) {
      if (millis() - lastButtonTime > buttonDelayTime) {
        volumeMenuUp();
      }
      lastButtonTime = millis();
    }
  }
}

void updateEncoder() {
  int CLK = digitalRead(RotaryA);
  int DT = digitalRead(RotaryB);

  int encoded = (CLK << 1) | DT;
  int addData = (lastEncoded << 2) | encoded;

  if (addData == 0b1101 || addData == 0b0100 || addData == 0b0010 || addData == 0b1011) {
    if ((millis() - lastRotaryTime) > rotaryDelayTime) {
      volumeUp();
      lastRotaryTime = millis();
    }
  }
  if (addData == 0b1110 || addData == 0b0111 || addData == 0b0001 || addData == 0b1000) {
    if ((millis() - lastRotaryTime) > rotaryDelayTime) {
      volumeDown();
      lastRotaryTime = millis();
    }
  }
  lastEncoded = encoded;
}

void volumeMenuUp() {
  vol_menu++;
  if (vol_menu > 6) vol_menu = 0;
  btnPress();
}

void volumeMenuDown() {
  vol_menu--;
  if (vol_menu < 0) vol_menu = 6;
  btnPress();
}

void volumeUp() {
  if (vol_menu == 0) {
    mas_vol++;
    masVol();
  }
  if (vol_menu == 1) {
    ch1_vol++;
    ch1Vol(1);
  }
  if (vol_menu == 2) {
    ch2_vol++;
    ch2Vol(1);
  }
  if (vol_menu == 3) {
    ch3_vol++;
    ch3Vol(1);
  }
  if (vol_menu == 4) {
    ch4_vol++;
    ch4Vol(1);
  }
  if (vol_menu == 5) {
    ch5_vol++;
    ch5Vol(1);
  }
  if (vol_menu == 6) {
    ch6_vol++;
    ch6Vol(1);
  }
  btnPress();
}

void volumeDown() {
  if (vol_menu == 0) {
    mas_vol--;
    masVol();
  }
  if (vol_menu == 1) {
    ch1_vol--;
    ch1Vol(1);
  }
  if (vol_menu == 2) {
    ch2_vol--;
    ch2Vol(1);
  }
  if (vol_menu == 3) {
    ch3_vol--;
    ch3Vol(1);
  }
  if (vol_menu == 4) {
    ch4_vol--;
    ch4Vol(1);
  }
  if (vol_menu == 5) {
    ch5_vol--;
    ch5Vol(1);
  }
  if (vol_menu == 6) {
    ch6_vol--;
    ch6Vol(1);
  }
  btnPress();
}

void soundMute() {
  mute++;
  if (mute > 1) mute = 0;
  set_mute(mute);
}

void masVol() {
  if (mas_vol > masVolLimitMax) mas_vol = masVolLimitMax;
  if (mas_vol < masVolLimitMin) mas_vol = masVolLimitMin;

  if (mute == 0) {
    if (mas_vol == masVolLimitMin) set_mute(1);
    else set_mute(0);
  }

  for (int i = 1; i < 5; i++) {
    ch1Vol(i);
    ch2Vol(i);
    ch3Vol(i);
    ch4Vol(i);
    ch5Vol(i);
    ch6Vol(i);
  }
}

void ch1Vol(int adr) {
  if (ch1_vol > chVolLimitMax) ch1_vol = chVolLimitMax;
  if (ch1_vol < chVolLimitMin) ch1_vol = chVolLimitMin;
  ch1 = mas_vol + ch1_vol;
  set_address(adr);
  set_ch1(ch1);
}

void ch2Vol(int adr) {
  if (ch2_vol > chVolLimitMax) ch2_vol = chVolLimitMax;
  if (ch2_vol < chVolLimitMin) ch2_vol = chVolLimitMin;
  ch2 = mas_vol + ch2_vol;
  set_address(adr);
  set_ch2(ch2);
}

void ch3Vol(int adr) {
  if (ch3_vol > chVolLimitMax) ch3_vol = chVolLimitMax;
  if (ch3_vol < chVolLimitMin) ch3_vol = chVolLimitMin;
  ch3 = mas_vol + ch3_vol;
  set_address(adr);
  set_ch3(ch3);
}

void ch4Vol(int adr) {
  if (ch4_vol > chVolLimitMax) ch4_vol = chVolLimitMax;
  if (ch4_vol < chVolLimitMin) ch4_vol = chVolLimitMin;
  ch4 = mas_vol + ch4_vol;
  set_address(adr);
  set_ch4(ch4);
}

void ch5Vol(int adr) {
  if (ch5_vol > chVolLimitMax) ch5_vol = chVolLimitMax;
  if (ch5_vol < chVolLimitMin) ch5_vol = chVolLimitMin;
  ch5 = mas_vol + ch5_vol;
  set_address(adr);
  set_ch5(ch5);
}

void ch6Vol(int adr) {
  if (ch6_vol > chVolLimitMax) ch6_vol = chVolLimitMax;
  if (ch6_vol < chVolLimitMin) ch6_vol = chVolLimitMin;
  ch6 = mas_vol + ch6_vol;
  set_address(adr);
  set_ch6(ch6);
}

void powerUp() {
  if (power == 1) {
    lcd.clear();
    delay(500);
    lcd.setCursor(0, 1);
    lcd.print("   LOADING...   ");
    delay(1000);
    lcd.clear();
    if (mas_vol == masVolLimitMin) {
      set_mute(1);
    } else {
      mute = 0;
      set_mute(mute);
    }
    vol_menu = 0;
    menu_active = 0;
    digitalWrite(powerOut, HIGH);
    delay(300);

  } else {

    set_mute(1);
    delay(100);
    digitalWrite(powerOut, LOW);
    menu_active = 100;
  }
}

void startUp() {
  delay(500);
  pt2258();
  lcd.setCursor(0, 0);
  lcd.print("    DaacWave    ");
  delay(300);
  lcd.setCursor(0, 1);
  lcd.print("     AUDIOS     ");
  delay(1500);
  lcd.clear();
  delay(300);
  lcd.setCursor(0, 0);
  lcd.print("   6CH-DVC      ");
  lcd.setCursor(0, 1);
  lcd.print("   LOADING...   ");
  delay(1200);
  lcd.clear();
  delay(200);
  for (int i = 1; i < 5; i++) {
    ch1Vol(i);
    ch2Vol(i);
    ch3Vol(i);
    ch4Vol(i);
    ch5Vol(i);
    ch6Vol(i);
  }
}

//IR control --------------------------------------------------------------------------------//
void IRControl() {
  if (irrecv.decode(&results)) {
    switch (results.value) {
      //power -------------------------------------------------//
      case ir_power:
        power++;
        if (power > 1) power = 0;
        powerUp();
        break;
    }
    if (power == 1) {
      switch (results.value) {
        //mute -------------------------------------------------//
        case ir_mute:
          soundMute();
          break;
          if (mute == 0) {
            //VOL -------------------------------------------------//
            case ir_vol_up:
              volumeUp();
              break;

            case ir_vol_down:
              volumeDown();
              break;
          }

        //CH1 -------------------------------------------------//
        case ir_ch_up:
          volumeMenuUp();
          break;

        case ir_ch_down:
          volumeMenuDown();
          break;
      }
    }
    irrecv.resume();
  }
}

void lcdDisplay() {
  int a, b, c;
  switch (menu_active) {
    case 0:
      //vol ----------------------------------------------//
      switch (vol_menu) {
        case 0:
          lcd.setCursor(0, 1);
          //       ("       ");
          lcd.print("MAS-VOL");
          lcd.setCursor(0, 0);
          c = mas_vol - masVolLimitMin;
          break;

        case 1:
          lcd.setCursor(0, 1);
          //       ("       ");
          lcd.print("CH1-VOL");
          c = ch1_vol;
          break;

        case 2:
          lcd.setCursor(0, 1);
          //       ("       ");
          lcd.print("CH2-VOL");
          c = ch2_vol;
          break;

        case 3:
          lcd.setCursor(0, 1);
          //       ("       ");
          lcd.print("CH3-VOL");
          c = ch3_vol;
          break;

        case 4:
          lcd.setCursor(0, 1);
          //       ("       ");
          lcd.print("CH4-VOL");
          c = ch4_vol;
          break;

        case 5:
          lcd.setCursor(0, 1);
          //       ("       ");
          lcd.print("CH5-VOL");
          c = ch5_vol;
          break;

        case 6:
          lcd.setCursor(0, 1);
          //       ("       ");
          lcd.print("CH6-VOL");
          c = ch6_vol;
          break;
      }
      break;

    case 100:
      lcd.setCursor(0, 0);
      lcd.print("                ");
      lcd.setCursor(0, 1);
      if (muteLcdDelay == 0) {
        if ((millis() - muteLcdTime) >= muteLcdOnTime) {
          lcd.print("                ");
          muteLcdDelay = 1;
          muteLcdTime = millis();
        }
      } else {
        if ((millis() - muteLcdTime) >= muteLcdOffTime) {
          lcd.print("    STANDBY     ");
          muteLcdDelay = 0;
          muteLcdTime = millis();
        }
      }
      break;
  }
  if (menu_active == 0) {
    if (mute == 1) {
      lcd.setCursor(0, 0);
      if (muteLcdDelay == 0) {
        if ((millis() - muteLcdTime) >= muteLcdOnTime) {
          lcd.print("       ");
          muteLcdDelay = 1;
          muteLcdTime = millis();
        }
      } else {
        if ((millis() - muteLcdTime) >= muteLcdOffTime) {
          lcd.print("MUTE   ");
          muteLcdDelay = 0;
          muteLcdTime = millis();
        }
      }

    } else {
      lcd.setCursor(0, 0);
      lcd.print("6CH-DVC");
    }

    for (int i = 0; i < 8; i++)
      lcd.createChar(i, custom_num[i]);

    int y;
    if (c < 0) {
      lcd.setCursor(8, 1);
      lcd.print("-");
      y = chVolLimitMax - (c + chVolLimitMax);
    } else if (c == chVolLimitMin) {
      lcd.setCursor(8, 1);
      lcd.print("-");
      y = chVolLimitMax;
    } else {
      lcd.setCursor(8, 1);
      lcd.print(" ");
      y = c;
    }
    a = y / 10;
    b = y - a * 10;

    lcd.setCursor(9, 0);
    for (int i = 0; i < digit_width; i++)
      lcd.print(custom_num_top[a][i]);

    lcd.setCursor(9, 1);
    for (int i = 0; i < digit_width; i++)
      lcd.print(custom_num_bot[a][i]);

    lcd.setCursor(13, 0);
    for (int i = 0; i < digit_width; i++)
      lcd.print(custom_num_top[b][i]);

    lcd.setCursor(13, 1);
    for (int i = 0; i < digit_width; i++)
      lcd.print(custom_num_bot[b][i]);
  }
}

//EEPROM -----------------------------------------------------//
void eepromUpdate() {
  EEPROM.update(0, mas_vol);
  EEPROM.update(1, ch1_vol + 10);
  EEPROM.update(2, ch2_vol + 10);
  EEPROM.update(3, ch3_vol + 10);
  EEPROM.update(4, ch4_vol + 10);
  EEPROM.update(5, ch5_vol + 10);
  EEPROM.update(6, ch6_vol + 10);
}

void eepromRead() {
  mas_vol = EEPROM.read(0);
  ch1_vol = EEPROM.read(1) - 10;
  ch2_vol = EEPROM.read(2) - 10;
  ch3_vol = EEPROM.read(3) - 10;
  ch4_vol = EEPROM.read(4) - 10;
  ch5_vol = EEPROM.read(5) - 10;
  ch6_vol = EEPROM.read(6) - 10;
}

void btnPress() {
  returnTime = millis();
  return_d = 1;
}
void returnDelay() {
  if (millis() - returnTime > 5000 && return_d == 1 && vol_menu != 0) {
    vol_menu = 0;
    return_d = 0;
    lcd.clear();
  }
}