// Настройка пинов
constexpr uint8_t pinBuzzer = 9;
constexpr uint8_t pinLed = 3;
constexpr uint8_t pinSensor = A0;
// Частоты нот (в Гц)
const uint16_t scaleNotes[] = {
262, 294, 330, 349, 392, 440, 494, 523,
587, 659, 698, 784, 880, 988, 1047
};
constexpr uint8_t totalNotes = sizeof(scaleNotes) / sizeof(scaleNotes[0]);
void setup()
{
pinMode(pinBuzzer, OUTPUT);
pinMode(pinLed, OUTPUT);
}
void loop()
{
// Считывание показаний фоторезистора
uint16_t rawLight = analogRead(pinSensor);
// Определение индекса ноты на основе уровня освещенности
uint8_t noteIndex = map(rawLight, 0, 1023, 0, totalNotes - 1);
noteIndex = constrain(noteIndex, 0, totalNotes - 1);
uint16_t currentFreq = scaleNotes[noteIndex];
// Воспроизведение звука
tone(pinBuzzer, currentFreq);
// Включение светодиода, если частота порога превышает или равна 700 Гц
digitalWrite(pinLed, currentFreq >= 700);
delay(10);
}