#define CS 2 // LOAD PIN 10 LM1972
#define CLK 3 // SCLK PIN 9 LM1972
#define DATA 4 // DATA PIN 11 LM1972
#include <Wire.h>
#include <EEPROM.h>
#include <Encoder.h> // http://rcl-radio.ru/wp-content/uploads/2019/05/Encoder.zip
#include <MsTimer2.h> // http://rcl-radio.ru/wp-content/uploads/2018/11/MsTimer2.zip
#include <LiquidCrystal_I2C.h> //Библиотека - http://forum.rcl-radio.ru/misc.php?action=pan_download&item=45&download=1
LiquidCrystal_I2C lcd(0x27,16,2); // Устанавливаем дисплей
Encoder myEnc(9, 8);//CLK, DT
byte a1[8] = {0b00000,0b10101,0b10101,0b10101,0b10101,0b10101,0b10101,0b00000};
byte a2[8] = {0b00000,0b10100,0b10100,0b10100,0b10100,0b10100,0b10100,0b00000};
byte a3[8] = {0b00000,0b10000,0b10000,0b10000,0b10000,0b10000,0b10000,0b00000};
int vol,vol_d,z,z0,z1,vol_old;
byte w,w1,mute;
unsigned long times,oldPosition = -999,newPosition;
void setup() {
Wire.begin();Serial.begin(9600);
lcd.init();lcd.backlight();
MsTimer2::set(1, to_Timer);MsTimer2::start();
lcd.createChar(0,a1);lcd.createChar(1,a2);lcd.createChar(2,a3);
pinMode(CS,OUTPUT);
pinMode(CLK,OUTPUT);
pinMode(DATA,OUTPUT);
pinMode(10,INPUT); // кнопка энкодера MUTE
delay(100);
vol = EEPROM.read(0);
LM1972Write(0, vol);
LM1972Write(1, vol);
}
void loop() {
/// MUTE ///////////////////////////////////////
if(digitalRead(10)==LOW&&mute==0){mute=1;vol_old = vol;lcd.clear();lcd.setCursor(0,0);lcd.print(" MUTE ");
LM1972Write(0, 255);
LM1972Write(1, 255);
delay(300);}
if(digitalRead(10)==LOW&&mute==1){mute=0;vol=vol_old;lcd.clear();w1=1;
LM1972Write(0, vol);
LM1972Write(1, vol);
delay(300);}
/////////////// VOLUME /////////////////////////////////////////////////////////
if(mute==0){
if (newPosition != oldPosition&&mute==0){oldPosition = newPosition;
vol=vol-newPosition;myEnc.write(0);newPosition=0;times=millis();w=1;w1=1;vol_func();
LM1972Write(0, vol);
LM1972Write(1, vol);
}
vol_d=map(vol,0,126,0,45);
if(w1==1){
for(z=0,z0=0,z1=0;z<=vol_d;z++,z1++){if(z1>2){z1=0;z0++;}
if(z1==1){lcd.setCursor(z0,1);lcd.write((uint8_t)0);lcd.setCursor(z0+1,1);lcd.print(" ");}}
if(z1==3){lcd.setCursor(z0,1);lcd.write((uint8_t)1);}
if(z1==2){lcd.setCursor(z0,1);lcd.write((uint8_t)2);}w1=0;}
lcd.setCursor(0,0);lcd.print("VOLUME ");
lcd.print(vol);lcd.print(" ");
}
//////// EEPROM //////////////////////////////////////////////////
if(millis()-times>10000 && w==1 && mute == 0){EEPROM.update(0,vol);w=0;w1=1;lcd.clear();}
}
void to_Timer(){newPosition = myEnc.read()/4;}
void vol_func(){if(vol<0){vol=0;}if(vol>126){vol=126;}}
void LM1972Write(byte ch, byte att){
digitalWrite(CLK,LOW);delay(1);digitalWrite(CS,LOW);
for(int i = 7; i >= 0; i--){
digitalWrite(DATA, (ch >> i) & 0x01);
digitalWrite(CLK,HIGH);digitalWrite(CLK,LOW);}
for(int i = 7; i >= 0; i--){
digitalWrite(DATA, (att >> i) & 0x01);
digitalWrite(CLK,HIGH);digitalWrite(CLK,LOW);}
digitalWrite(CS,HIGH);delay(1);
}