fork download
  1. // AX2358 5.1 Surround System
  2. // DaacWaves <https://d...content-available-to-author-only...t.com>
  3.  
  4. #include <Wire.h>
  5. #include <EEPROM.h>
  6. #include <LiquidCrystal_I2C.h>
  7. #include <IRremote.h>
  8.  
  9. #define AX2358_address 0b1001010
  10.  
  11. #define btn_delay 300
  12.  
  13. #define sw01 9 // SW
  14. #define sw02 11 // DT
  15. #define sw03 10 // CLK
  16. #define sw04 A0 // Input
  17. #define sw05 A1 // Mute
  18. #define sw06 A2 // Power
  19.  
  20. #define sw_power 13 // Out
  21.  
  22. // IR HEX code
  23. #define ir_power 0x807F827D // IR power ON/OFF
  24. #define ir_mute 0x609FFF80 // IR mute
  25. #define ir_in_0 0x807F629D // IR input USB
  26. #define ir_in_1 0x807F52AD // IR input BLU
  27. #define ir_in_2 0x807FA25D // IR input FM
  28. #define ir_in_3 0x807F22DD // IR input AUX
  29. #define ir_in_4 0x807F20DF // IR input DVD
  30. #define ir_vol_i 0x807F906F // IR vol++
  31. #define ir_vol_d 0x807FA05F // IR vol--
  32. #define ir_fl_i 0x807F40BF // IR fl++
  33. #define ir_fl_d 0x807FC03F // IR fl--
  34. #define ir_fr_i 0x807F00FF // IR fr++
  35. #define ir_fr_d 0x807F807F // IR fr--
  36. #define ir_sl_i 0x807F48B7 // IR sl++
  37. #define ir_sl_d 0x807FC837 // IR sl--
  38. #define ir_sr_i 0x807F08F7 // IR sr++
  39. #define ir_sr_d 0x807F8877 // IR sr--
  40. #define ir_cn_i 0x807F50AF // IR cn++
  41. #define ir_cn_d 0x807F609F // IR cn--
  42. #define ir_sub_i 0x807FD02F // IR sub++
  43. #define ir_sub_d 0x807FE01F // IR sub--
  44. #define ir_sp_mode 0x807F0AF5 // IR speaker mode change
  45. #define ir_surr_mode 0x807FA857 // IR surround ON/OFF
  46. #define ir_mix_mode 0x00000000 // IR -6dB ON/OFF
  47. #define ir_reset 0x807F1AE5 // IR reset
  48.  
  49. IRrecv irrecv(8);
  50. decode_results results;
  51.  
  52. byte custom_num[8][8] = {
  53. { B00111, B01111, B11111, B11111, B11111, B11111, B11111, B11111 },
  54. { B11111, B11111, B11111, B00000, B00000, B00000, B00000, B00000 },
  55. { B11100, B11110, B11111, B11111, B11111, B11111, B11111, B11111 },
  56. { B11111, B11111, B11111, B11111, B11111, B11111, B01111, B00111 },
  57. { B00000, B00000, B00000, B00000, B00000, B11111, B11111, B11111 },
  58. { B11111, B11111, B11111, B11111, B11111, B11111, B11110, B11100 },
  59. { B11111, B11111, B11111, B00000, B00000, B00000, B11111, B11111 },
  60. { B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111 }
  61. };
  62.  
  63. const int digit_width = 3;
  64. 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};
  65. 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};
  66.  
  67. byte arrow_right[8] = {B00000, B10000, B11000, B11100, B11110, B11100, B11000, B10000};
  68.  
  69. LiquidCrystal_I2C lcd(0x27, 16, 2);
  70.  
  71. unsigned long time;
  72. int in, mute, return_d, surr, mix, a, b, x, power, menu, menu_active, ch_mute, speaker_mode, btn_press, long_press, vol_menu, vol_menu_jup, reset;
  73. int fl, fr, sl, sr, cn, sub, ir_menu, ir_on, mas_vol, fl_vol, fr_vol, sl_vol, sr_vol, cn_vol, sub_vol;
  74.  
  75. long btn_timer = 0;
  76. long long_press_time = 600;
  77.  
  78. void setup() {
  79. Wire.begin();
  80. Serial.begin(9600);
  81. irrecv.enableIRIn();
  82.  
  83. pinMode(sw01, INPUT); // SW
  84. pinMode(sw02, INPUT); // DT
  85. pinMode(sw03, INPUT); // CLK
  86. pinMode(sw04, INPUT); // Input
  87. pinMode(sw05, INPUT); // Mute
  88. pinMode(sw06, INPUT); // Power
  89. pinMode(sw_power, OUTPUT); // Out
  90.  
  91. digitalWrite(sw_power, LOW);
  92.  
  93. lcd.begin(16, 2);
  94.  
  95. power = 0;
  96. eeprom_read();
  97. start_up();
  98. power_up();
  99.  
  100. }
  101.  
  102. void loop() {
  103. lcd_update();
  104. eeprom_update();
  105. ir_control();
  106. return_delay();
  107.  
  108. if (menu_active == 0) {
  109. custom_num_shape();
  110. } else {
  111. custom_shape();
  112. }
  113.  
  114. //power -------------------------------------------------//
  115. if (analogRead(sw06) > 900) {
  116. power++;
  117. if (power > 1) {
  118. power = 0;
  119. }
  120. power_up();
  121. delay(btn_delay);
  122. }
  123.  
  124. if (power == 1) {
  125. //select input -------------------------------------------------//
  126. if (analogRead(sw04) > 900) {
  127. in++;
  128. set_in();
  129. delay(btn_delay);
  130. }
  131.  
  132. //select menu -------------------------------------------------//
  133. if (digitalRead(sw01) == LOW) {
  134. if (btn_press == 0) {
  135. btn_press = 1;
  136. btn_timer = millis();
  137. }
  138. if ((millis() - btn_timer > long_press_time) && (long_press == 0) && (menu_active == 0)) {
  139. long_press = 1;
  140. menu_active = 1;
  141. menu = 1;
  142. btn_cl();
  143. lcd.clear();
  144. } else if ((millis() - btn_timer > long_press_time) && (long_press == 0) && (menu_active == 1)) {
  145. long_press = 1;
  146. menu_active = 0;
  147. vol_menu = 0;
  148. reset = 0;
  149. btn_cl();
  150. lcd.clear();
  151. }
  152. } else {
  153. if (btn_press == 1) {
  154. if (long_press == 1) {
  155. long_press = 0;
  156. } else {
  157. if (menu_active == 1) {
  158. menu++;
  159. if (menu > 4) {
  160. menu = 1;
  161. }
  162. btn_cl();
  163. lcd.clear();
  164. } else if (menu_active == 0 && speaker_mode == 0) {
  165. vol_menu++;
  166. if (vol_menu > 6) {
  167. vol_menu = 0;
  168. }
  169. btn_cl();
  170. } else if (menu_active == 0 && speaker_mode == 1) {
  171. vol_menu++;
  172. if (vol_menu_jup == 0) {
  173. if (vol_menu > 2) {
  174. vol_menu = 6;
  175. vol_menu_jup = 1;
  176. }
  177. }
  178. if (vol_menu_jup == 1) {
  179. if (vol_menu > 6) {
  180. vol_menu = 0;
  181. vol_menu_jup = 0;
  182. }
  183. }
  184. btn_cl();
  185. }
  186. }
  187. btn_press = 0;
  188. }
  189. }
  190.  
  191. //mute -------------------------------------------------//
  192. if (analogRead(sw05) > 900 && mas_vol != 19) {
  193. mute++;
  194. if (mute == 1) {
  195. menu_active = 99;
  196. } else {
  197. menu_active = 0;
  198. }
  199. set_mute();
  200. delay(btn_delay);
  201. lcd.clear();
  202. }
  203. }
  204.  
  205. //menu active 0 -------------------------------------------------//
  206. if (menu_active == 0) {
  207. if (digitalRead(sw02) == LOW) {
  208. if (vol_menu == 0) {
  209. mas_vol++;
  210. }
  211. if (vol_menu == 1) {
  212. fl_vol++;
  213. }
  214. if (vol_menu == 2) {
  215. fr_vol++;
  216. }
  217. if (vol_menu == 3) {
  218. sl_vol++;
  219. }
  220. if (vol_menu == 4) {
  221. sr_vol++;
  222. }
  223. if (vol_menu == 5) {
  224. cn_vol++;
  225. }
  226. if (vol_menu == 6) {
  227. sub_vol++;
  228. }
  229. set_mas_vol();
  230. set_fl();
  231. set_fr();
  232. set_sub();
  233. if (speaker_mode == 0) {
  234. set_sl();
  235. set_sr();
  236. set_cn();
  237. }
  238. btn_cl();
  239. }
  240. if (digitalRead(sw03) == LOW) {
  241. if (vol_menu == 0) {
  242. mas_vol--;
  243. }
  244. if (vol_menu == 1) {
  245. fl_vol--;
  246. }
  247. if (vol_menu == 2) {
  248. fr_vol--;
  249. }
  250. if (vol_menu == 3) {
  251. sl_vol--;
  252. }
  253. if (vol_menu == 4) {
  254. sr_vol--;
  255. }
  256. if (vol_menu == 5) {
  257. cn_vol--;
  258. }
  259. if (vol_menu == 6) {
  260. sub_vol--;
  261. }
  262. set_mas_vol();
  263. set_fl();
  264. set_fr();
  265. set_sub();
  266. if (speaker_mode == 0) {
  267. set_sl();
  268. set_sr();
  269. set_cn();
  270. }
  271. btn_cl();
  272. }
  273. }
  274.  
  275. //menu active 1 -------------------------------------------------//
  276. if (menu_active == 1) {
  277. if (menu == 1) {
  278. if (digitalRead(sw02) == LOW) {
  279. surr++;
  280. set_surr();
  281. btn_cl();
  282. }
  283. }
  284. if (menu == 2) {
  285. if (digitalRead(sw02) == LOW) {
  286. speaker_mode++;
  287. if (speaker_mode == 1) {
  288. vol_menu_jup = 0;
  289. }
  290. set_speaker_mode();
  291. btn_cl();
  292. }
  293. }
  294. if (menu == 3) {
  295. if (digitalRead(sw02) == LOW) {
  296. mix++;
  297. set_mix();
  298. btn_cl();
  299. }
  300. }
  301. if (menu == 4) {
  302. if (digitalRead(sw02) == LOW) {
  303. reset++;
  304. set_reset();
  305. btn_cl();
  306. }
  307. }
  308. }
  309. }
  310.  
  311. //eeprom -----------------------------------------------------//
  312.  
  313. void eeprom_update() {
  314. EEPROM.update(0, in);
  315. EEPROM.update(1, mas_vol);
  316. EEPROM.update(2, fl_vol + 10);
  317. EEPROM.update(3, fr_vol + 10);
  318. EEPROM.update(4, sl_vol + 10);
  319. EEPROM.update(5, sr_vol + 10);
  320. EEPROM.update(6, cn_vol + 10);
  321. EEPROM.update(7, sub_vol + 10);
  322. EEPROM.update(8, surr);
  323. EEPROM.update(9, speaker_mode);
  324. EEPROM.update(10, mix);
  325. }
  326.  
  327. void eeprom_read() {
  328. in = EEPROM.read(0);
  329. mas_vol = EEPROM.read(1);
  330. fl_vol = EEPROM.read(2) - 10;
  331. fr_vol = EEPROM.read(3) - 10;
  332. sl_vol = EEPROM.read(4) - 10;
  333. sr_vol = EEPROM.read(5) - 10;
  334. cn_vol = EEPROM.read(6) - 10;
  335. sub_vol = EEPROM.read(7) - 10;
  336. surr = EEPROM.read(8);
  337. speaker_mode = EEPROM.read(9);
  338. mix = EEPROM.read(10);
  339. }
  340.  
  341. void btn_cl() {
  342. delay(btn_delay);
  343. time = millis();
  344. return_d = 1;
  345. }
  346. void ir_cl() {
  347. time = millis();
  348. return_d = 1;
  349. }
  350. void return_delay() {
  351. if (millis() - time > 5000 && return_d == 1 && mute == 0 && menu_active != 0) {
  352. menu_active = 0;
  353. vol_menu = 0;
  354. reset = 0;
  355. return_d = 0;
  356. lcd.clear();
  357. } else if (millis() - time > 5000 && return_d == 1 && mute == 0 && menu_active == 0) {
  358. vol_menu = 0;
  359. return_d = 0;
  360. }
  361. }
  362.  
  363. //power up -----------------------------------------------------//
  364.  
  365. void power_up() {
  366. if (power == 1) {
  367. lcd.clear();
  368. delay(500);
  369. lcd.setCursor(0, 1);
  370. lcd.print(" LOADING... ");
  371. delay(1000);
  372. lcd.clear();
  373. if (mas_vol > 19) {
  374. mute = 0;
  375. }
  376. set_mute();
  377. vol_menu = 0;
  378. menu_active = 0;
  379. delay(300);
  380. ir_on = 1;
  381. vol_menu_jup = 0;
  382. digitalWrite(sw_power, HIGH);
  383.  
  384. } else {
  385.  
  386. (sw_power, LOW);
  387. mute = 1;
  388. set_mute();
  389. delay(100);
  390. menu_active = 100;
  391. ir_on = 0;
  392. }
  393. }
  394.  
  395. void start_up() {
  396. mute = 1;
  397. set_mute();
  398. delay(500);
  399. lcd.setCursor(0, 0);
  400. lcd.print(" Ui Tech ");
  401. delay(500);
  402. lcd.setCursor(0, 1);
  403. lcd.print(" 5.1 SYSTEM ");
  404. delay(1000);
  405. lcd.clear();
  406. delay(300);
  407. lcd.setCursor(0, 1);
  408. lcd.print(" LOADING... ");
  409. delay(1500);
  410. lcd.clear();
  411. delay(300);
  412. AX2358();
  413. set_in();
  414. set_surr();
  415. set_mix();
  416. set_fl();
  417. set_fr();
  418. set_sl();
  419. set_sr();
  420. set_cn();
  421. set_sub();
  422. }
  423.  
  424. //IR control --------------------------------------------------------------------------------//
  425.  
  426. void ir_control() {
  427. if ( irrecv.decode( &results )) {
  428.  
  429. switch (results.value) {
  430. //power -------------------------------------------------//
  431. case ir_power:
  432. power++;
  433. if (power > 1) {
  434. power = 0;
  435. }
  436. power_up();
  437. break;
  438. }
  439. if (ir_on == 1) {
  440. switch (results.value) {
  441. //mute -------------------------------------------------//
  442. case ir_mute:
  443. if (mas_vol != 19) {
  444. mute++;
  445. if (mute == 1) {
  446. menu_active = 99;
  447. } else {
  448. menu_active = 0;
  449. }
  450. set_mute();
  451. lcd.clear();
  452. }
  453.  
  454. break;
  455.  
  456. //select input -------------------------------------------------//
  457. case ir_in_0:
  458. in = 0;
  459. set_in();
  460. ir_cl();
  461. break;
  462.  
  463. case ir_in_1:
  464. in = 1;
  465. set_in();
  466. ir_cl();
  467. break;
  468.  
  469. case ir_in_2:
  470. in = 2;
  471. set_in();
  472. ir_cl();
  473. break;
  474.  
  475. case ir_in_3:
  476. in = 3;
  477. set_in();
  478. ir_cl();
  479. break;
  480.  
  481. case ir_in_4:
  482. in = 4;
  483. set_in();
  484. ir_cl();
  485. break;
  486. }
  487. }
  488.  
  489. if (ir_on == 1 && menu_active == 0) {
  490. switch (results.value) {
  491. //VOL -------------------------------------------------//
  492. case ir_vol_i:
  493. if (speaker_mode == 0 || speaker_mode == 1) {
  494. mas_vol++;
  495. vol_menu = 0;
  496. set_mas_vol();
  497. set_fl();
  498. set_fr();
  499. set_sub();
  500. if (speaker_mode == 0) {
  501. set_sl();
  502. set_sr();
  503. set_cn();
  504. }
  505. }
  506. break;
  507.  
  508. case ir_vol_d:
  509. if (speaker_mode == 0 || speaker_mode == 1) {
  510. mas_vol--;
  511. vol_menu = 0;
  512. set_mas_vol();
  513. set_fl();
  514. set_fr();
  515. set_sub();
  516. if (speaker_mode == 0) {
  517. set_sl();
  518. set_sr();
  519. set_cn();
  520. }
  521. }
  522. break;
  523.  
  524. //FL -------------------------------------------------//
  525. case ir_fl_i:
  526. if (speaker_mode == 0 || speaker_mode == 1) {
  527. fl_vol++;
  528. vol_menu = 1;
  529. set_fl();
  530. }
  531. break;
  532.  
  533. case ir_fl_d:
  534. if (speaker_mode == 0 || speaker_mode == 1) {
  535. fl_vol--;
  536. vol_menu = 1;
  537. set_fl();
  538. }
  539. break;
  540.  
  541. //FR -------------------------------------------------//
  542. case ir_fr_i:
  543. if (speaker_mode == 0 || speaker_mode == 1) {
  544. fr_vol++;
  545. vol_menu = 2;
  546. set_fr();
  547. }
  548. break;
  549.  
  550. case ir_fr_d:
  551. if (speaker_mode == 0 || speaker_mode == 1) {
  552. fr_vol--;
  553. vol_menu = 2;
  554. set_fr();
  555. }
  556. break;
  557.  
  558. //SL -------------------------------------------------//
  559. case ir_sl_i:
  560. if (speaker_mode == 0) {
  561. sl_vol++;
  562. vol_menu = 3;
  563. set_sl();
  564. }
  565. break;
  566.  
  567. case ir_sl_d:
  568. if (speaker_mode == 0) {
  569. sl_vol--;
  570. vol_menu = 3;
  571. set_sl();
  572. }
  573. break;
  574.  
  575. //SR -------------------------------------------------//
  576. case ir_sr_i:
  577. if (speaker_mode == 0) {
  578. sr_vol++;
  579. vol_menu = 4;
  580. set_sr();
  581. }
  582. break;
  583.  
  584. case ir_sr_d:
  585. if (speaker_mode == 0) {
  586. sr_vol--;
  587. vol_menu = 4;
  588. set_sr();
  589. }
  590. break;
  591.  
  592. //CN -------------------------------------------------//
  593. case ir_cn_i:
  594. if (speaker_mode == 0) {
  595. cn_vol++;
  596. vol_menu = 5;
  597. set_cn();
  598. }
  599. break;
  600.  
  601. case ir_cn_d:
  602. if (speaker_mode == 0) {
  603. cn_vol--;
  604. vol_menu = 5;
  605. set_cn();
  606. }
  607. break;
  608.  
  609. //SUB -------------------------------------------------//
  610. case ir_sub_i:
  611. if (speaker_mode == 0 || speaker_mode == 1) {
  612. sub_vol++;
  613. vol_menu = 6;
  614. set_sub();
  615. }
  616. break;
  617.  
  618. case ir_sub_d:
  619. if (speaker_mode == 0 || speaker_mode == 1) {
  620. sub_vol--;
  621. vol_menu = 6;
  622. set_sub();
  623. }
  624. break;
  625.  
  626. //speaker mode -------------------------------------------------//
  627. case ir_sp_mode:
  628. speaker_mode++;
  629. vol_menu = 0;
  630. if (speaker_mode == 1) {
  631. vol_menu_jup = 0;
  632. }
  633. set_speaker_mode();
  634. break;
  635.  
  636. //surround -------------------------------------------------//
  637. case ir_surr_mode:
  638. surr++;
  639. vol_menu = 0;
  640. set_surr();
  641. break;
  642.  
  643. // -------------------------------------------------//
  644. case ir_mix_mode:
  645. mix++;
  646. vol_menu = 0;
  647. set_mix();
  648. break;
  649.  
  650. // -------------------------------------------------//
  651. case ir_reset:
  652. reset++;
  653. vol_menu = 0;
  654. set_reset();
  655. break;
  656. }
  657. ir_cl();
  658. }
  659. irrecv.resume();
  660. }
  661. }
  662.  
  663. //custom shape --------------------------------------------------------------------------------//
  664.  
  665. void custom_num_shape() {
  666. for (int i = 0; i < 8; i++)
  667. lcd.createChar(i, custom_num[i]);
  668. }
  669.  
  670. void custom_shape() {
  671. lcd.createChar(1, arrow_right);
  672. }
  673.  
  674.  
  675. //lcd ---------------------------------------------------------//
  676.  
  677. void lcd_update() {
  678. int c;
  679. switch (menu_active) {
  680. case 0:
  681. //input -------------------------------------------------//
  682. lcd.setCursor(0, 0);
  683. if (in == 0) {
  684. lcd.print("IN1");
  685. }
  686. if (in == 1) {
  687. lcd.print("IN2");
  688. }
  689. if (in == 2) {
  690. lcd.print("IN3");
  691. }
  692. if (in == 3) {
  693. lcd.print("AUX");
  694. }
  695. if (in == 4) {
  696. lcd.print("DVD");
  697. }
  698.  
  699. //speaker mode ------------------------------------------//
  700. lcd.setCursor(4, 0);
  701. if (speaker_mode == 0) {
  702. lcd.print("5.1");
  703. }
  704. if (speaker_mode == 1) {
  705. lcd.print("2.1");
  706. }
  707.  
  708. //vol ----------------------------------------------//
  709. switch (vol_menu) {
  710. case 0:
  711. lcd.setCursor(0, 1);
  712. // (" ");
  713. lcd.print("MAS-VOL");
  714. c = mas_vol - 19;
  715. break;
  716.  
  717. case 1:
  718. lcd.setCursor(0, 1);
  719. // (" ");
  720. lcd.print("FL-VOL ");
  721. c = fl_vol;
  722. break;
  723.  
  724. case 2:
  725. lcd.setCursor(0, 1);
  726. // (" ");
  727. lcd.print("FR-VOL ");
  728. c = fr_vol;
  729. break;
  730.  
  731. case 3:
  732. lcd.setCursor(0, 1);
  733. // (" ");
  734. lcd.print("SL-VOL ");
  735. c = sl_vol;
  736. break;
  737.  
  738. case 4:
  739. lcd.setCursor(0, 1);
  740. // (" ");
  741. lcd.print("SR-VOL ");
  742. c = sr_vol;
  743. break;
  744.  
  745. case 5:
  746. lcd.setCursor(0, 1);
  747. // (" ");
  748. lcd.print("CN-VOL ");
  749. c = cn_vol;
  750. break;
  751.  
  752. case 6:
  753. lcd.setCursor(0, 1);
  754. // (" ");
  755. lcd.print("SUB-VOL");
  756. c = sub_vol;
  757. break;
  758. }
  759. break;
  760.  
  761. case 1:
  762. switch (menu) {
  763. case 1:
  764. lcd.setCursor(0, 0);
  765. lcd.print("Surround");
  766. lcd.setCursor(1, 1);
  767. lcd.print("ON");
  768. lcd.setCursor(6, 1);
  769. lcd.print("OFF");
  770. if (surr == 0) {
  771. lcd.setCursor(0, 1);
  772. lcd.write(1);
  773. lcd.setCursor(5, 1);
  774. lcd.print(" ");
  775. }
  776. if (surr == 1) {
  777. lcd.setCursor(0, 1);
  778. lcd.print(" ");
  779. lcd.setCursor(5, 1);
  780. lcd.write(1);
  781. }
  782. break;
  783.  
  784. case 2:
  785. lcd.setCursor(0, 0);
  786. lcd.print("Speaker Mode");
  787. lcd.setCursor(1, 1);
  788. lcd.print("5.1");
  789. lcd.setCursor(6, 1);
  790. lcd.print("2.1");
  791. if (speaker_mode == 0) {
  792. lcd.setCursor(0, 1);
  793. lcd.write(1);
  794. lcd.setCursor(5, 1);
  795. lcd.print(" ");
  796. }
  797. if (speaker_mode == 1) {
  798. lcd.setCursor(0, 1);
  799. lcd.print(" ");
  800. lcd.setCursor(5, 1);
  801. lcd.write(1);
  802. }
  803. break;
  804.  
  805. case 3:
  806. lcd.setCursor(0, 0);
  807. lcd.print("-6dB");
  808. lcd.setCursor(1, 1);
  809. lcd.print("ON");
  810. lcd.setCursor(6, 1);
  811. lcd.print("OFF");
  812. if (mix == 0) {
  813. lcd.setCursor(0, 1);
  814. lcd.write(1);
  815. lcd.setCursor(5, 1);
  816. lcd.print(" ");
  817. }
  818. if (mix == 1) {
  819. lcd.setCursor(0, 1);
  820. lcd.print(" ");
  821. lcd.setCursor(5, 1);
  822. lcd.write(1);
  823. }
  824. break;
  825.  
  826. case 4:
  827. lcd.setCursor(0, 0);
  828. lcd.print("All Reset");
  829. lcd.setCursor(1, 1);
  830. lcd.print("Reset");
  831. if (reset == 1) {
  832. lcd.setCursor(0, 1);
  833. lcd.write(1);
  834. }
  835.  
  836.  
  837. lcd.setCursor(0, 0);
  838. lcd.print("All Reset");
  839. lcd.setCursor(1, 1);
  840. lcd.print("Custom");
  841. lcd.setCursor(9, 1);
  842. lcd.print("Reset");
  843. if (reset == 0) {
  844. lcd.setCursor(0, 1);
  845. lcd.write(1);
  846. lcd.setCursor(8, 1);
  847. lcd.print(" ");
  848. }
  849. if (reset == 1) {
  850. lcd.setCursor(0, 1);
  851. lcd.print(" ");
  852. lcd.setCursor(8, 1);
  853. lcd.write(1);
  854. }
  855. break;
  856. }
  857. break;
  858.  
  859. case 99:
  860. lcd.setCursor(0, 0);
  861. lcd.print(" ");
  862. lcd.setCursor(0, 1);
  863. lcd.print(" MUTE ");
  864. break;
  865.  
  866. case 100:
  867. lcd.setCursor(0, 0);
  868. lcd.print(" ");
  869. lcd.setCursor(0, 1);
  870. lcd.print(" STANDBY ");
  871. break;
  872. }
  873. if (menu_active == 0) {
  874. int y;
  875. if (c < 0) {
  876. lcd.setCursor(8, 1);
  877. lcd.print("-");
  878. y = 10 - (c + 10);
  879. } else if (c == -10) {
  880. lcd.setCursor(8, 1);
  881. lcd.print("-");
  882. y = 10;
  883. } else {
  884. lcd.setCursor(8, 1);
  885. lcd.print(" ");
  886. y = c;
  887. }
  888. a = y / 10;
  889. b = y - a * 10;
  890.  
  891. lcd.setCursor(9, 0);
  892. for (int i = 0; i < digit_width; i++)
  893. lcd.print(custom_num_top[a][i]);
  894.  
  895. lcd.setCursor(9, 1);
  896. for (int i = 0; i < digit_width; i++)
  897. lcd.print(custom_num_bot[a][i]);
  898.  
  899. lcd.setCursor(13, 0);
  900. for (int i = 0; i < digit_width; i++)
  901. lcd.print(custom_num_top[b][i]);
  902.  
  903. lcd.setCursor(13, 1);
  904. for (int i = 0; i < digit_width; i++)
  905. lcd.print(custom_num_bot[b][i]);
  906. }
  907.  
  908. }
  909.  
  910. //all reset --------------------------------------------------------------------------------//
  911.  
  912. void set_reset() {
  913. if (reset == 1) {
  914. in = 0;
  915. mas_vol = 44;
  916. fl_vol = 0;
  917. fr_vol = 0;
  918. sl_vol = 0;
  919. sr_vol = 0;
  920. cn_vol = 0;
  921. sub_vol = 0;
  922. speaker_mode = 0;
  923. surr = 0;
  924. mix = 0;
  925. vol_menu = 0;
  926. menu_active = 0;
  927. reset = 0;
  928. lcd.clear();
  929. }
  930. set_in();
  931. set_fl();
  932. set_fr();
  933. set_sl();
  934. set_sr();
  935. set_cn();
  936. set_sub();
  937. set_speaker_mode();
  938. set_surr();
  939. set_mix();
  940. }
  941.  
  942. //speaker mode --------------------------------------------------------------------------------//
  943.  
  944. void set_speaker_mode() {
  945. if (speaker_mode > 1) {
  946. speaker_mode = 0;
  947. }
  948. if (speaker_mode < 0) {
  949. speaker_mode = 1;
  950. }
  951. switch (speaker_mode) {
  952. case 0: // 5.1 mode
  953. ch_mute = 0;
  954. break;
  955. case 1: // 2.1 mode
  956. ch_mute = 1;
  957. break;
  958. }
  959. set_sl();
  960. set_sr();
  961. set_cn();
  962. }
  963.  
  964. //AX2358 settings -----------------------------------------------------//
  965.  
  966. void set_in() {
  967. if (in > 4) {
  968. in = 0;
  969. }
  970. switch (in) {
  971. case 0: a = 0b11001011; break; // 1 input
  972. case 1: a = 0b11001010; break; // 2 input
  973. case 2: a = 0b11001001; break; // 3 input
  974. case 3: a = 0b11001000; break; // 4 input
  975. case 4: a = 0b11001111; break; // 6 CH input
  976. }
  977. AX2358_send(a);
  978. }
  979. void set_surr() {
  980. if (surr > 1) {
  981. surr = 0;
  982. }
  983. if (surr < 0) {
  984. surr = 1;
  985. }
  986. switch (surr) {
  987. case 0: a = 0b11000000; break; // Surround ON
  988. case 1: a = 0b11000001; break; // Surround OFF
  989. }
  990. AX2358_send(a);
  991. }
  992. void set_mix() {
  993. if (mix > 1) {
  994. mix = 0;
  995. }
  996. switch (mix) {
  997. case 0: a = 0b11000010; break; // (-6dB) on
  998. case 1: a = 0b11000011; break; // (-6dB) off
  999. }
  1000. AX2358_send(a);
  1001. }
  1002.  
  1003. //AX2358 Volume settings ----------------------------------------------//
  1004.  
  1005. void set_mas_vol() {
  1006. if (mas_vol > 69) {
  1007. mas_vol = 69;
  1008. }
  1009. if (mas_vol < 19) {
  1010. mas_vol = 19;
  1011. }
  1012. if (mas_vol == 19) {
  1013. mute = 1;
  1014. } else {
  1015. mute = 0;
  1016. }
  1017. set_mute();
  1018. }
  1019. void set_mute() {
  1020. if (mute > 1) {
  1021. mute = 0;
  1022. }
  1023. switch (mute) {
  1024. case 0: // 5.1 mode
  1025. ch_mute = 0;
  1026. break;
  1027. case 1: // 2.1 mode
  1028. ch_mute = 1;
  1029. break;
  1030. }
  1031. set_fl();
  1032. set_fr();
  1033. set_sub();
  1034. if (speaker_mode == 0) {
  1035. set_sl();
  1036. set_sr();
  1037. set_cn();
  1038. }
  1039. }
  1040. void set_fl() {
  1041. if (fl_vol > 10) {
  1042. fl_vol = 10;
  1043. }
  1044. if (fl_vol < -10) {
  1045. fl_vol = -10;
  1046. }
  1047. fl = mas_vol + fl_vol;
  1048. int c = 79 - fl;
  1049. a = c / 10;
  1050. b = c - a * 10;
  1051. AX2358_vol(0b10000000 + a, 0b10010000 + b); // CH1
  1052.  
  1053. switch (ch_mute) {
  1054. case 0: x = 0b11110000; break; // Mute disabled
  1055. case 1: x = 0b11110001; break; // Mute
  1056. }
  1057. AX2358_send(x);
  1058. }
  1059. void set_fr() {
  1060. if (fr_vol > 10) {
  1061. fr_vol = 10;
  1062. }
  1063. if (fr_vol < -10) {
  1064. fr_vol = -10;
  1065. }
  1066. fr = mas_vol + fr_vol;
  1067. int c = 79 - fr;
  1068. a = c / 10;
  1069. b = c - a * 10;
  1070. AX2358_vol(0b01000000 + a, 0b01010000 + b); // CH2
  1071.  
  1072. switch (ch_mute) {
  1073. case 0: x = 0b11110010; break; // Mute disabled
  1074. case 1: x = 0b11110011; break; // Mute
  1075. }
  1076. AX2358_send(x);
  1077. }
  1078. void set_cn() {
  1079. if (cn_vol > 10) {
  1080. cn_vol = 10;
  1081. }
  1082. if (cn_vol < -10) {
  1083. cn_vol = -10;
  1084. }
  1085. cn = mas_vol + cn_vol;
  1086. int c = 79 - cn;
  1087. a = c / 10;
  1088. b = c - a * 10;
  1089. AX2358_vol(0b00000000 + a, 0b00010000 + b); // CH3
  1090.  
  1091. switch (ch_mute) {
  1092. case 0: x = 0b11110100; break; // Mute disabled
  1093. case 1: x = 0b11110101; break; // Mute
  1094. }
  1095. AX2358_send(x);
  1096. }
  1097. void set_sub() {
  1098. if (sub_vol > 10) {
  1099. sub_vol = 10;
  1100. }
  1101. if (sub_vol < -10) {
  1102. sub_vol = -10;
  1103. }
  1104. sub = mas_vol + sub_vol;
  1105. int c = 79 - sub;
  1106. a = c / 10;
  1107. b = c - a * 10;
  1108. AX2358_vol(0b00100000 + a, 0b00110000 + b); // CH4
  1109.  
  1110. switch (ch_mute) {
  1111. case 0: x = 0b11110110; break; // Mute disabled
  1112. case 1: x = 0b11110111; break; // Mute
  1113. }
  1114. AX2358_send(x);
  1115. }
  1116. void set_sl() {
  1117. if (sl_vol > 10) {
  1118. sl_vol = 10;
  1119. }
  1120. if (sl_vol < -10) {
  1121. sl_vol = -10;
  1122. }
  1123. sl = mas_vol + sl_vol;
  1124. int c = 79 - sl;
  1125. a = c / 10;
  1126. b = c - a * 10;
  1127. AX2358_vol(0b01100000 + a, 0b01110000 + b); // CH5
  1128.  
  1129. switch (ch_mute) {
  1130. case 0: x = 0b11111000; break; // Mute disabled
  1131. case 1: x = 0b11111001; break; // Mute
  1132. }
  1133. AX2358_send(x);
  1134. }
  1135. void set_sr() {
  1136. if (sr_vol > 10) {
  1137. sr_vol = 10;
  1138. }
  1139. if (sr_vol < -10) {
  1140. sr_vol = -10;
  1141. }
  1142. sr = mas_vol + sr_vol;
  1143. int c = 79 - sr;
  1144. a = c / 10;
  1145. b = c - a * 10;
  1146. AX2358_vol(0b10100000 + a, 0b10110000 + b); // CH6
  1147.  
  1148. switch (ch_mute) {
  1149. case 0: x = 0b11111010; break; // Mute disabled
  1150. case 1: x = 0b11111011; break; // Mute
  1151. }
  1152. AX2358_send(x);
  1153. }
  1154.  
  1155. //AX2358 send -----------------------------------------------------//
  1156.  
  1157. void AX2358_send(char c) {
  1158. Wire.beginTransmission(AX2358_address);
  1159. Wire.write (c);
  1160. Wire.endTransmission();
  1161. }
  1162. void AX2358() {
  1163. Wire.beginTransmission(AX2358_address);
  1164. Wire.write (0b11000100);
  1165. Wire.endTransmission();
  1166. }
  1167. void AX2358_vol(char c, char d) {
  1168. Wire.beginTransmission(AX2358_address);
  1169. Wire.write (c);
  1170. Wire.write (d);
  1171. Wire.endTransmission();
  1172. }
  1173.  
  1174. //end code
Success #stdin #stdout 0.03s 25824KB
stdin
Standard input is empty
stdout
// AX2358 5.1 Surround System
// DaacWaves <https://d...content-available-to-author-only...t.com>

#include <Wire.h>
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
#include <IRremote.h>

#define AX2358_address 0b1001010

#define btn_delay 300

#define sw01 9          // SW
#define sw02 11         // DT
#define sw03 10         // CLK
#define sw04 A0         // Input
#define sw05 A1         // Mute
#define sw06 A2         // Power

#define sw_power 13     // Out

// IR HEX code
#define ir_power      0x807F827D    // IR power ON/OFF
#define ir_mute       0x609FFF80    // IR mute
#define ir_in_0       0x807F629D    // IR input USB
#define ir_in_1       0x807F52AD    // IR input BLU
#define ir_in_2       0x807FA25D    // IR input FM
#define ir_in_3       0x807F22DD    // IR input AUX
#define ir_in_4       0x807F20DF    // IR input DVD
#define ir_vol_i      0x807F906F    // IR vol++
#define ir_vol_d      0x807FA05F    // IR vol-- 
#define ir_fl_i       0x807F40BF    // IR fl++
#define ir_fl_d       0x807FC03F    // IR fl-- 
#define ir_fr_i       0x807F00FF    // IR fr++
#define ir_fr_d       0x807F807F    // IR fr--
#define ir_sl_i       0x807F48B7    // IR sl++
#define ir_sl_d       0x807FC837    // IR sl--
#define ir_sr_i       0x807F08F7    // IR sr++
#define ir_sr_d       0x807F8877    // IR sr--
#define ir_cn_i       0x807F50AF    // IR cn++
#define ir_cn_d       0x807F609F    // IR cn--
#define ir_sub_i      0x807FD02F    // IR sub++
#define ir_sub_d      0x807FE01F    // IR sub-- 
#define ir_sp_mode    0x807F0AF5    // IR speaker mode change
#define ir_surr_mode  0x807FA857    // IR surround ON/OFF
#define ir_mix_mode   0x00000000    // IR -6dB ON/OFF
#define ir_reset      0x807F1AE5    // IR reset

IRrecv irrecv(8);
decode_results results;

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};

byte arrow_right[8] = {B00000, B10000, B11000, B11100, B11110, B11100, B11000, B10000};

LiquidCrystal_I2C lcd(0x27, 16, 2); 

unsigned long time;
int in, mute, return_d, surr, mix, a, b, x, power, menu, menu_active, ch_mute, speaker_mode, btn_press, long_press, vol_menu, vol_menu_jup, reset;
int fl, fr, sl, sr, cn, sub, ir_menu, ir_on, mas_vol, fl_vol, fr_vol, sl_vol, sr_vol, cn_vol, sub_vol;

long btn_timer = 0;
long long_press_time = 600;

void setup() {
  Wire.begin();
  Serial.begin(9600);
  irrecv.enableIRIn();

  pinMode(sw01, INPUT);      // SW
  pinMode(sw02, INPUT);      // DT
  pinMode(sw03, INPUT);      // CLK
  pinMode(sw04, INPUT);      // Input
  pinMode(sw05, INPUT);      // Mute
  pinMode(sw06, INPUT);      // Power
  pinMode(sw_power, OUTPUT); // Out

  digitalWrite(sw_power, LOW);

  lcd.begin(16, 2);
  
  power = 0;
  eeprom_read();
  start_up();
  power_up();

}

void loop() {
  lcd_update();
  eeprom_update();
  ir_control();
  return_delay();

  if (menu_active == 0) {
    custom_num_shape();
  } else {
    custom_shape();
  }

  //power -------------------------------------------------//
  if (analogRead(sw06) > 900) {
    power++;
    if (power > 1) {
      power = 0;
    }
    power_up();
    delay(btn_delay);
  }

  if (power == 1) {
    //select input -------------------------------------------------//
    if (analogRead(sw04) > 900) {
      in++;
      set_in();
      delay(btn_delay);
    }

    //select menu -------------------------------------------------//
    if (digitalRead(sw01) == LOW) {
      if (btn_press == 0) {
        btn_press = 1;
        btn_timer = millis();
      }
      if ((millis() - btn_timer > long_press_time) && (long_press == 0) && (menu_active == 0)) {
        long_press = 1;
        menu_active = 1;
        menu = 1;
        btn_cl();
        lcd.clear();
      } else if ((millis() - btn_timer > long_press_time) && (long_press == 0) && (menu_active == 1)) {
        long_press = 1;
        menu_active = 0;
        vol_menu = 0;
        reset = 0;
        btn_cl();
        lcd.clear();
      }
    } else {
      if (btn_press == 1) {
        if (long_press == 1) {
          long_press = 0;
        } else {
          if (menu_active == 1) {
            menu++;
            if (menu > 4) {
              menu = 1;
            }
            btn_cl();
            lcd.clear();
          } else if (menu_active == 0 && speaker_mode == 0) {
            vol_menu++;
            if (vol_menu > 6) {
              vol_menu = 0;
            }
            btn_cl();
          } else if (menu_active == 0 && speaker_mode == 1) {
            vol_menu++;
            if (vol_menu_jup == 0) {
              if (vol_menu > 2) {
                vol_menu = 6;
                vol_menu_jup = 1;
              }
            }
            if (vol_menu_jup == 1) {
              if (vol_menu > 6) {
                vol_menu = 0;
                vol_menu_jup = 0;
              }
            }
            btn_cl();
          }
        }
        btn_press = 0;
      }
    }

    //mute -------------------------------------------------//
    if (analogRead(sw05) > 900 && mas_vol != 19) {
      mute++;
      if (mute == 1) {
        menu_active = 99;
      } else {
        menu_active = 0;
      }
      set_mute();
      delay(btn_delay);
      lcd.clear();
    }
  }

  //menu active 0 -------------------------------------------------//
  if (menu_active == 0) {
    if (digitalRead(sw02) == LOW) {
      if (vol_menu == 0) {
        mas_vol++;
      }
      if (vol_menu == 1) {
        fl_vol++;
      }
      if (vol_menu == 2) {
        fr_vol++;
      }
      if (vol_menu == 3) {
        sl_vol++;
      }
      if (vol_menu == 4) {
        sr_vol++;
      }
      if (vol_menu == 5) {
        cn_vol++;
      }
      if (vol_menu == 6) {
        sub_vol++;
      }
      set_mas_vol();
      set_fl();
      set_fr();
      set_sub();
      if (speaker_mode == 0) {
        set_sl();
        set_sr();
        set_cn();
      }
      btn_cl();
    }
    if (digitalRead(sw03) == LOW) {
      if (vol_menu == 0) {
        mas_vol--;
      }
      if (vol_menu == 1) {
        fl_vol--;
      }
      if (vol_menu == 2) {
        fr_vol--;
      }
      if (vol_menu == 3) {
        sl_vol--;
      }
      if (vol_menu == 4) {
        sr_vol--;
      }
      if (vol_menu == 5) {
        cn_vol--;
      }
      if (vol_menu == 6) {
        sub_vol--;
      }
      set_mas_vol();
      set_fl();
      set_fr();
      set_sub();
      if (speaker_mode == 0) {
        set_sl();
        set_sr();
        set_cn();
      }
      btn_cl();
    }
  }

  //menu active 1 -------------------------------------------------//
  if (menu_active == 1) {
    if (menu == 1) {
      if (digitalRead(sw02) == LOW) {
        surr++;
        set_surr();
        btn_cl();
      }
    }
    if (menu == 2) {
      if (digitalRead(sw02) == LOW) {
        speaker_mode++;
        if (speaker_mode == 1) {
          vol_menu_jup = 0;
        }
        set_speaker_mode();
        btn_cl();
      }
    }
    if (menu == 3) {
      if (digitalRead(sw02) == LOW) {
        mix++;
        set_mix();
        btn_cl();
      }
    }
    if (menu == 4) {
      if (digitalRead(sw02) == LOW) {
        reset++;
        set_reset();
        btn_cl();
      }
    }
  }
}

//eeprom -----------------------------------------------------//

void eeprom_update() {
  EEPROM.update(0, in);
  EEPROM.update(1, mas_vol);
  EEPROM.update(2, fl_vol + 10);
  EEPROM.update(3, fr_vol + 10);
  EEPROM.update(4, sl_vol + 10);
  EEPROM.update(5, sr_vol + 10);
  EEPROM.update(6, cn_vol + 10);
  EEPROM.update(7, sub_vol + 10);
  EEPROM.update(8, surr);
  EEPROM.update(9, speaker_mode);
  EEPROM.update(10, mix);
}

void eeprom_read() {
  in = EEPROM.read(0);
  mas_vol = EEPROM.read(1);
  fl_vol = EEPROM.read(2) - 10;
  fr_vol = EEPROM.read(3) - 10;
  sl_vol = EEPROM.read(4) - 10;
  sr_vol = EEPROM.read(5) - 10;
  cn_vol = EEPROM.read(6) - 10;
  sub_vol = EEPROM.read(7) - 10;
  surr = EEPROM.read(8);
  speaker_mode = EEPROM.read(9);
  mix = EEPROM.read(10);
}

void btn_cl() {
  delay(btn_delay);
  time = millis();
  return_d = 1;
}
void ir_cl() {
  time = millis();
  return_d = 1;
}
void return_delay() {
  if (millis() - time > 5000 && return_d == 1 && mute == 0 && menu_active != 0) {
    menu_active = 0;
    vol_menu = 0;
    reset = 0;
    return_d = 0;
    lcd.clear();
  } else if (millis() - time > 5000 && return_d == 1 && mute == 0 && menu_active == 0) {
    vol_menu = 0;
    return_d = 0;
  }
}

//power up -----------------------------------------------------//

void power_up() {
  if (power == 1) {
    lcd.clear();
    delay(500);
    lcd.setCursor(0, 1);
    lcd.print("   LOADING...   ");
    delay(1000);
    lcd.clear();
    if (mas_vol > 19) {
      mute = 0;
    }
    set_mute();
    vol_menu = 0;
    menu_active = 0;
    delay(300);
    ir_on = 1;
    vol_menu_jup = 0;
    digitalWrite(sw_power, HIGH);

  } else {

    (sw_power, LOW);
    mute = 1;
    set_mute();
    delay(100);
    menu_active = 100;
    ir_on = 0;
  }
}

void start_up() {
  mute = 1;
  set_mute();
  delay(500);
  lcd.setCursor(0, 0);
  lcd.print("    Ui Tech     ");
  delay(500);
  lcd.setCursor(0, 1);
  lcd.print("   5.1 SYSTEM   ");
  delay(1000);
  lcd.clear();
  delay(300);
  lcd.setCursor(0, 1);
  lcd.print("   LOADING...   ");
  delay(1500);
  lcd.clear();
  delay(300);
  AX2358();
  set_in();
  set_surr();
  set_mix();
  set_fl();
  set_fr();
  set_sl();
  set_sr();
  set_cn();
  set_sub();
}

//IR control --------------------------------------------------------------------------------//

void ir_control() {
  if ( irrecv.decode( &results )) {

    switch (results.value) {
      //power -------------------------------------------------//
      case ir_power:
        power++;
        if (power > 1) {
          power = 0;
        }
        power_up();
        break;
    }
    if (ir_on == 1) {
      switch (results.value) {
        //mute -------------------------------------------------//
        case ir_mute:
          if (mas_vol != 19) {
            mute++;
            if (mute == 1) {
              menu_active = 99;
            } else {
              menu_active = 0;
            }
            set_mute();
            lcd.clear();  
          }
          
          break;

        //select input -------------------------------------------------//
        case ir_in_0:
          in = 0;
          set_in();
          ir_cl();
          break;

        case ir_in_1:
          in = 1;
          set_in();
          ir_cl();
          break;

        case ir_in_2:
          in = 2;
          set_in();
          ir_cl();
          break;
          
        case ir_in_3:
          in = 3;
          set_in();
          ir_cl();
          break;

        case ir_in_4:
          in = 4;
          set_in();
          ir_cl();
          break;
      }
    }

    if (ir_on == 1 && menu_active == 0) {
      switch (results.value) {
        //VOL -------------------------------------------------//
        case ir_vol_i:
          if (speaker_mode == 0 || speaker_mode == 1) {
            mas_vol++;
            vol_menu = 0;
            set_mas_vol();
            set_fl();
            set_fr();
            set_sub();
            if (speaker_mode == 0) {
              set_sl();
              set_sr();
              set_cn();
            }
          }
          break;

        case ir_vol_d:
          if (speaker_mode == 0 || speaker_mode == 1) {
            mas_vol--;
            vol_menu = 0;
            set_mas_vol();
            set_fl();
            set_fr();
            set_sub();
            if (speaker_mode == 0) {
              set_sl();
              set_sr();
              set_cn();
            }
          }
          break;

        //FL -------------------------------------------------//
        case ir_fl_i:
          if (speaker_mode == 0 || speaker_mode == 1) {
            fl_vol++;
            vol_menu = 1;
            set_fl();
          }
          break;

        case ir_fl_d:
          if (speaker_mode == 0 || speaker_mode == 1) {
            fl_vol--;
            vol_menu = 1;
            set_fl();
          }
          break;

        //FR -------------------------------------------------//
        case ir_fr_i:
          if (speaker_mode == 0 || speaker_mode == 1) {
            fr_vol++;
            vol_menu = 2;
            set_fr();
          }
          break;

        case ir_fr_d:
          if (speaker_mode == 0 || speaker_mode == 1) {
            fr_vol--;
            vol_menu = 2;
            set_fr();
          }
          break;

        //SL -------------------------------------------------//
        case ir_sl_i:
          if (speaker_mode == 0) {
            sl_vol++;
            vol_menu = 3;
            set_sl();
          }
          break;

        case ir_sl_d:
          if (speaker_mode == 0) {
            sl_vol--;
            vol_menu = 3;
            set_sl();
          }
          break;

        //SR -------------------------------------------------//
        case ir_sr_i:
          if (speaker_mode == 0) {
            sr_vol++;
            vol_menu = 4;
            set_sr();
          }
          break;

        case ir_sr_d:
          if (speaker_mode == 0) {
            sr_vol--;
            vol_menu = 4;
            set_sr();
          }
          break;

        //CN -------------------------------------------------//
        case ir_cn_i:
          if (speaker_mode == 0) {
            cn_vol++;
            vol_menu = 5;
            set_cn();
          }
          break;

        case ir_cn_d:
          if (speaker_mode == 0) {
            cn_vol--;
            vol_menu = 5;
            set_cn();
          }
          break;

        //SUB -------------------------------------------------//
        case ir_sub_i:
          if (speaker_mode == 0 || speaker_mode == 1) {
            sub_vol++;
            vol_menu = 6;
            set_sub();
          }
          break;

        case ir_sub_d:
          if (speaker_mode == 0 || speaker_mode == 1) {
            sub_vol--;
            vol_menu = 6;
            set_sub();
          }
          break;

        //speaker mode -------------------------------------------------//
        case ir_sp_mode:
          speaker_mode++;
          vol_menu = 0;
          if (speaker_mode == 1) {
            vol_menu_jup = 0;
          }
          set_speaker_mode();
          break;

        //surround -------------------------------------------------//
        case ir_surr_mode:
          surr++;
          vol_menu = 0;
          set_surr();
          break;

        // -------------------------------------------------//
        case ir_mix_mode:
          mix++;
          vol_menu = 0;
          set_mix();
          break;

        // -------------------------------------------------//
        case ir_reset:
          reset++;
          vol_menu = 0;
          set_reset();
          break;
      }
      ir_cl();
    }
    irrecv.resume();
  }
}

//custom shape --------------------------------------------------------------------------------//

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

void custom_shape() {
  lcd.createChar(1, arrow_right);
}


//lcd ---------------------------------------------------------//

void lcd_update() {
  int c;
  switch (menu_active) {
    case 0:
      //input -------------------------------------------------//
      lcd.setCursor(0, 0);
      if (in == 0) {
        lcd.print("IN1");
      }
      if (in == 1) {
        lcd.print("IN2");
      }
      if (in == 2) {
        lcd.print("IN3");
      }
      if (in == 3) {
        lcd.print("AUX");
      }
      if (in == 4) {
        lcd.print("DVD");
      }

      //speaker mode ------------------------------------------//
      lcd.setCursor(4, 0);
      if (speaker_mode == 0) {
        lcd.print("5.1");
      }
      if (speaker_mode == 1) {
        lcd.print("2.1");
      }

      //vol ----------------------------------------------//
      switch (vol_menu) {
        case 0:
          lcd.setCursor(0, 1);
          //       ("       ");
          lcd.print("MAS-VOL");
          c = mas_vol - 19;
          break;

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

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

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

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

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

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

    case 1:
      switch (menu) {
        case 1:
          lcd.setCursor(0, 0);
          lcd.print("Surround");
          lcd.setCursor(1, 1);
          lcd.print("ON");
          lcd.setCursor(6, 1);
          lcd.print("OFF");
          if (surr == 0) {
            lcd.setCursor(0, 1);
            lcd.write(1);
            lcd.setCursor(5, 1);
            lcd.print(" ");
          }
          if (surr == 1) {
            lcd.setCursor(0, 1);
            lcd.print(" ");
            lcd.setCursor(5, 1);
            lcd.write(1);
          }
          break;

        case 2:
          lcd.setCursor(0, 0);
          lcd.print("Speaker Mode");
          lcd.setCursor(1, 1);
          lcd.print("5.1");
          lcd.setCursor(6, 1);
          lcd.print("2.1");
          if (speaker_mode == 0) {
            lcd.setCursor(0, 1);
            lcd.write(1);
            lcd.setCursor(5, 1);
            lcd.print(" ");
          }
          if (speaker_mode == 1) {
            lcd.setCursor(0, 1);
            lcd.print(" ");
            lcd.setCursor(5, 1);
            lcd.write(1);
          }
          break;

        case 3:
          lcd.setCursor(0, 0);
          lcd.print("-6dB");
          lcd.setCursor(1, 1);
          lcd.print("ON");
          lcd.setCursor(6, 1);
          lcd.print("OFF");
          if (mix == 0) {
            lcd.setCursor(0, 1);
            lcd.write(1);
            lcd.setCursor(5, 1);
            lcd.print(" ");
          }
          if (mix == 1) {
            lcd.setCursor(0, 1);
            lcd.print(" ");
            lcd.setCursor(5, 1);
            lcd.write(1);
          }
          break;

        case 4:
          lcd.setCursor(0, 0);
          lcd.print("All Reset");
          lcd.setCursor(1, 1);
          lcd.print("Reset");
          if (reset == 1) {
            lcd.setCursor(0, 1);
            lcd.write(1);
          }


          lcd.setCursor(0, 0);
          lcd.print("All Reset");
          lcd.setCursor(1, 1);
          lcd.print("Custom");
          lcd.setCursor(9, 1);
          lcd.print("Reset");
          if (reset == 0) {
            lcd.setCursor(0, 1);
            lcd.write(1);
            lcd.setCursor(8, 1);
            lcd.print(" ");
          }
          if (reset == 1) {
            lcd.setCursor(0, 1);
            lcd.print(" ");
            lcd.setCursor(8, 1);
            lcd.write(1);
          }
          break;
      }
      break;

    case 99:
      lcd.setCursor(0, 0);
      lcd.print("                ");
      lcd.setCursor(0, 1);
      lcd.print("      MUTE      ");
      break;

    case 100:
      lcd.setCursor(0, 0);
      lcd.print("                ");
      lcd.setCursor(0, 1);
      lcd.print("    STANDBY     ");
      break;
  }
  if (menu_active == 0) {
    int y;
    if (c < 0) {
      lcd.setCursor(8, 1);
      lcd.print("-");
      y = 10 - (c + 10);
    } else if (c == -10) {
      lcd.setCursor(8, 1);
      lcd.print("-");
      y = 10;
    } 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]);
  }

}

//all reset --------------------------------------------------------------------------------//

void set_reset() {
  if (reset == 1) {
    in = 0;
    mas_vol = 44;
    fl_vol = 0;
    fr_vol = 0;
    sl_vol = 0;
    sr_vol = 0;
    cn_vol = 0;
    sub_vol = 0;
    speaker_mode = 0;
    surr = 0;
    mix = 0;
    vol_menu = 0;
    menu_active = 0;
    reset = 0;
    lcd.clear();
  }
  set_in();
  set_fl();
  set_fr();
  set_sl();
  set_sr();
  set_cn();
  set_sub();
  set_speaker_mode();
  set_surr();
  set_mix();
}

//speaker mode --------------------------------------------------------------------------------//

void set_speaker_mode() {
  if (speaker_mode > 1) {
    speaker_mode = 0;
  }
  if (speaker_mode < 0) {
    speaker_mode = 1;
  }
  switch (speaker_mode) {
    case 0:                     // 5.1 mode
      ch_mute = 0;
      break;
    case 1:                     // 2.1 mode
      ch_mute = 1;
      break;
  }
  set_sl();
  set_sr();
  set_cn();
}

//AX2358 settings -----------------------------------------------------//

void set_in() {
  if (in > 4) {
    in = 0;
  }
  switch (in) {
    case 0: a = 0b11001011; break; // 1 input
    case 1: a = 0b11001010; break; // 2 input
    case 2: a = 0b11001001; break; // 3 input
    case 3: a = 0b11001000; break; // 4 input
    case 4: a = 0b11001111; break; // 6 CH input
  }
  AX2358_send(a);
}
void set_surr() {
  if (surr > 1) {
    surr = 0;
  }
  if (surr < 0) {
    surr = 1;
  }
  switch (surr) {
    case 0: a = 0b11000000; break; // Surround ON
    case 1: a = 0b11000001; break; // Surround OFF
  }
  AX2358_send(a);
}
void set_mix() {
  if (mix > 1) {
    mix = 0;
  }
  switch (mix) {
    case 0: a = 0b11000010; break; // (-6dB) on
    case 1: a = 0b11000011; break; // (-6dB) off
  }
  AX2358_send(a);
}

//AX2358 Volume settings ----------------------------------------------//

void set_mas_vol() {
  if (mas_vol > 69) {
    mas_vol = 69;
  }
  if (mas_vol < 19) {
    mas_vol = 19;
  }
  if (mas_vol == 19) {
    mute = 1;
  } else {
    mute = 0;
  }
  set_mute();
}
void set_mute() {
  if (mute > 1) {
    mute = 0;
  }
  switch (mute) {
    case 0:                     // 5.1 mode
      ch_mute = 0;
      break;
    case 1:                     // 2.1 mode
      ch_mute = 1;
      break;
  }
  set_fl();
  set_fr();
  set_sub();
  if (speaker_mode == 0) {
    set_sl();
    set_sr();
    set_cn();
  }
}
void set_fl() {
  if (fl_vol > 10) {
    fl_vol = 10;
  }
  if (fl_vol < -10) {
    fl_vol = -10;
  }
  fl = mas_vol + fl_vol;
  int c = 79 - fl;
  a = c / 10;
  b = c - a * 10;
  AX2358_vol(0b10000000 + a, 0b10010000 + b);  // CH1
  
  switch (ch_mute) {
    case 0: x = 0b11110000; break; // Mute disabled
    case 1: x = 0b11110001; break; // Mute
  }
  AX2358_send(x);
}
void set_fr() {
  if (fr_vol > 10) {
    fr_vol = 10;
  }
  if (fr_vol < -10) {
    fr_vol = -10;
  }
  fr = mas_vol + fr_vol;
  int c = 79 - fr;
  a = c / 10;
  b = c - a * 10;
  AX2358_vol(0b01000000 + a, 0b01010000 + b);  // CH2

  switch (ch_mute) {
    case 0: x = 0b11110010; break; // Mute disabled
    case 1: x = 0b11110011; break; // Mute
  }
  AX2358_send(x);
}
void set_cn() {
  if (cn_vol > 10) {
    cn_vol = 10;
  }
  if (cn_vol < -10) {
    cn_vol = -10;
  }
  cn = mas_vol + cn_vol;
  int c = 79 - cn;
  a = c / 10;
  b = c - a * 10;
  AX2358_vol(0b00000000 + a, 0b00010000 + b);  // CH3

  switch (ch_mute) {
    case 0: x = 0b11110100; break; // Mute disabled
    case 1: x = 0b11110101; break; // Mute
  }
  AX2358_send(x);
}
void set_sub() {
  if (sub_vol > 10) {
    sub_vol = 10;
  }
  if (sub_vol < -10) {
    sub_vol = -10;
  }
  sub = mas_vol + sub_vol;
  int c = 79 - sub;
  a = c / 10;
  b = c - a * 10;
  AX2358_vol(0b00100000 + a, 0b00110000 + b);  // CH4

  switch (ch_mute) {
    case 0: x = 0b11110110; break; // Mute disabled
    case 1: x = 0b11110111; break; // Mute
  }
  AX2358_send(x);
}
void set_sl() {
  if (sl_vol > 10) {
    sl_vol = 10;
  }
  if (sl_vol < -10) {
    sl_vol = -10;
  }
  sl = mas_vol + sl_vol;
  int c = 79 - sl;
  a = c / 10;
  b = c - a * 10;
  AX2358_vol(0b01100000 + a, 0b01110000 + b);  // CH5

  switch (ch_mute) {
    case 0: x = 0b11111000; break; // Mute disabled
    case 1: x = 0b11111001; break; // Mute
  }
  AX2358_send(x);
}
void set_sr() {
  if (sr_vol > 10) {
    sr_vol = 10;
  }
  if (sr_vol < -10) {
    sr_vol = -10;
  }
  sr = mas_vol + sr_vol;
  int c = 79 - sr;
  a = c / 10;
  b = c - a * 10; 
  AX2358_vol(0b10100000 + a, 0b10110000 + b);  // CH6

  switch (ch_mute) {
    case 0: x = 0b11111010; break; // Mute disabled
    case 1: x = 0b11111011; break; // Mute
  }
  AX2358_send(x);
}

//AX2358 send -----------------------------------------------------//

void AX2358_send(char c) {
  Wire.beginTransmission(AX2358_address);
  Wire.write (c);
  Wire.endTransmission();
}
void AX2358() {
  Wire.beginTransmission(AX2358_address);
  Wire.write (0b11000100);
  Wire.endTransmission();
}
void AX2358_vol(char c, char d) {
  Wire.beginTransmission(AX2358_address);
  Wire.write (c);
  Wire.write (d);
  Wire.endTransmission();
}

//end code