#include <toneAC.h>
// Підключення LCD
const int joystickXPin = A0; // Пін для зчитування значення X з джойстика
const int joystickYPin = A1; // Пін для зчитування значення Y з джойстика
const int potPin = A2; // Пін для зчитування значення потенціометра
const int buzzerPin = 9; // Пін для з'єднання з Buzzer
const int redPin = 6; // Пін для з'єднання з Red світлодіодом
const int greenPin = 5; // Пін для з'єднання з Green світлодіодом
const int bluePin = 3; // Пін для з'єднання з Blue світлодіодом
int joystickXValue, joystickYValue, potValue;
/*
int melody[] = { 262, 196, 196, 220, 196, 0, 247, 262 };
int noteDurations[] = { 150, 200, 400, 600, 400, 800, 200, 100 };
*/
void setup() {
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
//lcd.begin(16, 2);
//lcd.backlight();
/*
for (int thisNote = 0; thisNote < 8; thisNote++) {
//int noteDuration = 1000 / noteDurations[thisNote];
toneAC(melody[thisNote], 10, noteDurations[thisNote], true); // Play thisNote at full volume for noteDuration in the background.
delay(noteDurations[thisNote] * 4 / 3); // Wait while the tone plays in the background, plus another 33% delay between notes.
}
for (unsigned long freq = 125; freq <= 15000; freq += 10) {
toneAC(freq); // Play the frequency (125 Hz to 15 kHz sweep in 10 Hz steps).
delay(1); // Wait 1 ms so you can hear it.
}
toneAC(); // Turn off toneAC, can also use noToneAC().*/
}
void loop() {
// Зчитуємо значення з джойстика та потенціометра
joystickXValue = analogRead(joystickXPin);
joystickYValue = analogRead(joystickYPin);
potValue = analogRead(potPin);
// Мапуємо значення X та Y джойстика до діапазону нот
int note = map(joystickXValue, 0, 1023, 50, 400);
// Регулюємо темп музики за допомогою потенціометра
int tempo = map(potValue, 0, 1023, 130, 1000);
// Відтворення ноти
toneAC(tempo, 5, note, true);
delay(note * 6 / 3);
// Додаткова функціональність - виведення інформації на Serial Monitor
Serial.print("note: ");
Serial.print(note);
Serial.print("\ttempo: ");
Serial.println(tempo);
analogWrite(redPin, map(tempo, 30, 300, 0, 255));
//analogWrite(greenPin, map(tempo, 30, 300, 0, 255));
analogWrite(bluePin, map(note, 50, 1000, 0, 255));
//delay(100);
/*
lcd.setCursor(0, 0);
lcd.print("Note: ");
lcd.print(note);
lcd.setCursor(0, 1);
lcd.print("Tempo: ");
lcd.print(tempo);
*/
}