#include <Wire.h>
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
#define PT2258_address 0b1000100
#define btMute 4
const int potPin1 = A0; //INPUT1 (CH1 & CH2)
const int potPin2 = A1; //INPUT2 (CH5 & CH6)
// digitCharacter.................................................................
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};
// muteCharacter.................................................................
byte mute1[] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B00111,
B00111,
B00111
};
byte mute2[] = {
B00111,
B00111,
B00111,
B00000,
B00000,
B00000,
B00000,
B00000
};
byte mute3[] = {
B00000,
B00011,
B00011,
B01111,
B11111,
B11111,
B11111,
B11111
};
byte mute4[] = {
B11111,
B11111,
B11111,
B11111,
B01111,
B00011,
B00011,
B00000
};
byte mutex1[] = {
B00000,
B00000,
B00000,
B00000,
B01000,
B00100,
B00010,
B00001
};
byte mutex2[] = {
B10000,
B01000,
B00100,
B00010,
B00000,
B00000,
B00000,
B00000
};
byte mutex3[] = {
B00000,
B00000,
B00000,
B00000,
B00010,
B00100,
B01000,
B10000
};
byte mutex4[] = {
B00001,
B00010,
B00100,
B01000,
B00000,
B00000,
B00000,
B00000
};
int a1,b1,c1,a2,b2,c2, menu_active, mute, pt2258_mute, power, save_eeprom;
int volCH1, volCH2, setVolCH1, setVolCH2;
unsigned long time;
LiquidCrystal_I2C lcd(0x27, 16, 2); //(Address, Total Col, Total Row)
void setup() {
Wire.begin();
//lcd.begin();
lcd.init();
lcd.backlight();
pinMode(btMute, INPUT_PULLUP);
//pinMode(potPin1, INPUT); //Optional
//pinMode(potPin2, INPUT); //Optional
power=1;
eeprom_read();
start_up();
}
void loop() {
eeprom_update();
potVol_ch1();
potVol_ch2();
if (digitalRead(btMute) == LOW)
{
if(power==1)
{
mute++;
if (mute > 1) {
mute = 0;
}
set_mute();
delay(300);
}
}
// saveEEPROM after 10 second
if(millis()-time>10000 && save_eeprom==1)
{
eeprom_update();
save_eeprom=0;
}
}
// eeprom..................................................................
void eeprom_update() {
EEPROM.update(0, setVolCH1);
EEPROM.update(1, setVolCH2);
}
void eeprom_read() {
setVolCH1 = EEPROM.read(0);
setVolCH2 = EEPROM.read(1);
}
// setMute..................................................................
void set_mute() {
if(mute==1)
{
menu_active=1;
pt2258_mute=1;
setPt2258_mute();
lcd.clear();
delay(300);
lcd.setCursor(0, 1);
lcd.print("MUTE ");
lcd.createChar(0, mute1);
lcd.createChar(1, mute2);
lcd.createChar(2, mute3);
lcd.createChar(3, mute4);
lcd.createChar(4, mutex1);
lcd.createChar(5, mutex2);
lcd.createChar(6, mutex3);
lcd.createChar(7, mutex4);
lcd.setCursor(12, 0);
lcd.write(0);
lcd.setCursor(12, 1);
lcd.write(1);
lcd.setCursor(13, 0);
lcd.write(2);
lcd.setCursor(13, 1);
lcd.write(3);
lcd.setCursor(14, 0);
lcd.write(4);
lcd.setCursor(15, 1);
lcd.write(5);
lcd.setCursor(15, 0);
lcd.write(6);
lcd.setCursor(14, 1);
lcd.write(7);
}
else
{
pt2258_mute=0;
setPt2258_mute();
lcd.clear();
delay(300);
menu_active=0;
lcd_updatez();
}
}
// setVolume ch1...........................................................
void potVol_ch1()
{
if(menu_active==0)
{
int value1 = analogRead(potPin1);
value1 = map(value1, 0, 1023, 69, 19);
delay(200);
save_eeprom=1;
setVolCH1=value1;
c1 = setVolCH1; // c1 Menampilkan angka ke LCD
volumeCH1();
lcd_updatez();
}
}
// setVolume ch2..........................................................
void potVol_ch2()
{
if(menu_active==0)
{
int value2 = analogRead(potPin2);
value2 = map(value2, 0, 1023, 69, 19);
delay(200);
save_eeprom=1;
setVolCH2=value2;
c2 = setVolCH2; // c2 Menampilkan angka ke LCD
volumeCH2();
lcd_updatez();
}
}
// startUp......................................................................
void start_up() {
pt2258_mute=1;
setPt2258_mute();
lcd.setCursor(0, 1);
lcd.print(" HELLO... ");
delay(1500);
lcd.clear();
delay(300);
lcd.setCursor(0, 1);
lcd.print("LOADING ");
for(int x=6; x<15; x++)
{
lcd.setCursor(x+1, 1);
lcd.print(".");
delay(300);
}
delay(1000);
lcd.clear();
pt2258();
pt2258_mute=0;
setPt2258_mute();
menu_active=0;
volumeCH1();
volumeCH2();
lcd_updatez();
}
// lcd updated....................................................................
void lcd_updatez(){
if(menu_active==0)
{
custom_num_shape();
//lcd.setCursor(0, 1);
//lcd.print("VOLUME");
int y1,y2;
if (c1 < 0) {
lcd.setCursor(0, 1);
lcd.print("-");
y1 = 10 - (c1 + 10);
}
else if (c1 == -10) {
lcd.setCursor(0, 1);
lcd.print("-");
y1 = 10;
}
else {
lcd.setCursor(0, 1);
lcd.print(" ");
y1 = c1;
}
if (c2 < 0) {
lcd.setCursor(8, 1);
lcd.print("-");
y2 = 10 - (c2 + 10);
}
else if (c2 == -10) {
lcd.setCursor(8, 1);
lcd.print("-");
y2 = 10;
}
else {
lcd.setCursor(8, 1);
lcd.print(" ");
y2 = c2;
}
a1 = y1 / 10;
b1 = y1 - a1 * 10; // y1%10
a2 = y2 / 10;
b2 = y2 - a2 * 10; // y2%10
lcd.setCursor(1, 0);
for (int i = 0; i < digit_width; i++)
lcd.print(custom_num_top[a1][i]);
lcd.setCursor(9, 0);
for (int i = 0; i < digit_width; i++)
lcd.print(custom_num_top[a2][i]);
lcd.setCursor(1, 1);
for (int i = 0; i < digit_width; i++)
lcd.print(custom_num_bot[a1][i]);
lcd.setCursor(9, 1);
for (int i = 0; i < digit_width; i++)
lcd.print(custom_num_bot[a2][i]);
lcd.setCursor(5, 0);
for (int i = 0; i < digit_width; i++)
lcd.print(custom_num_top[b1][i]);
lcd.setCursor(13, 0);
for (int i = 0; i < digit_width; i++)
lcd.print(custom_num_top[b2][i]);
lcd.setCursor(5, 1);
for (int i = 0; i < digit_width; i++)
lcd.print(custom_num_bot[b1][i]);
lcd.setCursor(13, 1);
for (int i = 0; i < digit_width; i++)
lcd.print(custom_num_bot[b2][i]);
}
if(menu_active==1)
{
//
}
}
//custom shape........................................................
void custom_num_shape() {
for (int i = 0; i < 8; i++)
lcd.createChar(i, custom_num[i]);
}
//SetVolume CH1.......................................................
void volumeCH1()
{
if (setVolCH1 > 69)
{
setVolCH1 = 69;
}
if (setVolCH1 < 19)
{
setVolCH1 = 19;
}
volCH1 = setVolCH1 + 10;
int cx1 = 79 - volCH1;
a1 = cx1 / 10;
b1 = cx1 - a1 * 10;
pt2258_send(0b10000000 + a1, 0b10010000 + b1); //CH1
pt2258_send(0b01000000 + a1, 0b01010000 + b1); //CH2
if(setVolCH1 == 19)
{
pt2258_mute = 1;
}
else
{
pt2258_mute = 0;
}
setPt2258_mute();
}
//SetVolume CH2........................................................
void volumeCH2()
{
if (setVolCH2 > 69)
{
setVolCH2 = 69;
}
if (setVolCH2 < 19)
{
setVolCH2 = 19;
}
volCH2 = setVolCH2 + 10;
int cx2 = 79 - volCH2;
a2 = cx2 / 10;
b2 = cx2 - a2 * 10;
pt2258_send(0b01100000 + a2, 0b01110000 + b2); //CH5
pt2258_send(0B10100000 + a2, 0B10110000 + b2); //CH6
if(setVolCH2 == 19)
{
pt2258_mute = 1;
}
else
{
pt2258_mute = 0;
}
setPt2258_mute();
}
//setPt2258Mute......................................................
void setPt2258_mute() {
Wire.beginTransmission(PT2258_address);
switch (pt2258_mute) {
case 0: Wire.write (0b11111000);; break; // mute disabled
case 1: Wire.write (0b11111001);; break; // mute enable
}
Wire.endTransmission();
}
//pt2258 send........................................................
void pt2258() {
Wire.beginTransmission(PT2258_address);
Wire.write (0b11000000);
Wire.endTransmission();
}
void pt2258_send(char c, char d) {
Wire.beginTransmission(PT2258_address);
Wire.write (c);
Wire.write (d);
Wire.endTransmission();
}
/*
* CODE1=VCC, CODE2=GND
* Addres 88H
* pins 4 and 17 (CODE2 and CODE1)
* Arduino the I2C address is 7 bits, so instead of 0x88 (0B10001000), 0x44 (0B1000100) is indicated
*
* https://github.com/liman324/PT2258
*
* PT2258_address 0B1000100
CLEAR 0B11000000
VOL_MASTER_10 0B11010000
VOL_MASTER_1 0B11100000
VOL_1CH_10 0B10000000
VOL_1CH_1 0B10010000
VOL_2CH_10 0B01000000
VOL_2CH_1 0B01010000
VOL_3CH_10 0B00000000
VOL_3CH_1 0B00010000
VOL_4CH_10 0B00100000
VOL_4CH_1 0B00110000
VOL_5CH_10 0B01100000
VOL_5CH_1 0B01110000
VOL_6CH_10 0B10100000
VOL_6CH_1 0B10110000
MUTE 0B11111000
*/
/*
void set_mas(int mas_vol) {
c = 79 - mas_vol;
a = c / 10;
b = c - a * 10;
pt2258_send(0b11010000 + a, 0b11100000 + b); // All CH
}
void set_ch1(int ch1_vol) {
c = 79 - ch1_vol;
a = c / 10;
b = c - a * 10;
pt2258_send(0b10000000 + a, 0b10010000 + b); // CH1
}
void set_ch2(int ch2_vol) {
c = 79 - ch2_vol;
a = c / 10;
b = c - a * 10;
pt2258_send(0b01000000 + a, 0b01010000 + b); // CH2
}
void set_ch3(int ch3_vol) {
c = 79 - ch3_vol;
a = c / 10;
b = c - a * 10;
pt2258_send(0b00000000 + a, 0b00010000 + b); // CH3
}
void set_ch4(int ch4_vol) {
c = 79 - ch4_vol;
a = c / 10;
b = c - a * 10;
pt2258_send(0b00100000 + a, 0b00110000 + b); // CH4
}
void set_ch5(int ch5_vol) {
c = 79 - ch5_vol;
a = c / 10;
b = c - a * 10;
pt2258_send(0b01100000 + a, 0b01110000 + b); // CH5
}
void set_ch6(int ch6_vol) {
c = 79 - ch6_vol;
a = c / 10;
b = c - a * 10;
pt2258_send(0b10100000 + a, 0b10110000 + b); // CH6
}
*/