First, I want to state that my coding knowledge is less than minimal. I just read the comments and I try to write using examples and common sense.
With that out of the way, I want to say that just browsing through the forums helps a lot, but aparrently some things are just too difficult for a beginner.
I am trying to make a preamplifier using arduino and PT2314. Why? Just for the cool factor. By using the code from this project and with the help of the project starter, I managed to compile without errors (yay!)
Later I decided to add two neopixel rings, because they look cool.
The funny thing is that I still wait for some components to arrive, but Wokwi has been a great help, at least I know that things (should) work.
I'm using an Uno, two encoders, a 20x4 LCD, two rings and a ds3231. And of course, the PT2314.
Code works according to wokwi, but I need help with the pixels. I want one of them to be able to show the current volume when turning the encoder, but I have been unsuccessful. It kinda works, but the leds flicker and I have a feeling that it's not just the emulation.
Anyway, here is the code:
// Modified code from http://forum.rcl-radio.ru/viewtopic.php?pid=1084#p1084, with help from liman324. (Thank god for google translate!)
// Left encoder controls cursor and inputs, pressing when POWER OFF sets time.
// Right one controls values and long press calls secondary menu.
// Rings are just an afterthought, but seem to make the project laggy, at least in the emulator. They look nice, though :)
// Button is mute and power off/show time.
#define AUTO_GAIN 1 // auto adjust by volume
#define VOL_THR 35 //silence threshold (below it there will be no display on the lcd)
#define LOW_PASS 30 // lower threshold of noise sensitivity (no jumps in the absence of sound???)
#define DEF_GAIN 80 // default maximum threshold
#include "PT2314.h"
#include <LiquidCrystal_I2C.h>
#include "Encoder.h"
#include <EEPROM.h>
#include "MsTimer2.h"
#include "DS3231.h"
#include <Adafruit_NeoPixel.h>
#define PIN 7
#define NUMPIXELS 32
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
DS3231 clock;RTCDateTime DateTime;
PT2314 pt;
LiquidCrystal_I2C lcd(0x27,20,4);
Encoder myEnc(2, 4);// DT, CLK
Encoder myEnc2(3, 5);// DT, CLK
int menu,menu0,menu1,vol,bass,treb,in,balans,balans_d,vol_d,bass_d,treb_d,temp0,par,hour,minut,secon,z_old,gain0,loud;
int att_r,att_l,gain1,gain2,gain3,gain4,brig0,brig1;
byte w2[4],z,z0,z1,n,q,gr1,gr2,www,i,w,mute,power,in_x,save,i1,pin,pin2,hms,irr;
unsigned long time0,times_in;
unsigned int oldPosition = -999,newPosition;
unsigned int oldPosition2 = -999,newPosition2;
const PROGMEM byte mesto2[4]={0,11,0,11};
const PROGMEM byte mesto3[4]={0,0,1,1};
unsigned long gainTimer;
byte maxValue, maxValue_f,spek;
int i2;
int ek;
void setup() {
pixels.begin();
pixels.clear();
for(int i=0; i<16; i++) {
pixels.setPixelColor(16 * 0 + i, 0xFFFFFF);
pixels.setPixelColor(16 * 1 + i, 0xFFFFFF);
delay(75);
pixels.show(); }
for(int i=0; i<16; i++);
pixels.clear();
int bass=0;
int treb=0;
lcd.begin(20, 4);
lcd.init();lcd.backlight();
clock.begin();
pinMode(12,INPUT_PULLUP); // Encoder Button
pinMode(8,INPUT_PULLUP); // Encoder2 Button
pinMode(9,INPUT); // STANDBY / MUTE
pinMode(13,OUTPUT); // STANDBY LED
lcd.setCursor(0,0);lcd.print("From here...");lcd.setCursor(6,3);lcd.print("...to Eternity"); delay(1000);lcd.clear();
MsTimer2::set(3, to_Timer);MsTimer2::start();
// clock.setDateTime(__DATE__, __TIME__); // Set PC time as sketch time
if(EEPROM.read(100)!=0){for(int i=0;i<101;i++){EEPROM.update(i,0);}}// Clear memory on first boot
vol = EEPROM.read(0);treb = EEPROM.read(1)-7;bass = EEPROM.read(2)-7;in = EEPROM.read(3);
att_l = EEPROM.read(4);att_r = EEPROM.read(5);gain1 = EEPROM.read(6);gain2 = EEPROM.read(7);
gain3 = EEPROM.read(8);gain4 = EEPROM.read(9);loud = EEPROM.read(10);ek = EEPROM.read(13);
w2_arr();audio();cl();
}
void loop() {
DateTime = clock.getDateTime();hour = DateTime.hour;minut = DateTime.minute;secon = DateTime.second;
if (newPosition != oldPosition||digitalRead(12)==LOW) {if(spek==1){cl();spek=0;w=0;menu0=0;menu=0;w2_arr();time0=millis();w=1;}}
/////////////////////////////// control //////////////////////////////////////////////
if(mute==0&&power==0){
if(digitalRead(12)==LOW){pin2++;}if(digitalRead(12)==HIGH){pin2=0;}
if((pin2>5)&&menu0==0){pin2=0;menu0=1;cl();time0=millis();w=1;lcd.setCursor(6,1);lcd.print("SETTINGS"); delay(1000);lcd.clear();}
if((pin2>5)&&menu0==1){pin2=0;menu0=0;menu=0;cl();w2_arr();time0=millis();w=1;lcd.setCursor(8,1);lcd.print("MAIN"); delay(1000);lcd.clear();}
if(digitalRead(8)==LOW){in++;cl();time0=millis();w=1;times_in=millis();in_x=1;www=1;w=1;menu0=100;if(in>3){in=0;}}// INPUT
}
if(digitalRead(6)==HIGH){delay(5);pin++;} // Power/Mute Button
if((pin>0&&pin<10&&digitalRead(6)==LOW)&&mute==0&&power==0){pin=0;mute=1;pt.setAttR(31);pt.setAttL(31);menu0=100;cl();w=1;w2_arr();lcd.setCursor(8,1);lcd.print("MUTE");delay(300);}// mute on
if((pin>0&&pin<10&&digitalRead(6)==LOW)&&mute==1&&power==0){pin=0;mute=0;cl();time0=millis();w=1;w2_arr();menu0=0;myEnc.write(0);audio();}// mute off
if((pin>10)&&power==0){pin=0;power=1;save=1;pt.setAttR(31);pt.setAttL(31);cl();lcd.setCursor(5,1);lcd.print("POWER OFF");menu0=100;delay(3000);}// power off
if((pin>0)&&power==1){pin=0;power=0;lcd.clear();lcd.setCursor(5,1);lcd.print("POWER ON ");time0=millis();w=1;w2_arr();menu0=0;myEnc.write(0);audio();delay(3000);cl();}// power on
/////////////////////////////////////////////////////////////////////////////////////////////
if(power==0){digitalWrite(13,HIGH);
if(spek==0){
byte v1[8] = {0b00000,0b10101,0b10101,0b10101,0b10101,0b10101,0b10101,0b00000};
byte v2[8] = {0b00000,0b10100,0b10100,0b10100,0b10100,0b10100,0b10100,0b00000};
byte v3[8] = {0b00000,0b10000,0b10000,0b10000,0b10000,0b10000,0b10000,0b00000};
byte v4[8] = {0b10000,0b11000,0b11100,0b11110,0b11100,0b11000,0b10000,0b00000}; //>
byte v5[8] = {0b00000,0b00000,0b00000,0b00000,0b00000,0b00000,0b00000,0b00000};
lcd.createChar(0,v1);lcd.createChar(1,v2);lcd.createChar(2,v3);lcd.createChar(3,v4);lcd.createChar(4,v5);}
}
if(power==1){digitalWrite(13,LOW);
byte v1[8] = {7,7,7,7,7,7,7,7};
byte v2[8] = {7,7,0, 0, 0, 0, 0, 0};
byte v3[8] = { 0, 0, 0, 0, 0,0,31,31};
byte v4[8] = {31,31, 0, 0, 0, 0,31,31};
byte v5[8] = { 28, 28, 0, 0, 0, 0, 28, 28};
byte v6[8] = {28,28,28,28,28,28,28,28};
byte v7[8] = { 0, 0, 0, 0, 0, 0,7,7};
byte v8[8] = { 31, 31,0,0,0,0,0, 0};
byte a[6];
byte i0,d1,d2,d3,d4,d5,d6,e1,e2,e3;
lcd.createChar(1, v1);lcd.createChar(2, v2);lcd.createChar(3, v3);lcd.createChar(4, v4);lcd.createChar(5, v5);lcd.createChar(6, v6);lcd.createChar(7, v7);lcd.createChar(8, v8);
a[0]=DateTime.hour/10;
a[1]=DateTime.hour%10;
a[2]=DateTime.minute/10;
a[3]=DateTime.minute%10;
a[4]=DateTime.second/10;
a[5]=DateTime.second%10;
for(i=0;i<6;i++){
switch(i){
case 0: e1=0,e2=1,e3=2;break;
case 1: e1=3,e2=4,e3=5;break;
case 2: e1=7,e2=8,e3=9;break;
case 3: e1=10,e2=11,e3=12;break;
case 4: e1=14,e2=15,e3=16;break;
case 5: e1=17,e2=18,e3=19;break;
}
switch(a[i]){
case 0: d1=1,d2=8,d3=6,d4=1,d5=3,d6=6;break;
case 1: d1=32,d2=2,d3=6,d4=32,d5=32,d6=6;break;
case 2: d1=2,d2=8,d3=6,d4=1,d5=4,d6=5;break;
case 3: d1=2,d2=4,d3=6,d4=7,d5=3,d6=6;break;
case 4: d1=1,d2=3,d3=6,d4=32,d5=32,d6=6;break;
case 5: d1=1,d2=4,d3=5,d4=7,d5=3,d6=6;break;
case 6: d1=1,d2=4,d3=5,d4=1,d5=3,d6=6;break;
case 7: d1=1,d2=8,d3=6,d4=32,d5=32,d6=6;break;
case 8: d1=1,d2=4,d3=6,d4=1,d5=3,d6=6;break;
case 9: d1=1,d2=4,d3=6,d4=7,d5=3,d6=6;break;
}
lcd.setCursor(e1,0);lcd.write((uint8_t)d1);lcd.setCursor(e2,0);lcd.write((uint8_t)d2);lcd.setCursor(e3,0);lcd.write((uint8_t)d3);
lcd.setCursor(e1,1);lcd.write((uint8_t)d4);lcd.setCursor(e2,1);lcd.write((uint8_t)d5);lcd.setCursor(e3,1);lcd.write((uint8_t)d6);
}
lcd.setCursor(6,0);lcd.print(".");lcd.setCursor(13,0);lcd.print(".");lcd.setCursor(6,1);lcd.print(".");lcd.setCursor(13,1);lcd.print(".");
lcd.setCursor(5,3);lcd.print("POWER OFF");
//// set time //////
if(digitalRead(8)==LOW){hms++;cl();delay(200);if(hms>3){hms=0;}}
switch(hms){
case 1: lcd.setCursor(1,2);lcd.print("------");break;
case 2: lcd.setCursor(7,2);lcd.print("------");break;
case 3: lcd.setCursor(14,2);lcd.print("------");break;
}
if(digitalRead(8)==LOW&&hms==1){hour++;if(hour>23){hour=0;} clock.setDateTime(2020, 9, 15, hour, minut, secon);delay(100);}
if(digitalRead(8)==LOW&&hms==2){minut++;if(minut>59){minut=0;} clock.setDateTime(2020, 9, 15, hour, minut, secon);delay(100); }
if(digitalRead(8)==LOW&&hms==3){secon=0; clock.setDateTime(2020, 9, 15, hour, minut, secon);hms=0; }
}
/////////////////////////////// MENU0 = VOLUME TERBLE BASS BALANCE ///////////////////////////////////////////////////////////////////////
if(menu0==0){
switch(menu){
case 0: temp0 = vol;q=0;break;
case 1: temp0 = bass;q=1;break;
case 2: temp0 = treb;q=2;break;
}
if (newPosition != oldPosition){oldPosition = newPosition;
temp0=temp0+newPosition;myEnc.write(0);newPosition=0;time0=millis();w=1;w2[q]=1;www=1;}
switch(menu){
case 0: vol = temp0;vol_func();break;
case 1: bass = temp0;bass_func();break;
case 2: treb = temp0;treb_func();break;
}
if (newPosition2 != oldPosition2);{oldPosition2 = newPosition2;
menu=menu+newPosition2;myEnc2.write(0);newPosition2=0;time0=millis();
if(menu>2){menu=2;}if(menu<0){menu=0;}
}
au();
for(i=0;i<3;i++){if(menu==i){lcd.setCursor(0,i);lcd.write((uint8_t)3);}else{lcd.setCursor(0,i);lcd.print(" ");}}
lcd.setCursor(5,0);lcd.print("VOLUME ");if(vol>=0){lcd.print(" ");}lcd.print(vol);lcd.print(" ");vol_d=map(vol,0,56,1,24);
lcd.setCursor(5,1);lcd.print("BASS ");if(bass>=0){lcd.print(" ");}lcd.print(bass);lcd.print(" ");bass_d=map(bass,-7,7,1,24);
lcd.setCursor(5,2);lcd.print("TREBLE ");if(treb>=0){lcd.print(" ");}lcd.print(treb);lcd.print(" ");treb_d=map(treb,-7,7,1,24);
if (vol >0 && vol < 7 )
for(int i=0;i<2;i++){
pixels.setPixelColor(i, pixels.Color(255,255,225));}
if (vol >7 && vol < 15 )
for(int i=0;i<4;i++){
pixels.setPixelColor(i, pixels.Color(255,255,225));}
if (vol >15 && vol < 23 )
for(int i=0;i<6;i++){
pixels.setPixelColor(i, pixels.Color(255,255,225));}
if (vol >23 && vol < 31 )
for(int i=0;i<8;i++){
pixels.setPixelColor(i, pixels.Color(255,255,225));}
if (vol >31 && vol < 39 )
for(int i=0;i<10;i++){
pixels.setPixelColor(i, pixels.Color(255,255,225));}
if (vol >39 && vol < 47 )
for(int i=0;i<12;i++){
pixels.setPixelColor(i, pixels.Color(255,255,225));}
if (vol >47 && vol < 55 )
for(int i=0;i<14;i++){
pixels.setPixelColor(i, pixels.Color(255,255,225));}
if (vol >55 )
for(int i=0;i<16;i++){
pixels.setPixelColor(i, pixels.Color(255,255,225));}
if (newPosition != oldPosition){oldPosition = newPosition;
i=i+newPosition;myEnc.write(0);newPosition=0;time0=millis();pixels.clear();pixels.show();}
lcd.setCursor(1,3);
switch(in){
case 0: lcd.print("IN1");
for(int i=16; i<32; i++) {
pixels.setPixelColor(i, pixels.Color(255, 0, 0));
}pixels.show();break;
case 1: lcd.print("IN2 ");
for(int i=16; i<32; i++) {
pixels.setPixelColor(i, pixels.Color(0, 153, 0));
}pixels.show();break;
case 2: lcd.print("IN3 ");for(int i=16; i<32; i++) {
pixels.setPixelColor(i, pixels.Color(127, 0, 255));
}pixels.show();break;
case 3: lcd.print("IN4 ");for(int i=16; i<32; i++) {
pixels.setPixelColor(i, pixels.Color(0, 195, 203));
}pixels.show();break;
}
lcd.setCursor(12,3);
lcd.print(DateTime.hour/10);lcd.print(DateTime.hour%10);lcd.print(":");lcd.print(DateTime.minute/10);lcd.print(DateTime.minute%10);lcd.print(":");
lcd.print(DateTime.second/10);lcd.print(DateTime.second%10);
// lcd.setCursor(11,3);
// lcd.print(float(gain0*3.75));lcd.print(" ");lcd.print("dB ");
for(n=0;n<3;n++){if(w2[n]==1){
switch(n)
for(z=0,z0=0,z1=0;z<=par;z++,z1++){if(z1>2){z1=0;z0++;}
if(z1==1){lcd.setCursor(z0+12,n);lcd.write((uint8_t)0);lcd.setCursor(z0+1+12,n);z_old=7-par/3;while(z_old>0){z_old=z_old-1;lcd.write((uint8_t)4);}}}
if(z1==3){lcd.setCursor(z0+12,n);lcd.write((uint8_t)1);}
if(z1==2){lcd.setCursor(z0+12,n);lcd.write((uint8_t)2);}w2[n]=0;}
}}// menu0 = 0
///////////////////////////////////////////////////////////////////////////////////////////////
if(menu0==1){
switch(menu1){
case 0: temp0 = att_l;break;
case 1: temp0 = att_r;break;
case 2: temp0 = loud;break;
case 3: temp0 = ek;break;
}
if (newPosition != oldPosition){oldPosition = newPosition;
temp0=temp0+newPosition;myEnc.write(0);newPosition=0;time0=millis();}
switch(menu1){
case 0: att_l = temp0;att_l_func();break;
case 1: att_r = temp0;att_r_func();break;
case 2: loud = temp0;loud_func();break;
case 3: ek = temp0;ek_func();break;
}
if (newPosition2 != oldPosition2);{oldPosition2 = newPosition2;
menu1=menu1+newPosition2;myEnc2.write(0);newPosition2=0;time0=millis();
if(menu1>3){menu1=3;}if(menu1<0){menu1=0;}
}
au();
for(i=0;i<4;i++){if(menu1==i){lcd.setCursor(pgm_read_byte(&mesto2[i]),pgm_read_byte(&mesto3[i]));lcd.write((uint8_t)3);}else{lcd.setCursor(pgm_read_byte(&mesto2[i]),pgm_read_byte(&mesto3[i]));lcd.print(" ");}}
lcd.setCursor(1,0);lcd.print("ATT_L ");lcd.print(att_l);lcd.print(" ");lcd.setCursor(12,0);lcd.print("ATT_R ");lcd.print(att_r);lcd.print(" ");
lcd.setCursor(1,1);lcd.print("LOUD ");if(loud==0){lcd.print("OFF");}else{lcd.print("ON ");}lcd.setCursor(12,1);lcd.print("SPEK ");lcd.print(ek);
} // menu0 = 1
////////////////////////// GAIN IN /////////////////////////////////////////////////////////////////////////////////////
if(in_x==1){ menu0=100;
switch(in){
case 0: gain0 = gain1;break;
case 1: gain0 = gain2;break;
case 2: gain0 = gain3;break;
case 3: gain0 = gain4;break;}
if (newPosition2 != oldPosition2) {oldPosition2 = newPosition2;
gain0=gain0+newPosition2;myEnc2.write(0);newPosition2=0;time0=millis();times_in=millis();www=1;w=1;gain_func();}
switch(in){
case 0: gain1 = gain0;break;
case 1: gain2 = gain0;break;
case 2: gain3 = gain0;break;
case 3: gain4 = gain0;break;}
au();
switch(in){
case 0: lcd.setCursor(7,1);lcd.print("IN1 ");lcd.setCursor(6,2);lcd.print(float(gain0*3.75));lcd.print(" ");lcd.print("dB ");break;
case 1: lcd.setCursor(8,1);lcd.print("IN2 ");lcd.setCursor(6,2);lcd.print(float(gain0*3.75));lcd.print(" ");lcd.print("dB ");break;
case 2: lcd.setCursor(8,1);lcd.print("IN3 ");lcd.setCursor(6,2);lcd.print(float(gain0*3.75));lcd.print(" ");lcd.print("dB ");break;
case 3: lcd.setCursor(8,1);lcd.print("IN4 ");lcd.setCursor(6,2);lcd.print(float(gain0*3.75));lcd.print(" ");lcd.print("dB ");break;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(millis()-time0>10000&&w==1&&mute==0&&power==0){cl();w=0;spek=0;menu0=100;}
if(millis()-times_in>3000&&in_x==1&&mute==0&&power==0){cl();in_x=0;menu0=0;menu=0;w2_arr();}
if(save==1){save=0;
EEPROM.update(0,vol);EEPROM.update(1,treb+7);EEPROM.update(2,bass+7);EEPROM.update(3,in);
EEPROM.update(4,att_l);EEPROM.update(5,att_r);EEPROM.update(6,gain1);EEPROM.update(7,gain2);
EEPROM.update(8,gain3);EEPROM.update(9,gain4);EEPROM.update(10,loud);EEPROM.update(13,ek);}
}// loop
void ek_func(){if(ek>5){ek=5;}if(ek<0){ek=0;}}
void w2_arr(){w2[0]=1;w2[1]=1;w2[2]=1;w2[3]=1;}
void au(){if(www==1){audio();www=0;}}
void in_func(){if(in>3){in=3;}if(in<0){in=0;}}
void loud_func(){if(loud<0){loud=0;}if(loud>1){loud=1;}}
void gain_func(){if(gain0<0){gain0=0;}if(gain0>3){gain0=3;}}
void att_r_func(){if(att_r<0){att_r=0;}if(att_r>24){att_r=24;}}
void att_l_func(){if(att_l<0){att_l=0;}if(att_l>24){att_l=24;}}
void treb_func(){if(treb<-7){treb=-7;}if(treb>7){treb=7;}}
void bass_func(){if(bass<-7){bass=-7;}if(bass>7){bass=7;}}
void vol_func(){if(vol<0){vol=0;}if(vol>56){vol=56;}}
void cl(){spek=0;delay(300);lcd.clear();}
void cl1(){spek=0;delay(200);}
void audio(){
pt.setVolume(vol); // int 0...56 === -70...0 dB step 1.25 dB
pt.setAttL(att_l); // int 0...24 === 0...-30 db step 1.25 dB > int 31 === mute on
pt.setAttR(att_r); // int 0...24 === 0...-30 dB step 1.25 dB > int 31 === mute on
pt.setSwitch(in, loud, gain0);
// input int 0..3 === IN 1...4
// loudness int 0...1 === 1-on 0-off
// input gain int 0...3 === 0...11.25dB step 3.75 dB
pt.setBass(bass); // int -7...+7 === -14...+14 dB step 2 dB
pt.setTreble(treb); // int -7...+7 === -14...+14 dB step 2 dB
}
void to_Timer(){newPosition = myEnc.read()/4;newPosition2 = myEnc2.read()/4;}
Feel free to check the emulation, also.
Thanks for reading and for any help you can offer.