#define leftSpeaker 2 // Define output pin for speaker
#define rightSpeaker 0 // Define output pin for speaker
#define LEDC_RESOLUTION 8 // Set resolution to 8 bits
//nastavení tónů
const int NOTE_C4 = 262;
const int NOTE_CSHARP4 = 277;
const int NOTE_D4 = 294;
const int NOTE_DSHARP4 = 311;
const int NOTE_E4 = 330;
const int NOTE_F4 = 349;
const int NOTE_FSHARP4 = 370;
const int NOTE_G4 = 392;
const int NOTE_GSHARP4 = 415;
const int NOTE_A4 = 440;
const int NOTE_ASHARP4 = 466;
const int NOTE_B4 = 494;
//nastavení analogových vstupů
const int anRead1 = 36; //36
const int anRead2 = 39; //39
const int anRead3 = 35; //35
const int anRead4 = 34; //34
//jaký reproduktor hraje
int left = 0;
int right = 0;
//nastavení proměnných pro jednotlivé vstupy
int usg1 = 0;
int change1 = 0;
int l1 = 0;
int r1 = 0;
int usg2 = 0;
int change2 = 0;
int l2 = 0;
int r2 = 0;
int usg3 = 0;
int change3 = 0;
int l3 = 0;
int r3 = 0;
int usg4 = 0;
int change4 = 0;
int l4 = 0;
int r4 = 0;
void buttons(int anRead, int note1, int note2, int note3, int* l, int* r, int* usg, int* change) {
if (analogRead(anRead) > 2300) { // Žádné stisklé tlačítko, resetuje hodnoty
if (*usg > 0) {
ledcDetach(leftSpeaker);
ledcDetach(rightSpeaker);
*usg = 0;
*l = 0 ;
*r = 0;
left = 0;
right = 0;
}
} else if (analogRead(anRead) > 2150 && 2300 > analogRead(anRead)) { // Hraje tón D ----------------------------------
if (left == 0) { //Nehraje žádný tón
ledcDetach(rightSpeaker);
ledcAttachChannel(leftSpeaker, note3, LEDC_RESOLUTION, 1);
ledcWriteTone(leftSpeaker, note3);
left = 1;
right = 0;
*l = 1;
*r = 0;
*usg = 1;
}
if (left == 1 && right == 0 && *usg != 1) { // Hraje jeden tón
ledcAttachChannel(rightSpeaker, note3, LEDC_RESOLUTION, 2);
ledcWriteTone(rightSpeaker, note3);
right = 1;
*r = 1;
*usg = 1;
}
if (left == 1 && right == 1 && *usg >= 2) { // Hráli dva tóny z jednoho vstupu, ale ted hraje už jen jeden
ledcDetach(leftSpeaker);
ledcDetach(rightSpeaker);
ledcAttachChannel(leftSpeaker, note3, LEDC_RESOLUTION, 1);
ledcWriteTone(leftSpeaker, note3);
left = 1;
right = 0;
*usg = 1;
}
if (left == 1 && right == 1 && *usg == 1 && *change == 1) { //Hraje jeden tón na jiném vstupu a na aktualním vstupu se rychle vyměnili dva tóny
if (*l == 1 && *change == 1) { // rozhoduje jaký reproduktor hrál tón z tohoto vstupu
ledcDetach(leftSpeaker);
ledcAttachChannel(leftSpeaker, note3, LEDC_RESOLUTION, 1);
ledcWriteTone(leftSpeaker, note3);
}
if (*r == 1) { // rozhoduje jaký reproduktor hrál tón z tohoto vstupu
ledcDetach(rightSpeaker);
ledcAttachChannel(rightSpeaker, note3, LEDC_RESOLUTION, 2);
ledcWriteTone(rightSpeaker, note3);
}
change = 0;
}
} else if (analogRead(anRead) > 1950 && 2150 > analogRead(anRead)) { // Hraje tón CIS ---------------------------------
if (left == 0) { //Nehraje žádný tón
ledcDetach(rightSpeaker);
ledcAttachChannel(leftSpeaker, note2, LEDC_RESOLUTION, 1);
ledcWriteTone(leftSpeaker, note2);
left = 1;
right = 0;
*l = 1;
r = 0;
*usg = 1;
}
if (left == 1 && right == 0 && *usg != 1) { // Hraje jeden tón
ledcAttachChannel(rightSpeaker, note2, LEDC_RESOLUTION, 2);
ledcWriteTone(rightSpeaker, note2);
right = 1;
*r = 1;
*usg = 1;
}
if (left == 1 && right == 1 && *usg >= 2) { // Hráli dva tóny z jednoho vstupu, ale ted hraje už jen jeden
ledcDetach(leftSpeaker);
ledcDetach(rightSpeaker);
ledcAttachChannel(leftSpeaker, note2, LEDC_RESOLUTION, 1);
ledcWriteTone(leftSpeaker, note2);
left = 1;
right = 0;
*usg = 1;
}
if (left == 1 && right == 1 && *usg == 1 && *change == 1) { //Hraje jeden tón na jiném vstupu a na aktualním vstupu se rychle vyměnili dva tóny
if (*l == 1 && *change == 1) { // rozhoduje jaký reproduktor hrál tón z tohoto vstupu
ledcDetach(leftSpeaker);
ledcAttachChannel(leftSpeaker, note2, LEDC_RESOLUTION, 1);
ledcWriteTone(leftSpeaker, note2);
}
if (*r == 1) { // rozhoduje jaký reproduktor hrál tón z tohoto vstupu
ledcDetach(rightSpeaker);
ledcAttachChannel(rightSpeaker, note2, LEDC_RESOLUTION, 2);
ledcWriteTone(rightSpeaker, note2);
}
*change = 0;
}
} else if (analogRead(anRead) > 1480 && 1700 > analogRead(anRead)) { // Hraje tón C ----------------------------------------
if (left == 0) { //Nehraje žádný tón
ledcDetach(rightSpeaker);
ledcAttachChannel(leftSpeaker, note1, LEDC_RESOLUTION, 1);
ledcWriteTone(leftSpeaker, note1);
left = 1;
right = 0;
*l = 1;
*r = 0;
*usg = 1;
}
if (left == 1 && right == 0 && *usg != 1) { // Hraje jeden tón
ledcAttachChannel(rightSpeaker, note1, LEDC_RESOLUTION, 2);
ledcWriteTone(rightSpeaker, note1);
right = 1;
*r = 1;
*usg = 1;
}
if (left == 1 && right == 1 && *usg >= 2) { // Hráli dva tóny z jednoho vstupu, ale ted hraje už jen jeden
//vzpnout oboje a yapnout jeden do leva
ledcDetach(leftSpeaker);
ledcDetach(rightSpeaker);
ledcAttachChannel(leftSpeaker, note1, LEDC_RESOLUTION, 1);
ledcWriteTone(leftSpeaker, note1);
left = 1;
right = 0;
*usg = 1;
}
if (left == 1 && right == 1 && *usg == 1 && *change == 1) { //Hraje jeden tón na jiném vstupu a na aktualním vstupu se rychle vyměnili dva tóny
if (*l == 1 && *change == 1) { // rozhoduje jaký reproduktor hrál tón z tohoto vstupu
ledcDetach(leftSpeaker);
ledcAttachChannel(leftSpeaker, note1, LEDC_RESOLUTION, 1);
ledcWriteTone(leftSpeaker, note1);
}
if (*r == 1) { // rozhoduje jaký reproduktor hrál tón z tohoto vstupu
ledcDetach(rightSpeaker);
ledcAttachChannel(rightSpeaker, note1, LEDC_RESOLUTION, 2);
ledcWriteTone(rightSpeaker, note1);
}
change = 0;
}
}
else if (analogRead(anRead) > 1700 && 1950 > analogRead(anRead)) { //kombinace tónů CIS, D -----------------------------------
if ((left == 1 && right == 0 && *usg == 1) || (left == 0 && right == 0)) { //Když hraje jeden tón a přidá se k němu druhý nebo nehraje žádný tón
ledcDetach(leftSpeaker);
ledcAttachChannel(leftSpeaker, note2, LEDC_RESOLUTION, 1);
ledcWriteTone(leftSpeaker, note2);
ledcDetach(rightSpeaker);
ledcAttachChannel(rightSpeaker, note3, LEDC_RESOLUTION, 2);
ledcWriteTone(rightSpeaker, note3);
*usg = 2;
left = 1;
right = 1;
}
if (left == 1 && right == 1 && *usg == 3) { //přepínaní dvou stisklích tl. na dvě stisklé na stejném vstupu
*usg = 2;
ledcDetach(leftSpeaker);
ledcAttachChannel(leftSpeaker, note2, LEDC_RESOLUTION, 1);
ledcWriteTone(leftSpeaker, note2);
ledcDetach(rightSpeaker);
ledcAttachChannel(rightSpeaker, note3, LEDC_RESOLUTION, 2);
ledcWriteTone(rightSpeaker, note3);
}
if (left == 1 && right == 1 && *usg == 1) { //přepínání jednoho tónu na vstupu a dvě stiklé tl. jsou přechod, když je jiné tl. stiskle na jinym vstupu
*change = 1;
}
} else if (analogRead(anRead) > 1050 && 1480 > analogRead(anRead)) { //kombinace C D ---------------------------
if ((left == 1 && right == 0 && usg1 == 1) || (left == 0 && right == 0)) { //Když hraje jeden tón a přidá se k němu druhý nebo nehraje žádný tón
ledcDetach(leftSpeaker);
ledcAttachChannel(leftSpeaker, note1, LEDC_RESOLUTION, 1);
ledcWriteTone(leftSpeaker, note1);
ledcDetach(rightSpeaker);
ledcAttachChannel(rightSpeaker, note3, LEDC_RESOLUTION, 2);
ledcWriteTone(rightSpeaker, note3);
*usg = 2;
left = 1;
right = 1;
}
if (left == 1 && right == 1 && *usg == 3) { //přepínaní dvou stisklích tl. na dvě stisklé na stejném vstupu
*usg = 2;
ledcDetach(leftSpeaker);
ledcAttachChannel(leftSpeaker, note1, LEDC_RESOLUTION, 1);
ledcWriteTone(leftSpeaker, note1);
ledcDetach(rightSpeaker);
ledcAttachChannel(rightSpeaker, note3, LEDC_RESOLUTION, 2);
ledcWriteTone(rightSpeaker, note3);
}
if (left == 1 && right == 1 && *usg == 1) { //přepínání jednoho tónu na vstupu a dvě stiklé tl. jsou přechod, když je jiné tl. stiskle na jinym vstupu
*change = 1;
}
} else if (analogRead(anRead) > 550 && 1050 > analogRead(anRead)) { //kombinace C CIS ---------------------------
if ((left == 1 && right == 0 && *usg == 1) || (left == 0 && right == 0)) { //Když hraje jeden tón a přidá se k němu druhý nebo nehraje žádný tón
ledcDetach(leftSpeaker);
ledcAttachChannel(leftSpeaker, note1, LEDC_RESOLUTION, 1);
ledcWriteTone(leftSpeaker, note1);
ledcDetach(rightSpeaker);
ledcAttachChannel(rightSpeaker, note2, LEDC_RESOLUTION, 2);
ledcWriteTone(rightSpeaker, note2);
*usg = 2;
left = 1;
right = 1;
}
if (left == 1 && right == 1 && *usg == 3) { //přepínaní dvou stisklích tl. na dvě stisklé na stejném vstupu
*usg = 2;
ledcDetach(leftSpeaker);
ledcAttachChannel(leftSpeaker, note1, LEDC_RESOLUTION, 1);
ledcWriteTone(leftSpeaker, note1);
ledcDetach(rightSpeaker);
ledcAttachChannel(rightSpeaker, note2, LEDC_RESOLUTION, 2);
ledcWriteTone(rightSpeaker, note2);
}
if (left == 1 && right == 1 && *usg == 1) { //přepínání jednoho tónu na vstupu a dvě stiklé tl. jsou přechod, když je jiné tl. stiskle na jinym vstupu
*change = 1;
}
}
else { // stisklé 3 tlačítka
*usg = 3;
}
}
void setup() {
Serial.begin(115000);
}
void loop() {
buttons(anRead1, NOTE_C4, NOTE_CSHARP4, NOTE_D4, &l1, &r1, &usg1, &change1);
buttons(anRead2, NOTE_DSHARP4, NOTE_E4, NOTE_F4, &l2, &r2, &usg2, &change2);
buttons(anRead3, NOTE_FSHARP4, NOTE_G4, NOTE_GSHARP4, &l3, &r3, &usg3, &change3);
buttons(anRead4, NOTE_A4, NOTE_ASHARP4, NOTE_B4, &l4, &r4, &usg4, &change4);
}